Animate.cs is a simple script to facilitate simple animations to the properties for any Unity Component. Just add Animate as a component to the GameObject and setup the property/properties to be animated. Invoke it by its animation state index or call them all together.
Animations in the GIF:
-
Camera: Zoom out.
-
Button: scale up (ease out), color, scale down (ease in), text color, and gameObject deactivated on animation end.
-
Cube: position up (ease out), position down (ease in) loop, rotation.
-
Title: text anchorMax resize, icon color alpha
The animation state needs:
-
A state name (optional).
-
The exact Component type name (required): It must be the exact name of the component to animate its property.
-
The exact property name of the component to be animated.
-
Duration in seconds of the animation.
-
The interpolation curve of the animation, default: linear interpolation.
-
The parameter type of the property to animate.
-
Value of the parameter type to animate.
-
An (optional) Event that is invoked once the animation has ended. (It may invoke another animation state to create loops).
-
Linked GameObject (optional), the animation can be linked to any GameObject.
To start all the animation states together:
GetComponent<Animate>().RunAnimations();
If you prefer to run animation state separated (example for the first state):
int stateIndex = 0;
GetComponent<Animate>().AnimateState(stateIndex);
The script can not run nested properties. Ex: Trying to animate the color of the material in a MeshRenderer.
GetComponent<MeshRenderer>().material.color
-
Vector2
-
Vector3
-
Float
-
Integer
-
Color
This component is not recommended for heavy use of animations as it use System.Reflection inside while loops that decrease performance in comparison with dedicated animation scripts.
The easy use of the component facilitate to create multiple types of animations for UI interactions and very simple 3d animations.
Building an interactive UI Button with PointerEnter and PointerExit event listeners
1. The hierarchy:
Button with a text and an icon
2. The animation states
In this example, these are the animated properties for each described gameObject Component.
-
Button RectTransform: offsetMin/offsetMax
-
Button UnityEngine.UI.Image: color
-
Button/TextContainer RectTransform: anchorMax
-
Button/Icon CanvasGroup: alpha
3. Advanced property to reset animations
Enabling "Advanced" allows to modify the reset animations method and duration
4. AnimateUIListener Component:
Attaching AnimateUIListener to the button allows to interact with PointerEnter/PointerExit events, running the comma separated state indexes once the event occurs. If "Reset States" is enabled then the event will reset the animations when the event is triggered.
Thanks to @Deadcows for his ConditionalFieldAttribute in MyBox