Init.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Warning($"当前版本:{Application.unityVersion}, 最好使用运行指南推荐版本!");
  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<NetOuterComponent>();
  34. Game.Scene.AddComponent<ResourcesComponent>();
  35. Game.Scene.AddComponent<BehaviorTreeComponent>();
  36. Game.Scene.AddComponent<PlayerComponent>();
  37. Game.Scene.AddComponent<UnitComponent>();
  38. Game.Scene.AddComponent<ClientFrameComponent>();
  39. Game.Scene.AddComponent<UIComponent>();
  40. // 下载ab包
  41. await BundleHelper.DownloadBundle();
  42. // 加载配置
  43. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle("config.unity3d");
  44. Game.Scene.AddComponent<ConfigComponent>();
  45. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle("config.unity3d");
  46. Game.Scene.AddComponent<MessageDispatherComponent>();
  47. #if ILRuntime
  48. Log.Debug("run in ilruntime mode");
  49. this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  50. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
  51. Game.EventSystem.LoadHotfixDll();
  52. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
  53. ILHelper.InitILRuntime();
  54. this.start = new ILStaticMethod("Hotfix.Init", "Start", 0);
  55. this.update = new ILStaticMethod("Hotfix.Init", "Update", 0);
  56. this.lateUpdate = new ILStaticMethod("Hotfix.Init", "LateUpdate", 0);
  57. this.onApplicationQuit = new ILStaticMethod("Hotfix.Init", "OnApplicationQuit", 0);
  58. #else
  59. Log.Debug("run in mono mode");
  60. Game.Scene.GetComponent<ResourcesComponent>().LoadBundle($"code.unity3d");
  61. Game.EventSystem.LoadHotfixDll();
  62. Game.Scene.GetComponent<ResourcesComponent>().UnloadBundle($"code.unity3d");
  63. Type hotfixInit = Game.EventSystem.HotfixAssembly.GetType("Hotfix.Init");
  64. this.start = new MonoStaticMethod(hotfixInit, "Start");
  65. this.update = new MonoStaticMethod(hotfixInit, "Update");
  66. this.lateUpdate = new MonoStaticMethod(hotfixInit, "LateUpdate");
  67. this.onApplicationQuit = new MonoStaticMethod(hotfixInit, "OnApplicationQuit");
  68. #endif
  69. Game.Scene.AddComponent<OpcodeTypeComponent>();
  70. // 进入热更新层
  71. this.start.Run();
  72. }
  73. catch (Exception e)
  74. {
  75. Log.Error(e.ToString());
  76. }
  77. }
  78. private void Update()
  79. {
  80. this.update?.Run();
  81. Game.EventSystem.Update();
  82. }
  83. private void LateUpdate()
  84. {
  85. this.lateUpdate?.Run();
  86. Game.EventSystem.LateUpdate();
  87. }
  88. private void OnApplicationQuit()
  89. {
  90. Instance = null;
  91. Game.Close();
  92. this.onApplicationQuit?.Run();
  93. }
  94. }
  95. }