DynamicUIGrid.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. [ExecuteInEditMode]
  6. public class DynamicUIGrid : MonoBehaviour
  7. {
  8. private int m_activeChildCount
  9. {
  10. get
  11. {
  12. int num = 0;
  13. for (int i = 0; i < base.transform.childCount; i++)
  14. {
  15. Transform child = base.transform.GetChild(i);
  16. if (child.gameObject.activeSelf)
  17. {
  18. num++;
  19. }
  20. }
  21. return num;
  22. }
  23. }
  24. private Vector3 sortAxis
  25. {
  26. get
  27. {
  28. return DynamicUIGrid.GetSortAxis(this.m_SortType);
  29. }
  30. }
  31. private void Update()
  32. {
  33. this.Reposition();
  34. }
  35. public void UpdateChildUI()
  36. {
  37. this.m_ChildWidgetPair.Clear();
  38. for (int i = 0; i < base.transform.childCount; i++)
  39. {
  40. Transform child = base.transform.GetChild(i);
  41. if (child.gameObject.activeSelf)
  42. {
  43. UIWidget component = child.GetComponent<UIWidget>();
  44. if (component)
  45. {
  46. this.m_ChildWidgetPair.Add(child, component);
  47. }
  48. }
  49. }
  50. }
  51. public void Reposition()
  52. {
  53. Vector3 a = Vector3.zero;
  54. for (int i = 0; i < this.m_ChildWidgetPair.Count; i++)
  55. {
  56. Transform transform = this.m_ChildWidgetPair.Keys.ElementAt(i);
  57. float num = 0f;
  58. float num2 = 0f;
  59. if (i != 0)
  60. {
  61. if (this.m_SortType == DynamicUIGrid.SortAxisType.Up || this.m_SortType == DynamicUIGrid.SortAxisType.Down)
  62. {
  63. num = (float)this.m_ChildWidgetPair[transform].height;
  64. num2 = this.m_VerticalSpace;
  65. }
  66. else
  67. {
  68. num = (float)this.m_ChildWidgetPair[transform].width;
  69. num2 = this.m_HorizontalSpace;
  70. }
  71. }
  72. transform.localPosition = a + this.sortAxis * (num + num2);
  73. a = transform.localPosition;
  74. }
  75. if (this.m_ChildInverse)
  76. {
  77. this.ChildPosInverse();
  78. }
  79. }
  80. private void ChildPosInverse()
  81. {
  82. List<Transform> list = this.m_ChildWidgetPair.Keys.ToList<Transform>();
  83. int count = list.Count;
  84. for (int i = 0; i < list.Count / 2; i++)
  85. {
  86. Transform transform = list[i];
  87. int num = count - (1 + i);
  88. if (num >= 0)
  89. {
  90. Transform transform2 = list[num];
  91. Vector3 localPosition = transform.localPosition;
  92. transform.localPosition = transform2.localPosition;
  93. transform2.localPosition = localPosition;
  94. }
  95. }
  96. }
  97. public static Vector3 GetSortAxis(DynamicUIGrid.SortAxisType sort_type)
  98. {
  99. switch (sort_type)
  100. {
  101. case DynamicUIGrid.SortAxisType.Up:
  102. return Vector3.up;
  103. case DynamicUIGrid.SortAxisType.Down:
  104. return Vector3.down;
  105. case DynamicUIGrid.SortAxisType.Left:
  106. return Vector3.left;
  107. default:
  108. return Vector3.right;
  109. }
  110. }
  111. [SerializeField]
  112. [Header("UIの配置方向")]
  113. private DynamicUIGrid.SortAxisType m_SortType;
  114. [SerializeField]
  115. [Header("配置時に空ける横のスペース")]
  116. private float m_HorizontalSpace;
  117. [SerializeField]
  118. [Header("配置時に空ける縦のスペース")]
  119. private float m_VerticalSpace;
  120. [SerializeField]
  121. [Header("子の位置を反転させるか")]
  122. private bool m_ChildInverse;
  123. private Dictionary<Transform, UIWidget> m_ChildWidgetPair = new Dictionary<Transform, UIWidget>();
  124. public enum SortAxisType
  125. {
  126. Up,
  127. Down,
  128. Left,
  129. Right
  130. }
  131. }