Init.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.InitILRuntime();
  36. this.start = new ILStaticMethod("Hotfix.Init", "Start", 0);
  37. this.update = new ILStaticMethod("Hotfix.Init", "Update", 0);
  38. this.lateUpdate = new ILStaticMethod("Hotfix.Init", "LateUpdate", 0);
  39. this.onApplicationQuit = new ILStaticMethod("Hotfix.Init", "OnApplicationQuit", 0);
  40. #else
  41. Log.Debug("run in mono mode");
  42. ObjectEvents.Instance.Add("Model", typeof(Init).Assembly);
  43. Assembly hotfix = DllHelper.LoadHotfixAssembly();
  44. ObjectEvents.Instance.Add("Hotfix", hotfix);
  45. Type hotfixInit = hotfix.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. // 进入热更新层
  52. this.start.Run();
  53. }
  54. catch (Exception e)
  55. {
  56. Log.Error(e.ToString());
  57. }
  58. }
  59. private void Update()
  60. {
  61. this.update.Run();
  62. ObjectEvents.Instance.Update();
  63. }
  64. private void LateUpdate()
  65. {
  66. this.lateUpdate.Run();
  67. ObjectEvents.Instance.LateUpdate();
  68. }
  69. private void OnApplicationQuit()
  70. {
  71. Instance = null;
  72. Game.Close();
  73. ObjectEvents.Close();
  74. this.onApplicationQuit.Run();
  75. }
  76. }
  77. }