Actions make it simple to set up groups or sequences of object interpolations. For example: Changing a color from red to blue over a specified duration.
All Actions inherit from the 'ActionBase' class, which allows them to be paused, resumed, restarted, and updated with a given Delta Time. The static 'Action' class is used to interface with the system.
The primary class for object interpolation is the 'ActionProperty' which can be created using the following static functions in the 'Action' class.
Note: The entire Action System is in the 'ActionSystem' namespace.
//Sequence Version
public static ActionProperty<T> Property<T>(ActionSequence seq, Property<T> startVal, T endVal, double duration, Curve ease);
//Group Version
public static ActionProperty<T> Property<T>(ActionGroup grp, Property<T> startVal, T endVal, double duration, Curve ease);
seq: The 'ActionSequence' for the property to be part of. Sequences are updated in first-in first-out order. Sequences and groups can even be in a sequence.
grp: The 'ActionGroup' for the property to be part of. Groups are updated all at the same time. Sequences and groups can even be in a group.
startVal: A 'Property' wrapper around the getter and setter functions of the value to be interpolated. Properties are created as follows:
//The property to interpolate must be public, gettable, and settable.
public float InterpolatedValue { get; set; }
//Inside of a function, the extension method 'GetProperty' can be called. This function takes in a lambda of the variable to be interpolated.
Property<float> floatProp = this.GetProperty(val => val.InterpolatedValue);
endVal: The value that the property is interpolating towards.
duration: The time in seconds of the interpolation.
ease: The type of easing curve that the interpolation should use. All the eases listed here (A link to Gizma Easeing) are implemented. A 'Curve' is can be implicitly converted from either the 'Ease' enum or a Unity 'AnimationCurve'. Creating a public class variable of a 'Curve' like this:
public Curve EasingCurve = Ease.Linear;
Will show up in the inspector like so:
Checking 'Custom' allows a custom AnimationCurve to be used:
After the sequence or group is filled with the desired actions, the Action must have its Update function called every frame, with the desired Delta Time passed in.
Note: Instead of creating a new ActionGroup in every script that uses Actions, it is recommended that the extension method 'GetActions' be called inside of the MonoBehaviour.
ActionGroup grp = this.GetActions();
This function will get and return the ActionGroup inside of the 'ObjectActions' component. If there is no 'ObjectActions' component on the GameObject, the function will add an invisible one. Using this component will mean that all actions on the object will be updated automatically.
Output
First, the InterpolatedValue will interpolate to 5 over 3 seconds, then...
ActionProperty: Interpolates an object from one value to another. In order to be interpolated a type must be able to be added or subtracted from itself, and be multiplied and divided by either floats or doubles.
ActionSequence: A sequence of other Actions that happen one after the other. A looping sequence will repeat once the final Action has completed.
ActionGroup: A group of other Actions that happen all at the same time. A looping group will repeat once the longest Action has completed.
ActionDelay: Simply delays the sequence for the specified number of seconds.
ActionCall: Will call the given function that takes up to 4 paramaters.
ActionReturnCall: Will call the given function that takes up to 4 paramaters and stores the return type.
All the mathematical eases on the following website have been implemented: http://gizma.com/easing/
Download the Action System!