UnityEssentials
Small but useful tools and features for Unity
SimpleAnimation.cs
Go to the documentation of this file.
1 using System;
2 using UnityEngine.Events;
3 
4 namespace UnityEngine
5 {
11  [Serializable]
12  public abstract class SimpleAnimation : ISimpleAnimation
13  {
17  [Tooltip("The name of the animation")]
18  [SerializeField] public string name;
22  public float timeStamp { get => _timeStamp; protected set => _timeStamp = value; }
23  [HideInInspector] private float _timeStamp;
27  public float progress
28  {
29  get {
30  if (!mirror)
31  return timeStamp/ duration;
32  return 1 - (timeStamp/ duration);
33  }
34  set => SetProgress(value);
35  }
39  [Tooltip("Determines how the animation behaves once finished. Should be executed only once? Start over? Go back?")]
40  [SerializeField] public WrapMode wrapMode;
44  [Tooltip("Should the animation be played from end to start instead of from start to end?")]
45  [SerializeField] public bool mirror;
49  [Tooltip("How much should the animation last?")]
50  [SerializeField] public float duration;
54  [Tooltip("The curve of the animation over time. The bottom is the start state and the top is end state.")]
55  [SerializeField] public AnimationCurve curve;
59  public float currentAnimationCurveValue => curve.Evaluate(timeStamp / duration);
63  [Tooltip("Events executed every time the animation advances")]
64  [SerializeField] public UnityEvent onStep;
68  [Tooltip("Events executed when the animation finishes")]
69  [SerializeField] public UnityEvent onFinish;
70 
71 
75  public enum WrapMode
76  {
77  Once,
78  Loop,
79  PingPong
80  }
81 
88  public virtual bool Step(float deltaTime, bool inverseIfMirror = true)
89  {
90  if (!mirror || !inverseIfMirror)
91  timeStamp += deltaTime;
92  else if (mirror)
93  timeStamp -= deltaTime;
94  else
95  Debug.LogError("Unexpected Step call for a SimpleAnimation.");
96 
97  onStep?.Invoke();
98 
99  if ( ((timeStamp >= duration) && !mirror) || ((timeStamp <= 0) && mirror) )
100  {
101  if (!mirror)
103  else
104  timeStamp = 0;
105 
106  onFinish?.Invoke();
107 
108  switch (wrapMode)
109  {
110 
111  case WrapMode.Once:
112  break;
113  case WrapMode.Loop:
114  SetProgress(0f);
115  break;
116  case WrapMode.PingPong:
117  mirror = !mirror;
118  break;
119  default:
120  throw new ArgumentOutOfRangeException();
121  }
122 
123  return ((timeStamp >= duration) && !mirror) || ((timeStamp <= 0) && mirror); // Double evaluation to avoid bugs with modifications on the event invoked.
124  }
125 
126  return false;
127  }
128 
132  public void Reset()
133  {
134  timeStamp = !mirror? 0f : duration;
135  }
136 
137 
138  public override string ToString()
139  {
140  return "Current time: " + timeStamp + "/" + duration + ". Mirror: " + mirror;
141  }
142 
143 
149  public static AnimationCurve GetCurve(Curve curve)
150  {
151  switch (curve)
152  {
153  case Curve.Linear:
154  return AnimationCurve.Linear(0, 0, 1, 1);
155  case Curve.EaseInOut:
156  return AnimationCurve.EaseInOut(0, 0, 1, 1);
157  default:
158  throw new ArgumentOutOfRangeException(nameof(curve), curve, null);
159  }
160  }
161 
166  public virtual void SetProgress(float progress)
167  {
168  if (mirror) progress = 1 - progress;
169  float desiredTime = progress * duration;
170  Step(desiredTime - timeStamp, !mirror);
171  }
172 
176  public abstract UnityEngine.Object GetAnimatedObject(bool displayWarningIfNotApplicable = true);
177 
178  }
179 
183  public enum Curve
184  {
185  Linear,
186  EaseInOut
187  }
188 
189 }
Curve
Predefined curves for the SimpleAnimations
Base interface to create simple animations of any element.
Base class to create simple animations of any element.
WrapMode
How the animation behaves once finished. Should be executed only once? Start over?...
float timeStamp
The current moment/time of the animation. From 0 to duration.
float duration
How much should the animation last?
float progress
The progress of the animation. From 0 to 1.
AnimationCurve curve
The curve of the animation over time.
UnityEvent onFinish
Events executed when the animation finishes.
void Reset()
Sets the time stamps of the animation to the beginning (the behaviour changes depending on if the ani...
float currentAnimationCurveValue
The value of the animation at the current time. 0 means start state. 1 means end state.
UnityEvent onStep
Events executed every time the animation advances.
WrapMode wrapMode
Determines how the animation behaves once finished. Should be executed only once? Start over?...
string name
The name of the animation
virtual void SetProgress(float progress)
Sets the animation at the given progress.
static AnimationCurve GetCurve(Curve curve)
Obtains the desired type of animation curve with a duration of 1 (starting on 0), the start value bei...
abstract UnityEngine.Object GetAnimatedObject(bool displayWarningIfNotApplicable=true)
Returns the UnityEngine.Object animated. If not applicable, return null.
bool mirror
Should the animation be played from end to start instead of from start to end?
virtual bool Step(float deltaTime, bool inverseIfMirror=true)
Go forward or backwards in the animation.