Init.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 UnityEngine;
  10. namespace Model
  11. {
  12. public class Init : MonoBehaviour
  13. {
  14. public static Init Instance;
  15. public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;
  16. private IStaticMethod start;
  17. public Action HotfixUpdate;
  18. public Action HotfixLateUpdate;
  19. public Action HotfixOnApplicationQuit;
  20. private async void Start()
  21. {
  22. try
  23. {
  24. if (Application.unityVersion != "2017.1.0p5")
  25. {
  26. Log.Warning($"当前版本:{Application.unityVersion}, 最好使用运行指南推荐版本!");
  27. }
  28. DontDestroyOnLoad(gameObject);
  29. Instance = this;
  30. Game.EventSystem.Add(DLLType.Model, typeof(Init).Assembly);
  31. Game.Scene.AddComponent<GlobalConfigComponent>();
  32. Game.Scene.AddComponent<NetOuterComponent>();
  33. Game.Scene.AddComponent<ResourcesComponent>();
  34. Game.Scene.AddComponent<BehaviorTreeComponent>();
  35. Game.Scene.AddComponent<PlayerComponent>();
  36. Game.Scene.AddComponent<UnitComponent>();
  37. Game.Scene.AddComponent<ClientFrameComponent>();
  38. Game.Scene.AddComponent<UIComponent>();
  39. // 下载ab包
  40. await BundleHelper.DownloadBundle();
  41. // 加载配置
  42. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle("config.unity3d");
  43. Game.Scene.AddComponent<ConfigComponent>();
  44. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle("config.unity3d");
  45. Game.Scene.AddComponent<MessageDispatherComponent>();
  46. #if ILRuntime
  47. Log.Debug("run in ilruntime mode");
  48. this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  49. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
  50. Game.EventSystem.LoadHotfixDll();
  51. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
  52. ILHelper.InitILRuntime();
  53. this.start = new ILStaticMethod("Hotfix.Init", "Start", 0);
  54. #else
  55. Log.Debug("run in mono mode");
  56. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
  57. Game.EventSystem.LoadHotfixDll();
  58. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
  59. Type hotfixInit = Game.EventSystem.HotfixAssembly.GetType("Hotfix.Init");
  60. this.start = new MonoStaticMethod(hotfixInit, "Start");
  61. #endif
  62. Game.Scene.AddComponent<OpcodeTypeComponent>();
  63. // 进入热更新层
  64. this.start.Run();
  65. Game.EventSystem.Run(EventIdType.TestHotfixSubscribMonoEvent, "TestHotfixSubscribMonoEvent");
  66. }
  67. catch (Exception e)
  68. {
  69. Log.Error(e.ToString());
  70. }
  71. }
  72. private void Update()
  73. {
  74. this.HotfixUpdate.Invoke();
  75. Game.EventSystem.Update();
  76. }
  77. private void LateUpdate()
  78. {
  79. this.HotfixLateUpdate.Invoke();
  80. Game.EventSystem.LateUpdate();
  81. }
  82. private void OnApplicationQuit()
  83. {
  84. Instance = null;
  85. Game.Close();
  86. this.HotfixOnApplicationQuit.Invoke();
  87. }
  88. }
  89. }