using System.Collections; using System.Collections.Generic; using UnityEngine; public class ViewManager2 { private class ViewCacheInfo { public string fullPath; public GameObject gameObject; } private static List _ViewCacheInfos = new(); private static GameObject currentViewParent; public static void ShowView(string path) { InstantiateView(path); } public static GameObject getGameObjectAndShowView(string path) { GameObject o = InstantiateView(path); return o; } static GameObject InstantiateView(string path) { int groupIndex = 0; //根据条件设置group if (path == Path_InfraredScreenPositioningView) { groupIndex = 1; } //查找创造的ViewMgr 第一个层级作为ViewManager2的父节点 currentViewParent = ViewMgr.Instance.transform.Find(groupIndex.ToString()).gameObject; string fullPath = "SmartBow/Prefabs/Views/" + path; _DestroyExistViews(fullPath); GameObject o = Object.Instantiate(Resources.Load(fullPath), currentViewParent.transform); // 根据条件是否设置原来的sorting if (path == Path_GameResultView || path == Path_GameRankView) { o.GetComponent().overrideSorting = true; } // 检查并移除 Canvas Scaler 组件 //UnityEngine.UI.CanvasScaler canvasScaler = o.GetComponent(); //if (canvasScaler != null) //{ // Object.Destroy(canvasScaler); //} // 返回删除CanvasScaler对象 // 设置CanvasScaler RectTransform 的属性 RectTransform rectTransform = RemoveCanvasScaler(o.transform); //RectTransform rectTransform = o.GetComponent(); //if (rectTransform != null) //{ // rectTransform.localScale = Vector3.one; // 重置缩放 // rectTransform.sizeDelta = ((RectTransform)currentViewParent.transform).rect.size; // 设置大小 // rectTransform.anchoredPosition = Vector2.zero; // 重置位置 //} // 设置 RectTransform 全屏适应父级 if (rectTransform != null) { if (rectTransform == o.GetComponent()) { rectTransform.anchorMin = Vector2.zero; // 左下角对齐父级 rectTransform.anchorMax = Vector2.one; // 右上角对齐父级 rectTransform.offsetMin = Vector2.zero; // 移除左下角偏移 rectTransform.offsetMax = Vector2.zero; // 移除右上角偏移 rectTransform.localScale = Vector3.one; // 确保缩放为 1 } else { rectTransform.pivot = new Vector2(0.5f, 0.5f); rectTransform.localScale = Vector3.one; // 确保缩放为 1 rectTransform.sizeDelta = ((RectTransform)currentViewParent.transform).rect.size; // 设置大小 rectTransform.anchoredPosition = Vector2.zero; // 重置位置 } } ViewCacheInfo viewCacheInfo = new ViewCacheInfo(); viewCacheInfo.fullPath = fullPath; viewCacheInfo.gameObject = o; _ViewCacheInfos.Add(viewCacheInfo); return o; } static RectTransform RemoveCanvasScaler(Transform parent) { // 检查自身是否有 Canvas 组件 Canvas canvas = parent.GetComponent(); if (canvas != null) { // 检查并移除 Canvas Scaler 组件 UnityEngine.UI.CanvasScaler canvasScaler = parent.GetComponent(); if (canvasScaler != null) { Object.Destroy(canvasScaler); return parent.GetComponent(); // 返回删除 CanvasScaler 的 RectTransform } } else { // 如果没有 Canvas,轮询子物体 foreach (Transform child in parent) { RectTransform result = RemoveCanvasScaler(child); if (result != null) // 如果找到了被删除的 RectTransform,则返回 { return result; } } } return null; // 如果没有找到,则返回 null } /// /// 测试使用 /// /// /// public static GameObject ShowTestInfraredScreenPositioningView(Transform mParent) { string fullPath = "SmartBow/Prefabs/Views/" + Path_InfraredScreenPositioningView; GameObject o = Object.Instantiate(Resources.Load(fullPath), mParent); Transform children = o.transform.Find("ScreenPositioningView"); UnityEngine.UI.CanvasScaler canvasScaler = children.GetComponent(); if (canvasScaler != null) { Object.Destroy(canvasScaler); } RectTransform rectTransform = children.GetComponent(); rectTransform.pivot = new Vector2(0.5f, 0.5f); rectTransform.localScale = Vector3.one; // 确保缩放为 1 rectTransform.sizeDelta = ((RectTransform)mParent.parent.transform).rect.size; // 设置大小 rectTransform.anchoredPosition = Vector2.zero; // 重置位置 ViewCacheInfo viewCacheInfo = new ViewCacheInfo(); viewCacheInfo.fullPath = fullPath; viewCacheInfo.gameObject = o; _ViewCacheInfos.Add(viewCacheInfo); return o; } /** * 跳转场景时候,所有UI视图 */ public static void HideAllView() { foreach (var viewCacheInfo in _ViewCacheInfos) { if (viewCacheInfo.gameObject) { Object.Destroy(viewCacheInfo.gameObject); } } _ViewCacheInfos.Clear(); } public static void HideView(string path) { string fullPath = "SmartBow/Prefabs/Views/" + path; _DestroyExistViews(fullPath); } private static void _DestroyExistViews(string fullPath) { _ViewCacheInfos.FindAll(e => e.fullPath == fullPath).ForEach(e => { if (e.gameObject) Object.Destroy(e.gameObject); }); _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath); } public static void RemoveView(string path) { string fullPath = "SmartBow/Prefabs/Views/" + path; _RemoveView(fullPath); } //仅移除 _ViewCacheInfos private static void _RemoveView(string fullPath) { _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath); } public const string Path_SettingsView = "Home/SettingsView"; public const string Path_SocialView = "Home/SocialView"; public const string Path_PersonalView = "Home/PersonalView"; public const string Path_RankingView = "Home/RankingView"; public const string Path_ConnectGuidanceView = "Home/ConnectGuidanceView"; public const string Path_GyrGuidanceView = "Home/GyrGuidanceView"; public const string Path_MagGuidanceView = "Home/MagGuidanceView"; public const string Path_InfraredView = "Home/InfraredView"; public const string Path_InfraredScreenPositioningView = "Home/InfraredScreenPositioningView"; public const string Path_HomeViewTip = "Home/HomeView_Tip"; public const string Path_GameResultView = "GameResultView"; //Rank排行榜 public const string Path_GameRankView = "GameRankView"; }