Init.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 UnityEngine;
  10. namespace Model
  11. {
  12. public class Init : MonoBehaviour
  13. {
  14. public static Init Instance;
  15. public ILRuntime.Runtime.Enviorment.AppDomain AppDomain;
  16. private IStaticMethod start;
  17. private IStaticMethod update;
  18. private IStaticMethod lateUpdate;
  19. private IStaticMethod onApplicationQuit;
  20. private void Start()
  21. {
  22. try
  23. {
  24. if (Application.unityVersion != "2017.1.0f3")
  25. {
  26. Log.Error("请使用Unity2017正式版");
  27. return;
  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.Add("Model", typeof(Init).Assembly);
  44. Assembly hotfix = DllHelper.LoadHotfixAssembly();
  45. ObjectEvents.Instance.Add("Hotfix", hotfix);
  46. Type hotfixInit = hotfix.GetType("Hotfix.Init");
  47. this.start = new MonoStaticMethod(hotfixInit, "Start");
  48. this.update = new MonoStaticMethod(hotfixInit, "Update");
  49. this.lateUpdate = new MonoStaticMethod(hotfixInit, "LateUpdate");
  50. this.onApplicationQuit = new MonoStaticMethod(hotfixInit, "OnApplicationQuit");
  51. #endif
  52. // 进入热更新层
  53. this.start.Run();
  54. }
  55. catch (Exception e)
  56. {
  57. Log.Error(e.ToString());
  58. }
  59. }
  60. private void Update()
  61. {
  62. this.update.Run();
  63. ObjectEvents.Instance.Update();
  64. }
  65. private void LateUpdate()
  66. {
  67. this.lateUpdate.Run();
  68. ObjectEvents.Instance.LateUpdate();
  69. }
  70. private void OnApplicationQuit()
  71. {
  72. Instance = null;
  73. Game.Close();
  74. ObjectEvents.Close();
  75. this.onApplicationQuit.Run();
  76. }
  77. }
  78. }