AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ScaleToValue.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.Collections;
5 using System.Collections.Generic;
6 using UnityEngine;
7 using UnityEngine.Events;
8 
9 namespace HoloToolkit.Examples.Prototyping
10 {
14  public class ScaleToValue : MonoBehaviour
15  {
24  public enum LerpTypes { Linear, EaseIn, EaseOut, EaseInOut, Free }
25 
26  [Tooltip("The object to scale")]
27  public GameObject TargetObject;
28 
29  [Tooltip("The scale value to animate to")]
30  public Vector3 TargetValue;
31 
32  [Tooltip("The ease type")]
34 
35  [Tooltip("The duration of the scale animation in seconds")]
36  public float LerpTime = 1f;
37 
38  [Tooltip("Auto start? or status")]
39  public bool IsRunning = false;
40 
41  [Tooltip("Animation complete!")]
42  public UnityEvent OnComplete;
43 
44  // animation ticker
45  private float mLerpTimeCounter;
46 
47  // cached start scale
48  private Vector3 mStartValue;
49 
54  private void Awake()
55  {
56  if (TargetObject == null)
57  {
58  TargetObject = this.gameObject;
59  }
60  mStartValue = GetScale();
61  }
62 
66  public void StartRunning()
67  {
68  if (TargetObject == null)
69  {
70  TargetObject = this.gameObject;
71  }
72 
73  mStartValue = GetScale();
74  mLerpTimeCounter = 0;
75  IsRunning = true;
76  }
77 
81  public void ResetTransform()
82  {
83  this.transform.localScale = mStartValue;
84  IsRunning = false;
85  mLerpTimeCounter = 0;
86  }
87 
91  public void Reverse()
92  {
93  TargetValue = mStartValue;
94  mStartValue = TargetValue;
95  mLerpTimeCounter = 0;
96  IsRunning = true;
97  }
98 
102  public void StopRunning()
103  {
104  IsRunning = false;
105  }
106 
111  private Vector3 GetScale()
112  {
113  return TargetObject.transform.localScale;
114  }
115 
122  private Vector3 GetNewScale(Vector3 currentScale, float percent)
123  {
124  Vector3 newScale = Vector3.one;
125  switch (LerpType)
126  {
127  case LerpTypes.Linear:
128  newScale = Vector3.Lerp(mStartValue, TargetValue, percent);
129  break;
130  case LerpTypes.EaseIn:
131  newScale = Vector3.Lerp(mStartValue, TargetValue, QuadEaseIn(0, 1, percent));
132  break;
133  case LerpTypes.EaseOut:
134  newScale = Vector3.Lerp(mStartValue, TargetValue, QuadEaseOut(0, 1, percent));
135  break;
136  case LerpTypes.EaseInOut:
137  newScale = Vector3.Lerp(mStartValue, TargetValue, QuadEaseInOut(0, 1, percent));
138  break;
139  case LerpTypes.Free:
140  newScale = Vector3.Lerp(currentScale, TargetValue, percent);
141  break;
142  default:
143  break;
144  }
145 
146  return newScale;
147  }
148 
149  // ease curves
150  public static float QuadEaseIn(float s, float e, float v)
151  {
152  return e * (v /= 1) * v + s;
153  }
154 
155  public static float QuadEaseOut(float s, float e, float v)
156  {
157  return -e * (v /= 1) * (v - 2) + s;
158  }
159 
160  public static float QuadEaseInOut(float s, float e, float v)
161  {
162  if ((v /= 0.5f) < 1)
163  return e / 2 * v * v + s;
164 
165  return -e / 2 * ((--v) * (v - 2) - 1) + s;
166  }
167 
171  private void Update()
172  {
173  if (IsRunning && LerpType != LerpTypes.Free)
174  {
175  // get the time
176  mLerpTimeCounter += Time.deltaTime;
177  float percent = mLerpTimeCounter / LerpTime;
178 
179  // set the scale
180  this.transform.localScale = GetNewScale(this.transform.localScale, percent);
181 
182  // fire the event if complete
183  if (percent >= 1)
184  {
185  IsRunning = false;
186  OnComplete.Invoke();
187  }
188  }
189  else if (LerpType == LerpTypes.Free)
190  {
191  bool wasRunning = IsRunning;
192 
193  // set the scale
194  this.transform.localScale = GetNewScale(this.transform.localScale, LerpTime * Time.deltaTime);
195  IsRunning = this.transform.localScale != TargetValue;
196 
197  // fire the event if complete
198  if (IsRunning != wasRunning && !IsRunning)
199  {
200  OnComplete.Invoke();
201  }
202  }
203  }
204  }
205 }
void StartRunning()
Start the animation
Definition: ScaleToValue.cs:66
Animates the scaling of an object with eases
Definition: ScaleToValue.cs:14
void Reverse()
Reverse the scale animation for a ping pong effect
Definition: ScaleToValue.cs:91
static float QuadEaseOut(float s, float e, float v)
static float QuadEaseInOut(float s, float e, float v)
void ResetTransform()
reset the scale value to the cached starting value
Definition: ScaleToValue.cs:81
static float QuadEaseIn(float s, float e, float v)
LerpTypes
ease types Linear: steady progress EaseIn: ramp up in speed EaseOut: ramp down in speed EaseInOut: ra...
Definition: ScaleToValue.cs:24