ViewManager2.cs 8.4 KB

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