Skip to content

Instantly share code, notes, and snippets.

@softrare
Last active August 29, 2015 14:16
Show Gist options
  • Save softrare/d913657ccc38cca956da to your computer and use it in GitHub Desktop.
Save softrare/d913657ccc38cca956da to your computer and use it in GitHub Desktop.
A mask for a state machine behaviour script in Unity3d (v5). Information taken from http://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours
using UnityEngine;
using System.Collections;
/*
* source: http://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours
*
* The core of state machine behaviours are 5 functions that are automatically called whilst in an animator state and 2 functions that are automatically called whilst in a sub-state machine.
* Commented out versions of some of these functions are added to new state machine behaviours when they are created. These functions are OnStateEnter, OnStateUpdate, OnStateExit, OnStateMove and OnStateIK.
* The additional functions for sub-state machine transitions are OnStateMachineEnter and OnStateMachineExit.
* All of these functions have 3 parameters passed to them: an Animator, an AnimatorStateInfo and the layer index
*
* The Animator parameter is a reference to the specific animator that this state machine behaviour is on.
* For example, this could be used to set the values of animator parameters that were only necessary in this state, such as those for a blend tree.
* The AnimatorStateInfo is the current info for the state that the state machine behaviour is on. It is the equivalent of writing animator.GetCurrentStateInfo(layerIndex);
* This can be useful in operations that involve the normalised time of the clip.
* The layerIndex is the layer the state machine behaviour’s state is on. For example, zero for the base layer, one for the first, and so on.
*
* As with MonoBehaviours, the functions of a StateMachineBehaviour are called under specific circumstances.
* Read more:
* http://docs.unity3d.com/Manual/StateMachineBehaviours.html
* http://blogs.unity3d.com/2014/06/26/shiny-new-animation-features-in-unity-5-0/
*/
public class NewStateMachineBehaviourScript : StateMachineBehaviour {
/*
* This will be called when the animator first transitions to this state.
* OnStateEnter is called on the first frame of the state being played.
*/
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
/*
* OnStateUpdate is called after MonoBehaviour Updates on every frame whilst the animator is playing the state this behaviour belongs to.
*/
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
/*
* This will be called once the animator has transitioned out of the state.
* OnStateExit is called on the last frame of a transition to another state.
*/
override public void OnStateExit (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
/*
* OnStateMove is called before OnAnimatorMove would be called in MonoBehaviours for every frame the state is playing.
* When OnStateMove is called, it will stop OnAnimatorMove from being called in MonoBehaviours.
*/
override public void OnStateMove (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
/*
* This will be called every frame whilst in the state.
* OnStateIK is called after OnAnimatorIK on MonoBehaviours for every frame the while the state is being played. It is important to note that OnStateIK will only be called if the state is on a layer that has an IK pass.
* By default, layers do not have an IK pass and so this function will not be called. For more information on IK see the information linked below.
*/
override public void OnStateIK (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
/*
* OnStateMachineEnter is called on the first frame that the animator plays the contents of a Sub-State Machine.
*/
public void OnStateMachineEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
/*
* OnStateMachineExit is called on the last frame of a transition from a Sub-State Machine.
*/
public void OnStateMachineExit (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment