AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ProgressIndicatorOrbsRotator.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 
6 
7 namespace HoloToolkit.UX.Progress
8 {
13  public class ProgressIndicatorOrbsRotator : MonoBehaviour
14  {
15  [SerializeField]
16  public GameObject[] orbs;
17 
18  [SerializeField]
19  private Material orbMaterial = null;
20 
22  public float SpacingDegrees;
23  public float Acceleration;
24  public int Revolutions;
25  public bool TestStop = false;
26  public bool HasAnimationFinished = false;
27 
28  private float timeElapsed;
29  private int deployedCount;
30  private bool timeUpdated;
31  private float[] angles;
32  private float timeSlice;
33  private float deg2rad = Mathf.PI / 180.0f;
34  private GameObject[] dots;
35  private bool stopRequested;
36  private float rotationWhenStopped;
37  private Material[] materials;
38 
39  private void Start()
40  {
41  rotationWhenStopped = 0.0f;
42  stopRequested = false;
43  timeSlice = 0;
44  timeUpdated = false;
45  deployedCount = 1;
46  timeElapsed = 0.0f;
47  angles = new float[orbs.Length];
48  for (int i = 0; i < angles.Length; ++i)
49  {
50  angles[i] = 0;
51  }
52 
53  dots = new GameObject[5];
54  materials = new Material[dots.Length];
55 
56  for (int i = 0; i < orbs.Length; ++i)
57  {
58  materials[i] = (Material)Instantiate(orbMaterial);
59  materials[i].color = new Color(1, 1, 1, 1);
60  dots[i] = orbs[i].transform.GetChild(0).gameObject;
61  materials[i] = dots[i].GetComponent<Renderer>().sharedMaterial = materials[i];
62  }
63  }
64 
65  public void Stop()
66  {
67  stopRequested = true;
68  rotationWhenStopped = angles[0];
69  }
70 
71  private void Update()
72  {
73  if (HasAnimationFinished == false)
74  {
75  UpdateTime();
76  ControlDotStarts();
77  IncrementOrbs();
78  HandleTestStop();
79  HandleStopping();
80  }
81  }
82 
83 
84  private void UpdateTime()
85  {
86  if (timeUpdated == false)
87  {
88  timeSlice = 0.0f;
89  timeElapsed = 0.0f;
90  timeUpdated = true;
91  }
92  else
93  {
94  timeSlice = Time.unscaledDeltaTime;
95  timeElapsed += timeSlice;
96  }
97  }
98 
99  private void ControlDotStarts()
100  {
101  if (deployedCount < orbs.Length)
102  {
103  if (angles[deployedCount - 1] >= SpacingDegrees)
104  {
105  deployedCount++;
106  }
107  }
108  }
109 
110  private void IncrementOrbs()
111  {
112  for (int i = 0; i < deployedCount; ++i)
113  {
114  IncrementOrb(i);
115  }
116  }
117 
118  private void IncrementOrb(int index)
119  {
120  float acceleratedDegrees = (RotationSpeedRawDegrees * (Acceleration + -Mathf.Cos(deg2rad * angles[index]))) * timeSlice;
121  orbs[index].gameObject.transform.Rotate(0, 0, acceleratedDegrees);
122  angles[index] += Mathf.Abs(acceleratedDegrees);
123 
124  HandleFade(index);
125  }
126 
127  private void HandleFade(int index)
128  {
129  Color adjustedColor = materials[index].color;
130 
131  //fade in
132  if (stopRequested == false && adjustedColor.a < 1.0f)
133  {
134  adjustedColor.a += (1.0f * timeSlice);
135  adjustedColor.a = Mathf.Min(1.0f, adjustedColor.a);
136  materials[index].color = adjustedColor;
137  }
138  //fade out
139  else if (stopRequested && angles[index] > rotationWhenStopped)
140  {
141  adjustedColor.a -= (1.0f * timeSlice);
142  adjustedColor.a = Mathf.Max(0.0f, adjustedColor.a);
143  materials[index].color = adjustedColor;
144  }
145  }
146 
147  private void HandleTestStop()
148  {
149  if (TestStop == true && stopRequested == false)
150  {
151  Stop();
152  }
153  }
154 
155  private void HandleStopping()
156  {
157  if (stopRequested == true && materials[orbs.Length - 1].color.a <= 0.01f)
158  {
159  HasAnimationFinished = true;
160  }
161  }
162  }
163 }
This class manages the &#39;rotating circle of dots&#39; effect that is used as a Progress Indicator effect...