AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
InteractiveGroup.cs
Go to the documentation of this file.
1 // Licensed under the MIT License. See LICENSE in the project root for license information.
2 
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Examples.InteractiveElements
9 {
10  [RequireComponent(typeof(InteractiveSet))]
11  public class InteractiveGroup : MonoBehaviour
12  {
13  [Tooltip("Gameobject containing GridLayoutGroup")]
14  public GameObject Grid;
15 
16  [Tooltip("Prefab for your interactive.")]
17  public GameObject InteractivePrefab;
18 
19  [Tooltip("scale for new instance of InteractivePrefab")]
20  public Vector3 PrefabScale = new Vector3(2000, 2000, 2000);
21 
22  [Tooltip("Data to fill the InteractiveSet.")]
23  public List<string> Titles = new List<string>();
24 
25  private void Start()
26  {
27  // note that simplifying this with the ??-operator does not work in Unity.
28  Grid = Grid == null ? transform.Find("Canvas/Grid").gameObject : Grid;
29 
30  Interactive interactive = InteractivePrefab.GetComponent<Interactive>();
31  if (interactive == null)
32  {
33  Debug.LogError("No interactive attached to Prefab, " +
34  "please attach one of the Interactive scripts " +
35  "to your InteractiveGroup Prefab!");
36  }
37  else
38  {
39  UpdateData();
40  }
41  }
42 
43  private List<InteractiveToggle> Interactives {
44  get
45  {
46  return GetInteractiveSet().Interactives;
47  }
48  set
49  {
50  GetInteractiveSet().Interactives = value;
51  }
52  }
53 
55  {
56  return GetComponent<InteractiveSet>();
57  }
58 
62  private void CreateInteractives()
63  {
64  for (int i = Interactives.Count; i < Titles.Count; i++)
65  {
66  GameObject PrefabInst = Instantiate(InteractivePrefab, Grid.transform) as GameObject;
67  PrefabInst.transform.localScale = PrefabScale;
68  InteractiveToggle InterInst = PrefabInst.GetComponent<InteractiveToggle>();
69  if (InterInst == null)
70  {
71  Debug.LogWarning("Please add an InteractiveToggle for your prefab " +
72  gameObject.name + " to use it in an InteractiveGroup.");
73  }
74  else
75  {
76  Interactives.Add(InterInst);
77  }
78  }
79  }
80 
84  public void UpdateData()
85  {
86  RemoveInteractives(Titles.Count);
87  CreateInteractives();
88 
89  for (int i = 0; i < Interactives.Count; i++)
90  {
91  // Set title
92  string title = Titles[i];
93  Interactive interactive = Interactives[i];
94  interactive.SetTitle(title);
95  interactive.Keyword = title;
96  }
97  GetInteractiveSet().SelectedIndices.Clear();
98  GetInteractiveSet().UpdateInteractives();
99  }
100 
101  private void OnDestroy()
102  {
103  RemoveInteractives();
104  }
105 
106 
111  private void RemoveInteractives(int keep = 0)
112  {
113  for (int i = Interactives.Count - 1; i >= keep; i--)
114  {
115  Interactive interactive = Interactives[i];
116  GetInteractiveSet().RemoveInteractive(i);
117  DestroyImmediate(interactive.gameObject);
118  }
119  }
120  }
121 }
A controller for managing multiple radial or tab type buttons
InteractiveToggle expands Interactive to expose selection or toggle states.
Interactive exposes basic button type events to the Unity Editor and receives messages from the Gestu...
Definition: Interactive.cs:22
void UpdateData()
position interactives and set title text.
void SetTitle(string title)
shortcut to set title (assuming this Interactive has a LabelTheme and a TextMesh attached to it) ...
Definition: Interactive.cs:250