Init.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using CommandLine;
  3. using UnityEngine;
  4. namespace ET
  5. {
  6. public class Init: MonoBehaviour
  7. {
  8. private void Awake()
  9. {
  10. #if UNITY_EDITOR
  11. //Editor模式下去掉debug浮窗插件
  12. var debugObj = GameObject.Find("IngameDebugConsole");
  13. if (debugObj != null)
  14. {
  15. GameObject.Destroy(debugObj);
  16. }
  17. var globalConfig = Resources.Load<GlobalConfig>("GlobalConfig");
  18. if (globalConfig.PlayMode != YooAsset.YooAssets.EPlayMode.EditorSimulateMode)
  19. {
  20. var window = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow), true, "Game");
  21. if(window != null)
  22. {
  23. window.ShowNotification(new GUIContent($"PlayMode is {globalConfig.PlayMode}"), 5);
  24. }
  25. return;
  26. }
  27. #endif
  28. DontDestroyOnLoad(gameObject);
  29. //登陆过程限定帧率为30fps
  30. Application.targetFrameRate = (int)FPS.Login;
  31. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  32. {
  33. Log.Error(e.ExceptionObject.ToString());
  34. };
  35. Game.AddSingleton<MainThreadSynchronizationContext>();
  36. // 命令行参数
  37. string[] args = "".Split(" ");
  38. Parser.Default.ParseArguments<Options>(args)
  39. .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
  40. .WithParsed(Game.AddSingleton);
  41. Game.AddSingleton<TimeInfo>();
  42. Game.AddSingleton<Logger>().ILog = new UnityLogger();
  43. Game.AddSingleton<ObjectPool>();
  44. Game.AddSingleton<IdGenerater>();
  45. Game.AddSingleton<EventSystem>();
  46. Game.AddSingleton<TimerComponent>();
  47. Game.AddSingleton<CoroutineLockComponent>();
  48. ETTask.ExceptionHandler += Log.Error;
  49. Game.AddSingleton<CodeLoader>().Start();
  50. }
  51. private void Update()
  52. {
  53. Game.Update((int)(Time.deltaTime * 1000));
  54. }
  55. private void LateUpdate()
  56. {
  57. Game.LateUpdate();
  58. Game.FrameFinishUpdate();
  59. }
  60. private void OnApplicationQuit()
  61. {
  62. Game.Close();
  63. }
  64. }
  65. }