UnityEssentials
Small but useful tools and features for Unity
SimpleAnimationsManager.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 
4 namespace UnityEngine
5 {
9  public class SimpleAnimationsManager : MonoBehaviour
10  {
15  [SerializeReference] public List<ISimpleAnimation> animations;
20  public SimpleAnimation GetAnimation(int index)
21  {
22  if (animations.Count > index)
23  return (SimpleAnimation) animations[index];
24 
25  Debug.LogWarning($"Trying to get the animation with index '{index}' of the 'SimpleAnimationsManager' of the GameObject {gameObject.name} but the size of the array is {animations.Count}.", gameObject);
26  return null;
27  }
33  public SimpleAnimation GetAnimation(string animationName)
34  {
35  foreach (ISimpleAnimation IAnimation in animations)
36  {
37  SimpleAnimation animation = (SimpleAnimation)IAnimation;
38  if (string.Compare(animation.name, animationName, StringComparison.Ordinal) == 0)
39  return animation;
40  }
41  Debug.LogWarning($"Trying to find the animation with name '{animationName}' but it is not found in the 'SimpleAnimationsManager' of the GameObject {gameObject.name}", gameObject);
42  return null;
43  }
44 
48  private HashSet<SimpleAnimation> animationsToStop = new HashSet<SimpleAnimation>();
49 
53  private HashSet<SimpleAnimation> playingAnimations = new HashSet<SimpleAnimation>();
54 
60  public void Play(SimpleAnimation animation, bool resume = false)
61  {
62  playingAnimations.Add(animation);
63 
64  if (!resume)
65  animation.Reset();
66 
67  animationsToStop.Remove(animation);
68  }
69 
75  public void Play(string animationName, bool resume = false)
76  {
77  Play(GetAnimation(animationName), resume);
78  }
79 
85  public void Play(int index, bool resume = false)
86  {
87  Play(GetAnimation(index), resume);
88  }
89 
94  public void Stop(SimpleAnimation animation)
95  {
96  playingAnimations.Remove(animation);
97  animationsToStop.Remove(animation);
98  }
99 
104  public void Play(string animationName)
105  {
106  Play(GetAnimation(animationName));
107  }
108 
113  public void Stop(int index)
114  {
115  Stop(GetAnimation(index));
116  }
117 
122  public void Stop(string animationName)
123  {
124  Stop(GetAnimation(animationName));
125  }
126 
127  private void Update()
128  {
129  if (animationsToStop.Count > 0)
130  {
131  List<SimpleAnimation> tempAnimToStop = new List<SimpleAnimation>(animationsToStop);
132  foreach (SimpleAnimation animation in tempAnimToStop)
133  {
134  Stop(animation);
135  }
136  }
137 
138 
139  foreach (SimpleAnimation animation in playingAnimations)
140  {
141  if (animation.Step(Time.deltaTime))
142  {
143  animationsToStop.Add(animation);
144  }
145  }
146  }
147 
153  public void SetProgress(SimpleAnimation animation, float progress)
154  {
155  animation.SetProgress(progress);
156  }
157 
163  public void SetProgress(string animationName, float progress)
164  {
165  SetProgress(GetAnimation(animationName), progress);
166  }
167 
173  public void SetProgress(int index, float progress)
174  {
175  SetProgress(GetAnimation(index), progress);
176  }
177  }
178 
179 }
Base interface to create simple animations of any element.
Base class to create simple animations of any element.
void Reset()
Sets the time stamps of the animation to the beginning (the behaviour changes depending on if the ani...
string name
The name of the animation
virtual void SetProgress(float progress)
Sets the animation at the given progress.
virtual bool Step(float deltaTime, bool inverseIfMirror=true)
Go forward or backwards in the animation.
Component used to control the behaviour of any SimpleAnimation
SimpleAnimation GetAnimation(int index)
Obtain an animation configured trough the inspector and stored in the SimpleAnimationsManager's memor...
SimpleAnimation GetAnimation(string animationName)
Returns the simple animation stored in this SimpleAnimationsManager witht he same name as the given.
void Play(SimpleAnimation animation, bool resume=false)
Starts playing the animation.
void Play(int index, bool resume=false)
Starts playing the animation.
HashSet< SimpleAnimation > animationsToStop
List of all the animations that should stopped so they will be removed from the "playingAnimations" l...
void Play(string animationName)
Starts playing the animation.
void Stop(int index)
Stops playing the animation.
void SetProgress(int index, float progress)
Sets the animation at the given progress.
void SetProgress(SimpleAnimation animation, float progress)
Sets the animation at the given progress.
void Play(string animationName, bool resume=false)
Starts playing the animation.
void SetProgress(string animationName, float progress)
Sets the animation at the given progress.
HashSet< SimpleAnimation > playingAnimations
List of all the animations that are being played.
List< ISimpleAnimation > animations
List of all the animations meant to be configured trough the inspector and/or stored in the SimpleAni...
void Stop(string animationName)
Stops playing the animation.
void Stop(SimpleAnimation animation)
Stops playing the animation.