Init.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using System.Threading;
  5. using UnityEngine;
  6. namespace ET
  7. {
  8. public interface IEntry
  9. {
  10. void Start();
  11. void Update();
  12. void LateUpdate();
  13. void OnApplicationQuit();
  14. }
  15. public class Init: MonoBehaviour
  16. {
  17. private IEntry entry;
  18. private void Awake()
  19. {
  20. SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Instance);
  21. DontDestroyOnLoad(gameObject);
  22. Assembly modelAssembly = null;
  23. if (Define.IsEditor)
  24. {
  25. UnityEngine.Debug.Log("unity editor mode!");
  26. foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  27. {
  28. string assemblyName = $"{assembly.GetName().Name}.dll";
  29. if (assemblyName != "Unity.ModelView.dll")
  30. {
  31. continue;
  32. }
  33. modelAssembly = assembly;
  34. break;
  35. }
  36. }
  37. else
  38. {
  39. UnityEngine.Debug.Log("unity script mode!");
  40. byte[] dllBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.dll");
  41. byte[] pdbBytes = File.ReadAllBytes("./Temp/Bin/Debug/Unity.Script.pdb");
  42. modelAssembly = Assembly.Load(dllBytes, pdbBytes);
  43. }
  44. Type initType = modelAssembly.GetType("ET.Entry");
  45. this.entry = Activator.CreateInstance(initType) as IEntry;
  46. }
  47. private void Start()
  48. {
  49. this.entry.Start();
  50. }
  51. private void Update()
  52. {
  53. this.entry.Update();
  54. }
  55. private void LateUpdate()
  56. {
  57. this.entry.LateUpdate();
  58. }
  59. private void OnApplicationQuit()
  60. {
  61. this.entry.OnApplicationQuit();
  62. }
  63. }
  64. }