Init.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using UnityEngine;
  7. namespace ET
  8. {
  9. public interface IEntry
  10. {
  11. void Start();
  12. void Update();
  13. void LateUpdate();
  14. void OnApplicationQuit();
  15. }
  16. public class Init: MonoBehaviour
  17. {
  18. private IEntry entry;
  19. private void Awake()
  20. {
  21. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  22. DontDestroyOnLoad(gameObject);
  23. Assembly modelAssembly = null;
  24. if (Define.IsEditor)
  25. {
  26. UnityEngine.Debug.Log("unity editor mode!");
  27. foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  28. {
  29. string assemblyName = $"{assembly.GetName().Name}.dll";
  30. if (assemblyName != "Unity.ModelView.dll")
  31. {
  32. continue;
  33. }
  34. modelAssembly = assembly;
  35. break;
  36. }
  37. }
  38. else
  39. {
  40. UnityEngine.Debug.Log("unity standalone mode!");
  41. foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  42. {
  43. string assemblyName = $"{assembly.GetName().Name}.dll";
  44. if (assemblyName != "Unity.ModelView.dll")
  45. {
  46. continue;
  47. }
  48. modelAssembly = assembly;
  49. break;
  50. }
  51. /*
  52. byte[] log = new byte[1024];
  53. Interpreter.InterpreterSetLog((buff, n) =>
  54. {
  55. Marshal.Copy(buff, log, 0, n);
  56. UnityEngine.Debug.Log(log.Utf8ToStr(0, n));
  57. });
  58. Interpreter.InterpreterInit(@"E:\ET\Unity\UnityScript\", "Unity.Script.dll");
  59. UnityEngine.Debug.Log("unity script mode!");
  60. byte[] dllBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.dll");
  61. byte[] pdbBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.pdb");
  62. modelAssembly = Assembly.Load(dllBytes, pdbBytes);
  63. */
  64. }
  65. Type initType = modelAssembly.GetType("ET.Entry");
  66. this.entry = Activator.CreateInstance(initType) as IEntry;
  67. }
  68. private void Start()
  69. {
  70. this.entry.Start();
  71. }
  72. private void Update()
  73. {
  74. this.entry.Update();
  75. }
  76. private void LateUpdate()
  77. {
  78. this.entry.LateUpdate();
  79. }
  80. private void OnApplicationQuit()
  81. {
  82. this.entry.OnApplicationQuit();
  83. }
  84. }
  85. }