Init.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 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. ObjectEvents.Instance.Add("Model", typeof(Init).Assembly);
  32. #if ILRuntime
  33. Log.Debug("run in ilruntime mode");
  34. this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  35. DllHelper.LoadHotfixAssembly();
  36. ILHelper.InitILRuntime();
  37. this.start = new ILStaticMethod("Hotfix.Init", "Start", 0);
  38. this.update = new ILStaticMethod("Hotfix.Init", "Update", 0);
  39. this.lateUpdate = new ILStaticMethod("Hotfix.Init", "LateUpdate", 0);
  40. this.onApplicationQuit = new ILStaticMethod("Hotfix.Init", "OnApplicationQuit", 0);
  41. #else
  42. Log.Debug("run in mono mode");
  43. ObjectEvents.Instance.HotfixAssembly = DllHelper.LoadHotfixAssembly();
  44. Type hotfixInit = ObjectEvents.Instance.HotfixAssembly.GetType("Hotfix.Init");
  45. this.start = new MonoStaticMethod(hotfixInit, "Start");
  46. this.update = new MonoStaticMethod(hotfixInit, "Update");
  47. this.lateUpdate = new MonoStaticMethod(hotfixInit, "LateUpdate");
  48. this.onApplicationQuit = new MonoStaticMethod(hotfixInit, "OnApplicationQuit");
  49. #endif
  50. Game.Scene.AddComponent<OpcodeTypeComponent>();
  51. Game.Scene.AddComponent<MessageDispatherComponent>();
  52. Game.Scene.AddComponent<NetOuterComponent>();
  53. Game.Scene.AddComponent<ResourcesComponent>();
  54. Game.Scene.AddComponent<BehaviorTreeComponent>();
  55. Game.Scene.AddComponent<ConfigComponent>();
  56. // 进入热更新层
  57. this.start.Run();
  58. }
  59. catch (Exception e)
  60. {
  61. Log.Error(e.ToString());
  62. }
  63. }
  64. private void Update()
  65. {
  66. this.update.Run();
  67. ObjectEvents.Instance.Update();
  68. }
  69. private void LateUpdate()
  70. {
  71. this.lateUpdate.Run();
  72. ObjectEvents.Instance.LateUpdate();
  73. }
  74. private void OnApplicationQuit()
  75. {
  76. Instance = null;
  77. Game.Close();
  78. ObjectEvents.Close();
  79. this.onApplicationQuit.Run();
  80. }
  81. }
  82. }