| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ViewManager2
- {
- private class ViewCacheInfo
- {
- public string fullPath;
- public GameObject gameObject;
- }
- private static List<ViewCacheInfo> _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)
- {
- //查找创造的ViewMgr 第一个层级作为ViewManager2的父节点
- if (currentViewParent == null)
- {
- currentViewParent = ViewMgr.Instance.transform.Find("0").gameObject;
- }
- string fullPath = "SmartBow/Prefabs/Views/" + path;
- _DestroyExistViews(fullPath);
- GameObject o = Object.Instantiate(Resources.Load<GameObject>(fullPath), currentViewParent.transform);
- // 根据条件是否设置原来的sorting
- if (path == Path_GameResultView)
- {
- o.GetComponent<Canvas>().overrideSorting = true;
- }
- // 检查并移除 Canvas Scaler 组件
- //UnityEngine.UI.CanvasScaler canvasScaler = o.GetComponent<UnityEngine.UI.CanvasScaler>();
- //if (canvasScaler != null)
- //{
- // Object.Destroy(canvasScaler);
- //}
- // 返回删除CanvasScaler对象
- // 设置CanvasScaler RectTransform 的属性
- RectTransform rectTransform = RemoveCanvasScaler(o.transform);
- //RectTransform rectTransform = o.GetComponent<RectTransform>();
- //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>())
- {
- 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<Canvas>();
- if (canvas != null)
- {
- // 检查并移除 Canvas Scaler 组件
- UnityEngine.UI.CanvasScaler canvasScaler = parent.GetComponent<UnityEngine.UI.CanvasScaler>();
- if (canvasScaler != null)
- {
- Object.Destroy(canvasScaler);
- return parent.GetComponent<RectTransform>(); // 返回删除 CanvasScaler 的 RectTransform
- }
- }
- else
- {
- // 如果没有 Canvas,轮询子物体
- foreach (Transform child in parent)
- {
- RectTransform result = RemoveCanvasScaler(child);
- if (result != null) // 如果找到了被删除的 RectTransform,则返回
- {
- return result;
- }
- }
- }
- return null; // 如果没有找到,则返回 null
- }
- 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";
- }
|