ViewManager2.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //查找创造的ViewMgr 第一个层级作为ViewManager2的父节点
  25. if (currentViewParent == null)
  26. {
  27. currentViewParent = ViewMgr.Instance.transform.Find("0").gameObject;
  28. }
  29. string fullPath = "SmartBow/Prefabs/Views/" + path;
  30. _DestroyExistViews(fullPath);
  31. GameObject o = Object.Instantiate(Resources.Load<GameObject>(fullPath), currentViewParent.transform);
  32. // 根据条件是否设置原来的sorting
  33. if (path == Path_GameResultView)
  34. {
  35. o.GetComponent<Canvas>().overrideSorting = true;
  36. }
  37. // 检查并移除 Canvas Scaler 组件
  38. //UnityEngine.UI.CanvasScaler canvasScaler = o.GetComponent<UnityEngine.UI.CanvasScaler>();
  39. //if (canvasScaler != null)
  40. //{
  41. // Object.Destroy(canvasScaler);
  42. //}
  43. // 返回删除CanvasScaler对象
  44. // 设置CanvasScaler RectTransform 的属性
  45. RectTransform rectTransform = RemoveCanvasScaler(o.transform);
  46. //RectTransform rectTransform = o.GetComponent<RectTransform>();
  47. //if (rectTransform != null)
  48. //{
  49. // rectTransform.localScale = Vector3.one; // 重置缩放
  50. // rectTransform.sizeDelta = ((RectTransform)currentViewParent.transform).rect.size; // 设置大小
  51. // rectTransform.anchoredPosition = Vector2.zero; // 重置位置
  52. //}
  53. // 设置 RectTransform 全屏适应父级
  54. if (rectTransform != null)
  55. {
  56. if (rectTransform == o.GetComponent<RectTransform>())
  57. {
  58. rectTransform.anchorMin = Vector2.zero; // 左下角对齐父级
  59. rectTransform.anchorMax = Vector2.one; // 右上角对齐父级
  60. rectTransform.offsetMin = Vector2.zero; // 移除左下角偏移
  61. rectTransform.offsetMax = Vector2.zero; // 移除右上角偏移
  62. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  63. }
  64. else {
  65. rectTransform.pivot = new Vector2(0.5f, 0.5f);
  66. rectTransform.localScale = Vector3.one; // 确保缩放为 1
  67. rectTransform.sizeDelta = ((RectTransform)currentViewParent.transform).rect.size; // 设置大小
  68. rectTransform.anchoredPosition = Vector2.zero; // 重置位置
  69. }
  70. }
  71. ViewCacheInfo viewCacheInfo = new ViewCacheInfo();
  72. viewCacheInfo.fullPath = fullPath;
  73. viewCacheInfo.gameObject = o;
  74. _ViewCacheInfos.Add(viewCacheInfo);
  75. return o;
  76. }
  77. static RectTransform RemoveCanvasScaler(Transform parent)
  78. {
  79. // 检查自身是否有 Canvas 组件
  80. Canvas canvas = parent.GetComponent<Canvas>();
  81. if (canvas != null)
  82. {
  83. // 检查并移除 Canvas Scaler 组件
  84. UnityEngine.UI.CanvasScaler canvasScaler = parent.GetComponent<UnityEngine.UI.CanvasScaler>();
  85. if (canvasScaler != null)
  86. {
  87. Object.Destroy(canvasScaler);
  88. return parent.GetComponent<RectTransform>(); // 返回删除 CanvasScaler 的 RectTransform
  89. }
  90. }
  91. else
  92. {
  93. // 如果没有 Canvas,轮询子物体
  94. foreach (Transform child in parent)
  95. {
  96. RectTransform result = RemoveCanvasScaler(child);
  97. if (result != null) // 如果找到了被删除的 RectTransform,则返回
  98. {
  99. return result;
  100. }
  101. }
  102. }
  103. return null; // 如果没有找到,则返回 null
  104. }
  105. public static void HideView(string path)
  106. {
  107. string fullPath = "SmartBow/Prefabs/Views/" + path;
  108. _DestroyExistViews(fullPath);
  109. }
  110. private static void _DestroyExistViews(string fullPath)
  111. {
  112. _ViewCacheInfos.FindAll(e => e.fullPath == fullPath).ForEach(e =>
  113. {
  114. if (e.gameObject) Object.Destroy(e.gameObject);
  115. });
  116. _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath);
  117. }
  118. public static void RemoveView(string path)
  119. {
  120. string fullPath = "SmartBow/Prefabs/Views/" + path;
  121. _RemoveView(fullPath);
  122. }
  123. //仅移除 _ViewCacheInfos
  124. private static void _RemoveView(string fullPath)
  125. {
  126. _ViewCacheInfos.RemoveAll(e => e.fullPath == fullPath);
  127. }
  128. public const string Path_SettingsView = "Home/SettingsView";
  129. public const string Path_SocialView = "Home/SocialView";
  130. public const string Path_PersonalView = "Home/PersonalView";
  131. public const string Path_RankingView = "Home/RankingView";
  132. public const string Path_ConnectGuidanceView = "Home/ConnectGuidanceView";
  133. public const string Path_GyrGuidanceView = "Home/GyrGuidanceView";
  134. public const string Path_MagGuidanceView = "Home/MagGuidanceView";
  135. public const string Path_InfraredView = "Home/InfraredView";
  136. public const string Path_InfraredScreenPositioningView = "Home/InfraredScreenPositioningView";
  137. public const string Path_HomeViewTip = "Home/HomeView_Tip";
  138. public const string Path_GameResultView = "GameResultView";
  139. }