Init.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. byte[] log = new byte[1024];
  41. Interpreter.InterpreterSetLog((buff, n) =>
  42. {
  43. Marshal.Copy(buff, log, 0, n);
  44. UnityEngine.Debug.Log(log.Utf8ToStr(0, n));
  45. });
  46. Interpreter.InterpreterInit("./Temp/Bin/Debug/", "Unity.Script.dll");
  47. /*
  48. UnityEngine.Debug.Log("unity script mode!");
  49. byte[] dllBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.dll");
  50. byte[] pdbBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.pdb");
  51. modelAssembly = Assembly.Load(dllBytes, pdbBytes);
  52. */
  53. }
  54. Type initType = modelAssembly.GetType("ET.Entry");
  55. this.entry = Activator.CreateInstance(initType) as IEntry;
  56. }
  57. private void Start()
  58. {
  59. this.entry.Start();
  60. }
  61. private void Update()
  62. {
  63. this.entry.Update();
  64. }
  65. private void LateUpdate()
  66. {
  67. this.entry.LateUpdate();
  68. }
  69. private void OnApplicationQuit()
  70. {
  71. this.entry.OnApplicationQuit();
  72. }
  73. }
  74. }