| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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)
- {
- 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<GameObject>(fullPath), currentViewParent.transform);
- // 根据条件是否设置原来的sorting
- if (path == Path_GameResultView || path == Path_GameRankView)
- {
- 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
- }
- /// <summary>
- /// 测试使用
- /// </summary>
- /// <param name="mParent"></param>
- /// <returns></returns>
- public static GameObject ShowTestInfraredScreenPositioningView(Transform mParent)
- {
- string fullPath = "SmartBow/Prefabs/Views/" + Path_InfraredScreenPositioningView;
- GameObject o = Object.Instantiate(Resources.Load<GameObject>(fullPath), mParent);
- Transform children = o.transform.Find("ScreenPositioningView");
- UnityEngine.UI.CanvasScaler canvasScaler = children.GetComponent<UnityEngine.UI.CanvasScaler>();
- if (canvasScaler != null)
- {
- Object.Destroy(canvasScaler);
- }
- RectTransform rectTransform = children.GetComponent<RectTransform>();
- 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";
- }
|