Init.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. Game.Scene.AddComponent<PlayerComponent>();
  57. Game.Scene.AddComponent<UnitComponent>();
  58. Game.Scene.AddComponent<ClientFrameComponent>();
  59. // 进入热更新层
  60. this.start.Run();
  61. }
  62. catch (Exception e)
  63. {
  64. Log.Error(e.ToString());
  65. }
  66. }
  67. private void Update()
  68. {
  69. this.update.Run();
  70. ObjectEvents.Instance.Update();
  71. }
  72. private void LateUpdate()
  73. {
  74. this.lateUpdate.Run();
  75. ObjectEvents.Instance.LateUpdate();
  76. }
  77. private void OnApplicationQuit()
  78. {
  79. Instance = null;
  80. Game.Close();
  81. ObjectEvents.Close();
  82. this.onApplicationQuit.Run();
  83. }
  84. }
  85. }