4 using System.Collections.Generic;
19 [AddComponentMenu(
"UI/HoloUIKit/UICollection")]
20 [RequireComponent(typeof(RectTransform))]
28 public float MaxWidth = -1.0f;
34 public float MaxHeight = -1.0f;
39 public float HorizontalSpacing = 0.0f;
44 public float VerticalSpacing = 0.0f;
50 public List<RectTransform> Items {
get;
private set; }
55 private RectTransform rectTransform;
59 Items =
new List<RectTransform>();
66 rectTransform = GetComponent<RectTransform>();
68 if (!Application.isEditor) {
return; }
77 if (!Application.isEditor) {
return; }
92 item.SetParent(transform);
93 item.transform.localScale = Vector3.one;
94 item.position = Vector3.zero;
95 item.anchoredPosition3D = Vector3.zero;
125 private void CollectItems()
129 foreach (Transform childTransform
in transform)
131 RectTransform childRect = childTransform.GetComponent<RectTransform>();
132 if (childRect != null)
141 Rect rect = rectTransform.rect;
143 Vector2 updatedSize = Vector2.zero;
147 updatedSize.x = rect.width;
152 updatedSize.x = MaxWidth;
155 if (MaxHeight < 0.0f)
158 updatedSize.y = rect.height;
163 updatedSize.y = MaxHeight;
166 Vector2 currentOffset = Vector2.zero;
167 Vector2 anchorVec = Vector2.up;
169 float columnHeight = 0.0f;
170 float maxPanelWidth = 0.0f;
172 for (
int i = 0; i < Items.Count; i++)
176 Items[i].anchorMin = anchorVec;
177 Items[i].anchorMax = anchorVec;
178 Items[i].pivot = anchorVec;
180 columnHeight = Mathf.Max(Items[i].rect.height, columnHeight);
182 if (Items[i].rect.width + currentOffset.x > updatedSize.x)
185 currentOffset.y += columnHeight + VerticalSpacing;
186 currentOffset.x = 0.0f;
187 columnHeight = Items[i].rect.height;
190 if (Items[i].rect.height + currentOffset.y > updatedSize.y)
198 Items[i].anchoredPosition =
new Vector2(currentOffset.x, -currentOffset.y);
201 currentOffset.x += Items[i].rect.width + HorizontalSpacing;
203 maxPanelWidth = Mathf.Max(currentOffset.x - HorizontalSpacing, maxPanelWidth);
207 float finalWidth = MaxWidth < 0.0f ? rect.width : maxPanelWidth;
208 float finalHeight = MaxHeight < 0.0f ? rect.height : columnHeight + currentOffset.y;
209 rectTransform.sizeDelta =
new Vector2(finalWidth, finalHeight);