Init.cs 2.5 KB

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