AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
UICollection.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.UI.Keyboard
8 {
19  [AddComponentMenu("UI/HoloUIKit/UICollection")]
20  [RequireComponent(typeof(RectTransform))]
21  [ExecuteInEditMode]
22  public class UICollection : MonoBehaviour
23  {
28  public float MaxWidth = -1.0f;
29 
34  public float MaxHeight = -1.0f;
35 
39  public float HorizontalSpacing = 0.0f;
40 
44  public float VerticalSpacing = 0.0f;
45 
50  public List<RectTransform> Items { get; private set; }
51 
55  private RectTransform rectTransform;
56 
57  private void Awake()
58  {
59  Items = new List<RectTransform>();
60  }
61 
62  // Use this for initialization
63  private void Start()
64  {
65  // Verify this is attached to a GameObject with a rect transform
66  rectTransform = GetComponent<RectTransform>();
67 
68  if (!Application.isEditor) { return; }
69 
70  // Collect children items already added (likely added in the Editor)
71  CollectItems();
72  UpdateLayout();
73  }
74 
75  private void Update()
76  {
77  if (!Application.isEditor) { return; }
78  CollectItems();
79  UpdateLayout();
80  }
81 
88  public void AddItem(RectTransform item)
89  {
90  Items.Add(item);
91 
92  item.SetParent(transform);
93  item.transform.localScale = Vector3.one;
94  item.position = Vector3.zero;
95  item.anchoredPosition3D = Vector3.zero;
96 
97  UpdateLayout();
98  }
99 
106  public void RemoveItem(RectTransform item)
107  {
108  Items.Remove(item);
109 
110  UpdateLayout();
111  }
112 
118  public void RemoveAllItems()
119  {
120  Items.Clear();
121 
122  UpdateLayout();
123  }
124 
125  private void CollectItems()
126  {
127  Items.Clear();
128 
129  foreach (Transform childTransform in transform)
130  {
131  RectTransform childRect = childTransform.GetComponent<RectTransform>();
132  if (childRect != null)
133  {
134  AddItem(childRect);
135  }
136  }
137  }
138 
139  protected virtual void UpdateLayout()
140  {
141  Rect rect = rectTransform.rect;
142 
143  Vector2 updatedSize = Vector2.zero;
144  if (MaxWidth < 0.0f)
145  {
146  // Set to the width of the panel
147  updatedSize.x = rect.width;
148  }
149  else
150  {
151  // Set to the max width
152  updatedSize.x = MaxWidth;
153  }
154 
155  if (MaxHeight < 0.0f)
156  {
157  // Set to the height of the panel
158  updatedSize.y = rect.height;
159  }
160  else
161  {
162  // Set to the max height
163  updatedSize.y = MaxHeight;
164  }
165 
166  Vector2 currentOffset = Vector2.zero;
167  Vector2 anchorVec = Vector2.up;
168 
169  float columnHeight = 0.0f;
170  float maxPanelWidth = 0.0f;
171 
172  for (int i = 0; i < Items.Count; i++)
173  {
174 
175  // Ensure the anchors and pivot are set properly for positioning in the UICollection
176  Items[i].anchorMin = anchorVec;
177  Items[i].anchorMax = anchorVec;
178  Items[i].pivot = anchorVec;
179 
180  columnHeight = Mathf.Max(Items[i].rect.height, columnHeight);
181 
182  if (Items[i].rect.width + currentOffset.x > updatedSize.x)
183  {
184  // Move to next column
185  currentOffset.y += columnHeight + VerticalSpacing;
186  currentOffset.x = 0.0f;
187  columnHeight = Items[i].rect.height;
188 
189  // Check to see if it can fit in the next column
190  if (Items[i].rect.height + currentOffset.y > updatedSize.y)
191  {
192  // Bail out... can't fit any more items!!!
193  break;
194  }
195  }
196 
197  // Position item
198  Items[i].anchoredPosition = new Vector2(currentOffset.x, -currentOffset.y);
199 
200  // Update current offset
201  currentOffset.x += Items[i].rect.width + HorizontalSpacing;
202 
203  maxPanelWidth = Mathf.Max(currentOffset.x - HorizontalSpacing, maxPanelWidth);
204  }
205 
206  // Update the panel size
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);
210  }
211  }
212 }
void AddItem(RectTransform item)
Adds a UI element to the collection. This will cause the collection layout to update immediately...
Definition: UICollection.cs:88
void RemoveAllItems()
Removes all UI elements added to the collection. This will cause the collection layout to update imme...
This component represents and ordered collection of UI elements. You can add to the UICollection by e...
Definition: UICollection.cs:22
void RemoveItem(RectTransform item)
Removes a UI element from the collection. This will cause the collection layout to update immediately...