AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CycleArray.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 UnityEngine;
5 using System.Collections;
6 using HoloToolkit.Unity;
7 
8 namespace HoloToolkit.Examples.Prototyping
9 {
15  public class CycleArray<Type> : MonoBehaviour, ICycle
16  {
20  [SerializeField]
21  protected Type[] Array;
22 
26  public GameObject TargetObject = null;
27 
31  public int DefaultIndex = 0;
32 
36  public int Index { get; set;}
37 
41  public Type Current
42  {
43  get
44  {
45  return Array[Index % Array.Length];
46  }
47  }
48 
49  private bool mHasInit = false;
50 
54  protected virtual void Awake()
55  {
56  if (TargetObject == null)
57  {
58  TargetObject = this.gameObject;
59  }
60  }
61 
65  protected virtual void Start()
66  {
67  if (!mHasInit)
68  {
69  SetIndex(DefaultIndex);
70  }
71  }
72 
78  public virtual void SetIndex(int index)
79  {
80  if (index > -1 && index <= GetLastIndex())
81  {
82  Index = index;
83  }
84  else
85  {
86  Debug.LogError("index out of bounds!");
87  }
88 
89  mHasInit = true;
90 
91  // do something with the updated index and apply it to the object
92  // Sample: TargetObject.transform.localScale = ScaleArray[CurrentIndex];
93  }
94 
98  public virtual void MoveNext()
99  {
100  if (Index < GetLastIndex())
101  {
102  SetIndex(Index + 1);
103  }
104  else
105  {
106  SetIndex(0);
107  }
108  }
109 
113  public virtual void MovePrevious()
114  {
115  if (Index > 0)
116  {
117  SetIndex(Index - 1);
118  }
119  else
120  {
121  SetIndex(GetLastIndex());
122  }
123  }
124 
129  public virtual int GetLastIndex()
130  {
131  // return the last index of the new array added above.
132  return Array.Length - 1;
133  }
134  }
135 }
CycleArray is a class for incrementing through component properties sequentially or assigning specifi...
Definition: CycleArray.cs:15
virtual void MovePrevious()
Move to the previous item in the array
Definition: CycleArray.cs:113
virtual void Start()
set the default values
Definition: CycleArray.cs:65
virtual void MoveNext()
Move to the next item in the array
Definition: CycleArray.cs:98
An interface for components that cycle through an array of properties. A component can be built to cy...
Definition: ICycle.cs:13
virtual int GetLastIndex()
Returns the last index in the array
Definition: CycleArray.cs:129
virtual void SetIndex(int index)
Assign a specific element from the array. Place your custom logic to assign an element to TargetObjec...
Definition: CycleArray.cs:78
virtual void Awake()
set the default TargetObject
Definition: CycleArray.cs:54
Type [] Array
Placeholder for the array of objects to cycle through
Definition: CycleArray.cs:21