Init.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Threading;
  5. using ILRuntime.CLR.Method;
  6. using ILRuntime.CLR.TypeSystem;
  7. using ILRuntime.Runtime.Enviorment;
  8. using UnityEngine;
  9. namespace Model
  10. {
  11. public class Init : MonoBehaviour
  12. {
  13. public static Init Instance;
  14. public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;
  15. private IStaticMethod start;
  16. private IStaticMethod update;
  17. private IStaticMethod lateUpdate;
  18. private IStaticMethod onApplicationQuit;
  19. private void Start()
  20. {
  21. try
  22. {
  23. if (Application.unityVersion != "2017.1.0f3")
  24. {
  25. Log.Error("请使用Unity2017正式版");
  26. return;
  27. }
  28. DontDestroyOnLoad(gameObject);
  29. Instance = this;
  30. ObjectEvents.Instance.Add("Model", typeof(Init).Assembly);
  31. #if ILRuntime
  32. Log.Debug("run in ilruntime mode");
  33. this.AppDomain = new ILRuntime.Runtime.Enviorment.AppDomain();
  34. DllHelper.LoadHotfixAssembly();
  35. ILHelper.RegisterILAdapter();
  36. ILHelper.RegisterDelegate();
  37. ILHelper.RegisterRedirection();
  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.Add("Model", typeof(Init).Assembly);
  45. Assembly hotfix = DllHelper.LoadHotfixAssembly();
  46. ObjectEvents.Instance.Add("Hotfix", hotfix);
  47. Type hotfixInit = hotfix.GetType("Hotfix.Init");
  48. this.start = new MonoStaticMethod(hotfixInit, "Start");
  49. this.update = new MonoStaticMethod(hotfixInit, "Update");
  50. this.lateUpdate = new MonoStaticMethod(hotfixInit, "LateUpdate");
  51. this.onApplicationQuit = new MonoStaticMethod(hotfixInit, "OnApplicationQuit");
  52. #endif
  53. // 进入热更新层
  54. this.start.Run();
  55. }
  56. catch (Exception e)
  57. {
  58. Log.Error(e.ToString());
  59. }
  60. }
  61. private void Update()
  62. {
  63. this.update.Run();
  64. ObjectEvents.Instance.Update();
  65. }
  66. private void LateUpdate()
  67. {
  68. this.lateUpdate.Run();
  69. ObjectEvents.Instance.LateUpdate();
  70. }
  71. private void OnApplicationQuit()
  72. {
  73. Instance = null;
  74. Game.Close();
  75. ObjectEvents.Close();
  76. this.onApplicationQuit.Run();
  77. }
  78. }
  79. }