ViewCanvas.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace WS
  6. {
  7. [ObjectSystem]
  8. public class ViewCanvasAwake : AwakeSystem<ViewCanvas, Transform, ViewShowModeType>
  9. {
  10. public override void Awake(ViewCanvas self, Transform a, ViewShowModeType b)
  11. {
  12. self.Awake(a, b);
  13. }
  14. }
  15. ///<summary>视图画布</summary>
  16. public class ViewCanvas : WSComponent
  17. {
  18. //根节点
  19. public Transform UIRoot;
  20. //画布的缩放
  21. private CanvasScaler UICanvasScaler;
  22. //Groups
  23. public Dictionary<ViewGroup, Transform> GroupDic = new Dictionary<ViewGroup, Transform>();
  24. //视图类型
  25. public ViewShowModeType ViewShowMode;
  26. public void Awake(Transform a, ViewShowModeType viewshow)
  27. {
  28. UIRoot = a;
  29. ViewShowMode = viewshow;
  30. #if PLATFORM_PICO
  31. if (ViewShowMode != ViewShowModeType.Screen)
  32. {
  33. int count = UIRoot.childCount;
  34. for (int i = 0; i < count; i++)
  35. {
  36. UnityEngine.Object.Destroy(UIRoot.GetChild(i).gameObject);
  37. }
  38. return;
  39. }
  40. #endif
  41. UICanvasScaler = UIRoot.GetComponent<CanvasScaler>();
  42. string[] groups = Enum.GetNames(typeof(ViewGroup));
  43. for (int i = 0; i < groups.Length; i++)
  44. {
  45. Transform group = UIRoot.Find(groups[i]);
  46. if (group != null)
  47. {
  48. ViewGroup form = (ViewGroup)Enum.Parse(typeof(ViewGroup), groups[i]);
  49. GroupDic[form] = group;
  50. }
  51. }
  52. if (ViewShowMode == ViewShowModeType.Screen)
  53. {
  54. AdaptationCanvasScaler();
  55. }
  56. }
  57. ///<summary>绘制</summary>
  58. public void ShowCanvas(View view)
  59. {
  60. if (view.ShowModeType != ViewShowModeType.Screen)
  61. {
  62. RectTransform rect = view.UIGameObject.GetComponent<RectTransform>();
  63. BoxCollider collider = view.UIGameObject.AddComponent<BoxCollider>();
  64. collider.size = new Vector3(rect.sizeDelta.x, rect.sizeDelta.y, 1);
  65. }
  66. #if PLATFORM_PICO
  67. if (view.ShowModeType != ViewShowModeType.Screen)
  68. {
  69. GameObject WorldCanvas = GamePoolManager.Instance.CreateObject(UIRoot.gameObject);
  70. view.UIGameObject.transform.SetParent(WorldCanvas.transform, false);
  71. view.UIGameObject.transform.localPosition = Vector3.zero;
  72. view.UIGameObject.transform.localScale = Vector3.one;
  73. view.UIGameObject.transform.SetAsLastSibling();
  74. return;
  75. }
  76. #endif
  77. Transform group = GetGroup(view.Group);
  78. if (group != null)
  79. {
  80. view.UIGameObject.transform.SetParent(group, false);
  81. view.UIGameObject.transform.localPosition = Vector3.zero;
  82. view.UIGameObject.transform.localScale = Vector3.one;
  83. if (ViewShowMode == ViewShowModeType.Screen)
  84. {
  85. RectTransform rect = view.UIGameObject.GetComponent<RectTransform>();
  86. rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
  87. rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
  88. rect.anchorMin = Vector2.zero;
  89. rect.anchorMax = Vector2.one;
  90. }
  91. view.UIGameObject.transform.SetAsLastSibling(); //移动到末尾
  92. }
  93. }
  94. ///<summary>适配分辨率</summary>
  95. public void AdaptationCanvasScaler()
  96. {
  97. ViewManager manager = (ViewManager)Parent;
  98. if (manager.CurrStandardScreen >= manager.StandardScreen)
  99. {
  100. UICanvasScaler.matchWidthOrHeight = 1;
  101. }
  102. else
  103. {
  104. UICanvasScaler.matchWidthOrHeight =
  105. manager.StandardScreen - manager.CurrStandardScreen;
  106. }
  107. }
  108. ///<summary>获取Group</summary>
  109. private Transform GetGroup(ViewGroup form)
  110. {
  111. Transform group = null;
  112. if (GroupDic.ContainsKey(form))
  113. {
  114. GroupDic.TryGetValue(form, out group);
  115. }
  116. else
  117. {
  118. Log.LogError($"ViewGroup:{form}物体不存在");
  119. }
  120. return group;
  121. }
  122. public override void Dispose()
  123. {
  124. base.Dispose();
  125. UIRoot = null;
  126. GroupDic.Clear();
  127. UICanvasScaler = null;
  128. }
  129. }
  130. }