ViewManager2.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ViewManager2
  5. {
  6. private class ViewCacheInfo
  7. {
  8. public string fullPath;
  9. public GameObject gameObject;
  10. }
  11. private static List<ViewCacheInfo> _ViewCacheInfos = new();
  12. private static GameObject currentViewParent;
  13. public static void ShowView(string path)
  14. {
  15. InstantiateView(path);
  16. }
  17. public static GameObject getGameObjectAndShowView(string path)
  18. {
  19. GameObject o = InstantiateView(path);
  20. return o;
  21. }
  22. static GameObject InstantiateView(string path)
  23. {
  24. int groupIndex = 0;
  25. //根据条件设置group
  26. if (path == Path_InfraredScreenPositioningView)
  27. {
  28. groupIndex = 1;
  29. }
  30. //查找创造的ViewMgr 第一个层级作为ViewManager2的父节点
  31. currentViewParent = ViewMgr.Instance.transform.Find(groupIndex.ToString()).gameObject;
  32. string fullPath = "SmartBow/Prefabs/Views/" + path;
  33. _DestroyExistViews(fullPath);
  34. GameObject o = Object.Instantiate(Resources.Load<GameObject>(fullPath), currentViewParent.transform);
  35. // 根据条件是否设置原来的sorting
  36. if (path == Path_GameResultView || path == Path_GameRankView)
  37. {
  38. o.GetComponent<Canvas>().overrideSorting = true;
  39. }
  40. // 检查并移除 Canvas Scaler 组件
  41. //UnityEngine.UI.CanvasScaler canvasScaler = o.GetComponent<UnityEngine.UI.CanvasScaler>();
  42. //if (canvasScaler != null)
  43. //{
  44. // Object.Destroy(canvasScaler);
  45. //}
  46. // 返回删除CanvasScaler对象
  47. // 设置CanvasScaler RectTransform 的属性
  48. RectTransform rectTransform = RemoveCanvasScaler(o.transform);
  49. //RectTransform rectTransform = o.GetComponent<RectTransform>();
  50. //if (rectTransform != null)
  51. //{
  52. // rectTransform.localScale = Vector3.one; // 重置缩放
  53. // rectTransform.sizeDelta = ((RectTransform)currentViewParent.transform).rect.size; // 设置大小
  54. // rectTransform.anchoredPosition = Vector2.zero; // 重置位置
  55. //}
  56. // 设置 RectTransform 全屏适应父级
  57. if (rectTransform != null)
  58. {
  59. if (rectTransform == o.GetComponent<RectTransform>())
  60. {
  61. rectTransform.anchorMin = Vector2.zero; // 左下角对齐父级
  62. rectTransform.anchorMax = Vector2.one; // 右上角对齐父级
  63. rectTransform.offsetMin = Vector2.zero; // 移除左下角偏移
  64. rectTransform.offsetMax = Vector2.zero; // 移除右上角偏移
  65. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  66. }
  67. else {
  68. rectTransform.pivot = new Vector2(0.5f, 0.5f);
  69. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  70. rectTransform.sizeDelta = ((RectTransform)currentViewParent.transform).rect.size; // 设置大小
  71. rectTransform.anchoredPosition = Vector2.zero; // 重置位置
  72. }
  73. }
  74. ViewCacheInfo viewCacheInfo = new ViewCacheInfo();
  75. viewCacheInfo.fullPath = fullPath;
  76. viewCacheInfo.gameObject = o;
  77. _ViewCacheInfos.Add(viewCacheInfo);
  78. return o;
  79. }
  80. static RectTransform RemoveCanvasScaler(Transform parent)
  81. {
  82. // 检查自身是否有 Canvas 组件
  83. Canvas canvas = parent.GetComponent<Canvas>();
  84. if (canvas != null)
  85. {
  86. // 检查并移除 Canvas Scaler 组件
  87. UnityEngine.UI.CanvasScaler canvasScaler = parent.GetComponent<UnityEngine.UI.CanvasScaler>();
  88. if (canvasScaler != null)
  89. {
  90. Object.Destroy(canvasScaler);
  91. return parent.GetComponent<RectTransform>(); // 返回删除 CanvasScaler 的 RectTransform
  92. }
  93. }
  94. else
  95. {
  96. // 如果没有 Canvas,轮询子物体
  97. foreach (Transform child in parent)
  98. {
  99. RectTransform result = RemoveCanvasScaler(child);
  100. if (result != null) // 如果找到了被删除的 RectTransform,则返回
  101. {
  102. return result;
  103. }
  104. }
  105. }
  106. return null; // 如果没有找到,则返回 null
  107. }
  108. /// <summary>
  109. /// 测试使用
  110. /// </summary>
  111. /// <param name="mParent"></param>
  112. /// <returns></returns>
  113. public static GameObject ShowTestInfraredScreenPositioningView(Transform mParent)
  114. {
  115. string fullPath = "SmartBow/Prefabs/Views/" + Path_InfraredScreenPositioningView;
  116. GameObject o = Object.Instantiate(Resources.Load<GameObject>(fullPath), mParent);
  117. Transform children = o.transform.Find("ScreenPositioningView");
  118. UnityEngine.UI.CanvasScaler canvasScaler = children.GetComponent<UnityEngine.UI.CanvasScaler>();
  119. if (canvasScaler != null)
  120. {
  121. Object.Destroy(canvasScaler);
  122. }
  123. RectTransform rectTransform = children.GetComponent<RectTransform>();
  124. rectTransform.pivot = new Vector2(0.5f, 0.5f);
  125. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  126. rectTransform.sizeDelta = ((RectTransform)mParent.parent.transform).rect.size; // 设置大小
  127. rectTransform.anchoredPosition = Vector2.zero; // 重置位置
  128. ViewCacheInfo viewCacheInfo = new ViewCacheInfo();
  129. viewCacheInfo.fullPath = fullPath;
  130. viewCacheInfo.gameObject = o;
  131. _ViewCacheInfos.Add(viewCacheInfo);
  132. return o;
  133. }
  134. /**
  135. * 跳转场景时候,所有UI视图
  136. */
  137. public static void HideAllView()
  138. {
  139. foreach (var viewCacheInfo in _ViewCacheInfos)
  140. {
  141. if (viewCacheInfo.gameObject)
  142. {
  143. Object.Destroy(viewCacheInfo.gameObject);
  144. }
  145. }
  146. _ViewCacheInfos.Clear();
  147. }
  148. public static void HideView(string path)
  149. {
  150. string fullPath = "SmartBow/Prefabs/Views/" + path;
  151. _DestroyExistViews(fullPath);
  152. }
  153. private static void _DestroyExistViews(string fullPath)
  154. {
  155. _ViewCacheInfos.FindAll(e => e.fullPath == fullPath).ForEach(e =>
  156. {
  157. if (e.gameObject) Object.Destroy(e.gameObject);
  158. });
  159. _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath);
  160. }
  161. public static void RemoveView(string path)
  162. {
  163. string fullPath = "SmartBow/Prefabs/Views/" + path;
  164. _RemoveView(fullPath);
  165. }
  166. //仅移除 _ViewCacheInfos
  167. private static void _RemoveView(string fullPath)
  168. {
  169. _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath);
  170. }
  171. public const string Path_SettingsView = "Home/SettingsView";
  172. public const string Path_SocialView = "Home/SocialView";
  173. public const string Path_PersonalView = "Home/PersonalView";
  174. public const string Path_RankingView = "Home/RankingView";
  175. public const string Path_ConnectGuidanceView = "Home/ConnectGuidanceView";
  176. public const string Path_GyrGuidanceView = "Home/GyrGuidanceView";
  177. public const string Path_MagGuidanceView = "Home/MagGuidanceView";
  178. public const string Path_InfraredView = "Home/InfraredView";
  179. public const string Path_InfraredScreenPositioningView = "Home/InfraredScreenPositioningView";
  180. public const string Path_HomeViewTip = "Home/HomeView_Tip";
  181. public const string Path_GameResultView = "GameResultView";
  182. //Rank排行榜
  183. public const string Path_GameRankView = "GameRankView";
  184. }