AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
LoadingAnimation.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;
6 
7 namespace HoloToolkit.Examples.UX
8 {
12  public class LoadingAnimation : MonoBehaviour
13  {
14  [SerializeField]
15  private GameObject[] orbs;
16  private bool startingLoader = false;
17  private int startingIndex;
18 
19  public Vector3 CenterPoint = new Vector3();
20  public Vector3 Axis = Vector3.forward;
21  public float Radius = 0.075f;
22  public float RevolutionSpeed = 1.9f;
23  public int Revolutions = 3;
24  public float AngleSpace = 12;
25  public bool IsPaused = false;
26  public bool SmoothEaseInOut = false;
27  public float SmoothRatio = 0.65f;
28 
29  private float angle = 0;
30  private float timeValue = 0;
31  private int revolutionsCount = 0;
32  private bool loopPause = false;
33  private int fadeIndex = 0;
34  private bool checkLoopPause = false;
35  private Vector3 positionVector;
36  private Vector3 rotatedPositionVector;
37  private LoadingAnimation loadingAnimation;
38 
39  public GameObject[] Orbs
40  {
41  get
42  {
43  return orbs;
44  }
45 
46  set
47  {
48  orbs = value;
49  }
50  }
51 
52  private void Start()
53  {
54  positionVector = transform.up;
55 
56  if (!Mathf.Approximately(Vector3.Angle(Axis, positionVector), 90))
57  {
58  positionVector = transform.forward;
59  if (!Mathf.Approximately(Vector3.Angle(Axis, positionVector), 90))
60  {
61  float x = Mathf.Abs(Axis.x);
62  float y = Mathf.Abs(Axis.y);
63  float z = Mathf.Abs(Axis.z);
64 
65  if (x > y && x > z)
66  {
67  // left or right - cross with the z axis
68  positionVector = Vector3.Cross(Axis * Radius, Vector3.forward);
69  }
70 
71  if (z > y && z > x)
72  {
73  // forward or backward - cross with the x axis
74  positionVector = Vector3.Cross(Axis * Radius, Vector3.right);
75  }
76 
77  if (y > z && y > x)
78  {
79  // up or down - cross with the x axis
80  positionVector = Vector3.Cross(Axis * Radius, Vector3.right);
81  }
82  }
83  }
84  }
85 
86  public void StartLoader()
87  {
88  loadingAnimation = gameObject.GetComponent<LoadingAnimation>();
89  for (int i = 0; i < Orbs.Length; ++i)
90  {
91  Orbs[i].SetActive(false);
92  FadeObject fade = Orbs[i].GetComponent<FadeObject>();
93  fade.ResetFade(0);
94  }
95 
96  startingLoader = true;
97  startingIndex = 0;
98  revolutionsCount = 0;
99  IsPaused = false;
100  }
101 
102  public void StopLoader()
103  {
104  for (int i = 0; i < Orbs.Length; ++i)
105  {
106  Orbs[i].gameObject.SetActive(false);
107  }
108  }
109 
110  public void StartOrbit()
111  {
112  IsPaused = false;
113  }
114 
115  public void ResetOrbit()
116  {
117  angle = 0;
118  }
119 
120  public float QuartEaseInOut(float s, float e, float v)
121  {
122  //e -= s;
123  if ((v /= 0.5f) < 1)
124  {
125  return e / 2 * v * v * v * v + s;
126  }
127 
128  return -e / 2 * ((v -= 2) * v * v * v - 2) + s;
129  }
130 
131  private void Update()
132  {
133  if (IsPaused)
134  {
135  return;
136  }
137 
138  float percentage = timeValue / RevolutionSpeed;
139 
140  for (int i = 0; i < Orbs.Length; ++i)
141  {
142  GameObject orb = Orbs[i];
143  float orbPercentage = percentage - AngleSpace / 360 * i;
144  if (orbPercentage < 0)
145  {
146  orbPercentage = 1 + orbPercentage;
147  }
148 
149  if (SmoothEaseInOut)
150  {
151  float linearSmoothing = 1 * (orbPercentage * (1 - SmoothRatio));
152  orbPercentage = QuartEaseInOut(0, 1, orbPercentage) * SmoothRatio + linearSmoothing;
153  }
154  angle = 0 - (orbPercentage) * 360;
155 
156  if (startingLoader)
157  {
158  if (orbPercentage >= 0 && orbPercentage < 0.5f)
159  {
160  if (i == startingIndex)
161  {
162  orb.SetActive(true);
163  if (i >= Orbs.Length - 1)
164  {
165  startingLoader = false;
166  }
167  startingIndex += 1;
168  }
169  }
170  }
171 
172  orb.transform.Rotate(Axis, angle);
173  rotatedPositionVector = Quaternion.AngleAxis(angle, Axis) * positionVector * Radius;
174  orb.transform.localPosition = CenterPoint + rotatedPositionVector;
175 
176  if (checkLoopPause != loopPause)
177  {
178  if (loopPause && orbPercentage > 0.25f)
179  {
180  if (i == fadeIndex)
181  {
182  FadeObject fade = orb.GetComponent<FadeObject>();
183  fade.FadeOut(false);
184  if (i >= Orbs.Length - 1)
185  {
186  checkLoopPause = loopPause;
187  }
188  fadeIndex += 1;
189  }
190 
191  }
192 
193  if (!loopPause && orbPercentage > 0.5f)
194  {
195  if (i == fadeIndex)
196  {
197  FadeObject fade = orb.GetComponent<FadeObject>();
198  fade.FadeIn(false);
199  if (i >= Orbs.Length - 1)
200  {
201  checkLoopPause = loopPause;
202  }
203  fadeIndex += 1;
204  }
205  }
206 
207  }
208  }
209 
210  timeValue += Time.deltaTime;
211  if (!loopPause)
212  {
213  if (timeValue >= RevolutionSpeed)
214  {
215  timeValue = timeValue - RevolutionSpeed;
216 
217  revolutionsCount += 1;
218 
219  if (revolutionsCount >= Revolutions && Revolutions > 0)
220  {
221  loopPause = true;
222  fadeIndex = 0;
223  revolutionsCount = 0;
224  loadingAnimation.gameObject.SetActive(false);
225  }
226  }
227  }
228  else
229  {
230  if (timeValue >= RevolutionSpeed)
231  {
232  timeValue = 0;
233  revolutionsCount += 1;
234  if (revolutionsCount >= Revolutions * 0.25f)
235  {
236  fadeIndex = 0;
237  loopPause = false;
238  revolutionsCount = 0;
239  }
240  }
241  }
242  }
243  }
244 }
245 
This class describes and performs a fade. It can be used to create a fadeIn or a fadeOut or both...
Definition: FadeObject.cs:13
void FadeOut(bool resetStartValue)
start the Fade out effect.
Definition: FadeObject.cs:122
void ResetFade(float value)
Resets the color to original before fade effect
Definition: FadeObject.cs:106
This class is used to setup and execute each of the loading animation effects of a progress indicator...
float QuartEaseInOut(float s, float e, float v)
void FadeIn(bool resetFade)
Begins the Fade in effect
Definition: FadeObject.cs:85