Init.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using CommandLine;
  3. using UnityEngine;
  4. namespace ET
  5. {
  6. public class Init: MonoBehaviour
  7. {
  8. public static Init Instance { get; private set; }
  9. private void Start()
  10. {
  11. Instance = this;
  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(World.Instance.AddSingleton);
  22. Options.Instance.StartConfig = $"StartConfig/Localhost";
  23. World.Instance.AddSingleton<Logger>().ILog = new UnityLogger();
  24. World.Instance.AddSingleton<ObjectPool>();
  25. World.Instance.AddSingleton<EventSystem>();
  26. MainThreadScheduler mainThreadScheduler = World.Instance.AddSingleton<MainThreadScheduler>();
  27. ETTask.ExceptionHandler += Log.Error;
  28. VProcess vProcess = VProcessSingleton.Instance.Create();
  29. mainThreadScheduler.Add(vProcess);
  30. vProcess.AddSingleton<MainThreadSynchronizationContext>();
  31. vProcess.AddSingleton<GlobalComponent>();
  32. vProcess.AddSingleton<TimeInfo>();
  33. vProcess.AddSingleton<IdGenerater>();
  34. vProcess.AddSingleton<TimerComponent>();
  35. vProcess.AddSingleton<CoroutineLockComponent>();
  36. World.Instance.AddSingleton<CodeLoader>().Start();
  37. }
  38. private void Update()
  39. {
  40. MainThreadScheduler.Instance.Update();
  41. }
  42. private void LateUpdate()
  43. {
  44. MainThreadScheduler.Instance.LateUpdate();
  45. }
  46. private void OnApplicationQuit()
  47. {
  48. World.Instance.Dispose();
  49. }
  50. }
  51. }