| | |
| | | /// <summary> |
| | | /// 初始化UI管理器 |
| | | /// </summary> |
| | | public override void Init() |
| | | public override async UniTask Init() |
| | | { |
| | | base.Init(); |
| | | |
| | | // 初始化UI根节点 |
| | | InitUIRoot(); |
| | | |
| | | await InitUIRoot(); |
| | | // 初始化缓存 |
| | | layerSortingOrderCache.Clear(); |
| | | layerTransformCache.Clear(); |
| | | uiInstanceCounter.Clear(); |
| | | |
| | | DOTween.SetTweensCapacity(500, 50); |
| | | Debug.Log("UI管理器初始化完成"); |
| | | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 初始化UI根节点 |
| | | /// </summary> |
| | | private void InitUIRoot() |
| | | private async UniTask InitUIRoot() |
| | | { |
| | | // 查找UI根节点 |
| | | GameObject root = GameObject.Find("UIRoot"); |
| | |
| | | // 如果场景中没有UI根节点,则创建一个 |
| | | if (root == null) |
| | | { |
| | | root = GameObject.Instantiate(BuiltInLoader.LoadPrefab("UIRoot")); |
| | | var prefab = await BuiltInLoader.LoadPrefabAsync("UIRoot"); |
| | | root = GameObject.Instantiate(prefab); |
| | | root.name = "UIRoot"; |
| | | if (root == null) |
| | | { |
| | | Debug.LogError("无法找到UI根节点"); |
| | | return; |
| | | } |
| | | |
| | | // 添加DontDestroyOnLoad组件,确保UI根节点在场景切换时不被销毁 |
| | | GameObject.DontDestroyOnLoad(root); |
| | | } |
| | | |
| | |
| | | |
| | | #region UI资源管理 |
| | | |
| | | private UIBase LoadUIResource(string uiName) |
| | | { |
| | | // private UIBase LoadUIResource(string uiName) |
| | | // { |
| | | |
| | | // 从资源管理器加载UI预制体 |
| | | GameObject prefab; |
| | | if (uiName == "LaunchWin" || uiName == "DownLoadWin" || uiName == "RequestSecretWin" || uiName == "GameAgeWarnWin") |
| | | { |
| | | prefab = BuiltInLoader.LoadPrefab(uiName); |
| | | } |
| | | else |
| | | { |
| | | #pragma warning disable CS0618 // Obsolete — sync legacy fallback, use LoadUIResourceAsync |
| | | prefab = ResManager.Instance.LoadAsset<GameObject>("UI", uiName); |
| | | #pragma warning restore CS0618 |
| | | } |
| | | // // 从资源管理器加载UI预制体 |
| | | // GameObject prefab; |
| | | // if (uiName == "LaunchWin" || uiName == "DownLoadWin" || uiName == "RequestSecretWin" || uiName == "GameAgeWarnWin") |
| | | // { |
| | | // prefab = await BuiltInLoader.LoadPrefabAsync(uiName); |
| | | // } |
| | | // else |
| | | // { |
| | | // prefab = await ResManager.Instance.LoadAssetAsync<GameObject>("UI", uiName); |
| | | // } |
| | | |
| | | // 检查预制体是否加载成功 |
| | | if (prefab == null) |
| | | { |
| | | // 记录错误日志 |
| | | Debug.LogError($"加载UI预制体失败: {uiName}"); |
| | | return null; |
| | | } |
| | | // // 检查预制体是否加载成功 |
| | | // if (prefab == null) |
| | | // { |
| | | // // 记录错误日志 |
| | | // Debug.LogError($"加载UI预制体失败: {uiName}"); |
| | | // return null; |
| | | // } |
| | | |
| | | // 实例化UI对象 |
| | | GameObject uiObject = GameObject.Instantiate(prefab); |
| | | // 设置对象名称 |
| | | uiObject.name = uiName; |
| | | // // 实例化UI对象 |
| | | // GameObject uiObject = GameObject.Instantiate(prefab); |
| | | // // 设置对象名称 |
| | | // uiObject.name = uiName; |
| | | |
| | | // 通过uiName映射Type |
| | | Type uiType = Type.GetType(uiName); |
| | | if (uiType == null) |
| | | { |
| | | Debug.LogError($"找不到UI类型: {uiName}"); |
| | | return null; |
| | | } |
| | | // // 通过uiName映射Type |
| | | // Type uiType = Type.GetType(uiName); |
| | | // if (uiType == null) |
| | | // { |
| | | // Debug.LogError($"找不到UI类型: {uiName}"); |
| | | // return null; |
| | | // } |
| | | |
| | | // 获取UI基类组件 |
| | | UIBase uiBase = uiObject.GetComponent(uiType) as UIBase; |
| | | // // 获取UI基类组件 |
| | | // UIBase uiBase = uiObject.GetComponent(uiType) as UIBase; |
| | | |
| | | // 检查UI基类组件是否存在 |
| | | if (uiBase == null) |
| | | { |
| | | // 记录错误日志 |
| | | Debug.LogError($"UI预制体 {uiName} 没有 UIBase 组件或类型不匹配"); |
| | | return null; |
| | | } |
| | | // // 检查UI基类组件是否存在 |
| | | // if (uiBase == null) |
| | | // { |
| | | // // 记录错误日志 |
| | | // Debug.LogError($"UI预制体 {uiName} 没有 UIBase 组件或类型不匹配"); |
| | | // return null; |
| | | // } |
| | | |
| | | // 设置UI名称 |
| | | uiBase.uiName = uiName; |
| | | // // 设置UI名称 |
| | | // uiBase.uiName = uiName; |
| | | |
| | | // 设置父节点为UI根节点 |
| | | Transform parentTrans = GetTransForLayer(uiBase.uiLayer); |
| | | // // 设置父节点为UI根节点 |
| | | // Transform parentTrans = GetTransForLayer(uiBase.uiLayer); |
| | | |
| | | uiObject.transform.SetParent(parentTrans, false); |
| | | // uiObject.transform.SetParent(parentTrans, false); |
| | | |
| | | // 设置排序顺序 |
| | | int baseSortingOrder = GetBaseSortingOrderForLayer(uiBase.uiLayer); |
| | | uiBase.SetSortingOrder(baseSortingOrder); |
| | | // // 设置排序顺序 |
| | | // int baseSortingOrder = GetBaseSortingOrderForLayer(uiBase.uiLayer); |
| | | // uiBase.SetSortingOrder(baseSortingOrder); |
| | | |
| | | return uiBase; |
| | | } |
| | | // return uiBase; |
| | | // } |
| | | |
| | | // 加载UI预制体 |
| | | private T LoadUIResource<T>(string uiName) where T : UIBase |
| | | private async UniTask<T> LoadUIResource<T>(string uiName) where T : UIBase |
| | | { |
| | | return LoadUIResource(uiName) as T; |
| | | return await LoadUIResourceAsync(uiName) as T; |
| | | } |
| | | |
| | | // ==================================================================== |
| | |
| | | GameObject root = GameObject.Find("UIRoot"); |
| | | if (root == null) |
| | | { |
| | | var prefab = await BuiltInLoader.LoadPrefabAsync("UIRoot"); |
| | | var prefab = await BuiltInLoader.LoadPrefabAsync("UIRoot"); // 已异步,无需修改 |
| | | root = GameObject.Instantiate(prefab); |
| | | root.name = "UIRoot"; |
| | | if (root == null) |
| | |
| | | return null; |
| | | } |
| | | |
| | | public UIBase OpenWindow(string uiName, int functionOrder = 0) |
| | | public async UniTask<UIBase> OpenWindow(string uiName, int functionOrder = 0) |
| | | { |
| | | // 优先从closedUIDict中获取 |
| | | UIBase parentUI = null; |
| | |
| | | #if UNITY_EDITOR |
| | | Debug.Log("OpenWindow getNewLoad " + uiName); |
| | | #endif |
| | | returnValue = LoadUIResource(uiName); |
| | | returnValue = await LoadUIResourceAsync(uiName); |
| | | if (returnValue == null) |
| | | { |
| | | // 记录错误日志 |