Init.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Threading.Tasks;
  3. using CommandLine;
  4. using UnityEngine;
  5. namespace ET
  6. {
  7. public class Init: MonoBehaviour
  8. {
  9. private Process process;
  10. private void Start()
  11. {
  12. DontDestroyOnLoad(gameObject);
  13. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  14. {
  15. Log.Error(e.ExceptionObject.ToString());
  16. };
  17. // 命令行参数
  18. string[] args = "".Split(" ");
  19. Parser.Default.ParseArguments<Options>(args)
  20. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  21. .WithParsed(Game.Instance.AddSingleton);
  22. Game.Instance.AddSingleton<Logger>().ILog = new UnityLogger();
  23. process = Game.Instance.Create(false);
  24. process.AddSingleton<MainThreadSynchronizationContext>();
  25. process.AddSingleton<GlobalComponent>();
  26. Options.Instance.StartConfig = $"StartConfig/Localhost";
  27. process.AddSingleton<TimeInfo>();
  28. process.AddSingleton<ObjectPool>();
  29. process.AddSingleton<IdGenerater>();
  30. process.AddSingleton<EventSystem>();
  31. process.AddSingleton<TimerComponent>();
  32. process.AddSingleton<CoroutineLockComponent>();
  33. ETTask.ExceptionHandler += Log.Error;
  34. process.AddSingleton<CodeLoader>().Start();
  35. Task.Run(() =>
  36. {
  37. while (true)
  38. {
  39. Game.Instance.Loop();
  40. }
  41. });
  42. }
  43. private void Update()
  44. {
  45. process.Update();
  46. }
  47. private void LateUpdate()
  48. {
  49. process.LateUpdate();
  50. process.FrameFinishUpdate();
  51. }
  52. private void OnApplicationQuit()
  53. {
  54. this.process.Dispose();
  55. }
  56. }
  57. }