Init.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.0p5")
  25. {
  26. Log.Error("请使用Unity2017.1.0p5版本");
  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.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. // 进入热更新层
  51. this.start.Run();
  52. }
  53. catch (Exception e)
  54. {
  55. Log.Error(e.ToString());
  56. }
  57. }
  58. private void Update()
  59. {
  60. this.update.Run();
  61. ObjectEvents.Instance.Update();
  62. }
  63. private void LateUpdate()
  64. {
  65. this.lateUpdate.Run();
  66. ObjectEvents.Instance.LateUpdate();
  67. }
  68. private void OnApplicationQuit()
  69. {
  70. Instance = null;
  71. Game.Close();
  72. ObjectEvents.Close();
  73. this.onApplicationQuit.Run();
  74. }
  75. }
  76. }