AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ObjectCollectionDynamic.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;
5 using System.Collections.Generic;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity.Collections
9 {
15  public class ObjectCollectionDynamic : MonoBehaviour
16  {
17  public enum BehaviorEnum
18  {
19  StoreEveryTime,
20  StoreOnceOnStartup,
21  StoreManually,
22  }
23 
27  [Serializable]
29  {
31  {
32  transform = node.transform;
33  Name = node.Name;
34  Offset = node.Offset;
35  Radius = node.Radius;
36 
37  localPositionOnStartup = transform.localPosition;
38  localEulerAnglesOnStartup = transform.localEulerAngles;
39  }
40 
41  public CollectionNodeDynamic (Transform nodeTransform)
42  {
43  transform = nodeTransform;
44  Name = nodeTransform.name;
45  Offset = Vector2.zero;
46  Radius = 0f;
47 
48  localPositionOnStartup = transform.localPosition;
49  localEulerAnglesOnStartup = transform.localEulerAngles;
50  }
51 
52  public Vector3 localPositionOnStartup;
53  public Vector3 localEulerAnglesOnStartup;
54  }
55 
59  public BehaviorEnum Behavior = BehaviorEnum.StoreEveryTime;
60 
64  public List<CollectionNodeDynamic> DynamicNodeList;
65 
66  [SerializeField]
67  private ObjectCollection collection;
68 
74  public virtual void RestoreArrangement()
75  {
76  if (DynamicNodeList == null)
77  return;
78 
79  for (int i = DynamicNodeList.Count - 1; i >= 0; i--)
80  {
81  if (DynamicNodeList[i].transform == null || DynamicNodeList[i].transform.parent != collection.transform)
82  {
83  DynamicNodeList.RemoveAt(i);
84  } else
85  {
86  DynamicNodeList[i].transform.localPosition = DynamicNodeList[i].localPositionOnStartup;
87  DynamicNodeList[i].transform.localEulerAngles = DynamicNodeList[i].localEulerAnglesOnStartup;
88  }
89  }
90  }
91 
95  public void StoreArrangement ()
96  {
97  if (collection == null)
98  return;
99 
100  DynamicNodeList = new List<CollectionNodeDynamic>();
101  if (collection.NodeList == null || collection.NodeList.Count != transform.childCount)
102  {
103  foreach (Transform child in transform)
104  {
105  DynamicNodeList.Add(new CollectionNodeDynamic(child));
106  }
107  }
108  else
109  {
110  foreach (CollectionNode node in collection.NodeList)
111  {
112  DynamicNodeList.Add(new CollectionNodeDynamic(node));
113  }
114  }
115  }
116 
117  void Awake()
118  {
119  if (collection == null)
120  {
121  collection = gameObject.GetComponent<ObjectCollection>();
122  }
123  }
124 
125  void Start()
126  {
127  switch (Behavior)
128  {
129  case BehaviorEnum.StoreManually:
130  default:
131  // Don't do anything
132  break;
133 
134  case BehaviorEnum.StoreEveryTime:
135  // Subscribe to the collection's update events
136  collection.OnCollectionUpdated += OnCollectionUpdated;
137  break;
138 
139  case BehaviorEnum.StoreOnceOnStartup:
140  // If we're only supposed to update once
141  // Gather our list of nodes now
142  StoreArrangement();
143  break;
144  }
145  }
146 
147  void OnCollectionUpdated (ObjectCollection collection)
148  {
149  StoreArrangement();
150  }
151  }
152 }
List< CollectionNodeDynamic > DynamicNodeList
List of dynamic nodes in the collection
List< CollectionNode > NodeList
List of objects with generated data on the object.
Action< ObjectCollection > OnCollectionUpdated
Action called when collection is updated
A utility that stores transform information for objects in a collection This info can then be used fo...
An Object Collection is simply a set of child objects organized with some layout parameters. The object collection can be used to quickly create control panels or sets of prefab/objects.
Collection node is a data storage class for individual data about an object in the collection...
void StoreArrangement()
Manually store collection arrangement
virtual void RestoreArrangement()
Sets each node in the collection to its last stored arrangement Automatically prunes destroyed or rem...
Extends collection node class to include stored local position / rotation data