using System; using System.Threading; using CommandLine; using UnityEngine; namespace ET { public class Init: MonoBehaviour { private void Start() { DontDestroyOnLoad(gameObject); // 为当前应用程序域添加未处理异常的事件处理器 AppDomain.CurrentDomain.UnhandledException += (sender, e) => { // 记录异常信息到日志中 Log.Error(e.ExceptionObject.ToString()); }; // 添加线程同步处理功能 Game.AddSingleton(); // 命令行参数 // 使用 CommandLineParser 来标准化地解析命令行 // CommandLineParser 是一款用于解析命令行参数的 NuGet 包。 // 我们可以将下面的参数改为string[] args = "--AppType GameTool --StartConfig StartConfig/Localhost --Process 2 --Develop 1 --LogLevel 2 --Console 1 --CreateScenes 0".Split(" "); string[] args = "".Split(" "); Parser.Default.ParseArguments(args) // 将args数组中的元素解析为Options类型的对象 .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))// 如果解析失败,调用WithNotParsed方法,抛出一个异常,异常信息包含错误原因 .WithParsed(Game.AddSingleton);// 如果解析成功,调用WithParsed方法,将解析得到的Options对象作为参数传递给Game.AddSingleton方法 Game.AddSingleton(); Game.AddSingleton().ILog = new UnityLogger(); Game.AddSingleton(); Game.AddSingleton(); Game.AddSingleton(); Game.AddSingleton(); Game.AddSingleton(); ETTask.ExceptionHandler += Log.Error; Game.AddSingleton().Start(); } private void Update() { // 更新游戏中的单例对象 Game.Update((int)(Time.deltaTime * 1000)); } private void LateUpdate() { // 更新游戏中的单例对象 Game.LateUpdate(); Game.FrameFinishUpdate(); } private void OnApplicationQuit() { Game.Close(); } } }