Init.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Threading;
  6. using ILRuntime.CLR.Method;
  7. using ILRuntime.CLR.TypeSystem;
  8. using ILRuntime.Runtime.Enviorment;
  9. using MongoDB.Bson.IO;
  10. using UnityEngine;
  11. namespace Model
  12. {
  13. public class Init : MonoBehaviour
  14. {
  15. public static Init Instance;
  16. public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;
  17. private IStaticMethod start;
  18. private IStaticMethod update;
  19. private IStaticMethod lateUpdate;
  20. private IStaticMethod onApplicationQuit;
  21. private async void Start()
  22. {
  23. try
  24. {
  25. if (Application.unityVersion != "2017.1.0p5")
  26. {
  27. Log.Error("请使用Unity2017.1.0p5版本");
  28. }
  29. DontDestroyOnLoad(gameObject);
  30. Instance = this;
  31. Game.EventSystem.Add(DLLType.Model, typeof(Init).Assembly);
  32. Game.Scene.AddComponent<GlobalConfigComponent>();
  33. Game.Scene.AddComponent<OpcodeTypeComponent>();
  34. Game.Scene.AddComponent<NetOuterComponent>();
  35. Game.Scene.AddComponent<ResourcesComponent>();
  36. Game.Scene.AddComponent<BehaviorTreeComponent>();
  37. Game.Scene.AddComponent<PlayerComponent>();
  38. Game.Scene.AddComponent<UnitComponent>();
  39. Game.Scene.AddComponent<ClientFrameComponent>();
  40. Game.Scene.AddComponent<UIComponent>();
  41. // 下载ab包
  42. await BundleHelper.DownloadBundle();
  43. // 加载配置
  44. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle("config.unity3d");
  45. Game.Scene.AddComponent<ConfigComponent>();
  46. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle("config.unity3d");
  47. Game.Scene.AddComponent<MessageDispatherComponent>();
  48. #if ILRuntime
  49. Log.Debug("run in ilruntime mode");
  50. this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  51. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
  52. Game.EventSystem.LoadHotfixDll();
  53. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
  54. ILHelper.InitILRuntime();
  55. this.start = new ILStaticMethod("Hotfix.Init", "Start", 0);
  56. this.update = new ILStaticMethod("Hotfix.Init", "Update", 0);
  57. this.lateUpdate = new ILStaticMethod("Hotfix.Init", "LateUpdate", 0);
  58. this.onApplicationQuit = new ILStaticMethod("Hotfix.Init", "OnApplicationQuit", 0);
  59. #else
  60. Log.Debug("run in mono mode");
  61. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
  62. Game.EventSystem.LoadHotfixDll();
  63. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
  64. Type hotfixInit = Game.EventSystem.HotfixAssembly.GetType("Hotfix.Init");
  65. this.start = new MonoStaticMethod(hotfixInit, "Start");
  66. this.update = new MonoStaticMethod(hotfixInit, "Update");
  67. this.lateUpdate = new MonoStaticMethod(hotfixInit, "LateUpdate");
  68. this.onApplicationQuit = new MonoStaticMethod(hotfixInit, "OnApplicationQuit");
  69. #endif
  70. // 进入热更新层
  71. this.start.Run();
  72. Game.EventSystem.Run(EventIdType.InitSceneStart);
  73. }
  74. catch (Exception e)
  75. {
  76. Log.Error(e.ToString());
  77. }
  78. }
  79. private void Update()
  80. {
  81. this.update?.Run();
  82. Game.EventSystem.Update();
  83. }
  84. private void LateUpdate()
  85. {
  86. this.lateUpdate?.Run();
  87. Game.EventSystem.LateUpdate();
  88. }
  89. private void OnApplicationQuit()
  90. {
  91. Instance = null;
  92. Game.Close();
  93. this.onApplicationQuit?.Run();
  94. }
  95. }
  96. }