(3) New Features of Unity5.0 ------ animation StateMachineBehaviours,

Source: Internet
Author: User

(3) New Features of Unity5.0 ------ animation StateMachineBehaviours,

Source: http://blog.csdn.net/u010019717

Author: Sun Guangdong time: 2015.3.31

(State machine behaviours) the action of a State machine can be appended to an animation State or a substate machine in an Animator Controller script. Every time you enter a state, you can add various State dependencies such as playing sound to it. They can even be independent of animation. It is used for logic state machine. To Add (State machine behaviours) State machine behavior to the State or substate machine, click Add Behaviour in inspector.


You can choose to create a new State machine (State machine behaviours) from the existing State machine behavior.

The new (state machine behaviours) state machine behavior is created in C.


All (state machine behaviours) state machine behaviors inherit from the same base class, StateMachineBehaviour. Because they support inheritance, this function can be easily implemented if you want to add multiple classes. For more information about inheritance, see the following link.

StateMachineBehaviour Function

(State machine behaviours) the core of state machine behavior is five functions: two functions automatically called in the animation state: automatically called in a substate machine. When they are created, they are added to the behavior of the new (state machine behaviours) state machine to comment out some of these functions that are out of version. These functions are OnStateEnter, OnStateUpdate, OnStateExit, OnStateMove, and OnStateIK. The substate machine transition (transitions) of the additional functions is OnStateMachineEnter and OnStateMachineExit.

All these functions have three parameters passed to them: Animator, AnimatorStateInfo, and layer index.

override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex){}

·(Animator parameter)The animation parameter is a specific animator, which is a reference to the behavior of this state machine. For example, this can be used to set the animation parameter values only in this state, for example, for blend tree hybrid trees.

·AnimatorStateInfo is the action of the state machine, which is the current information of the state. It is equivalent to writing animator. GetCurrentStateInfo (layerIndex); this can be useful for operations involving the normal time of the clip.

·LayerIndex is the layer of the state machine action state. For example, 0 is the base layer, and 1 is used for the first layer.


With MonoBehaviours, The StateMachineBehaviour function is called under specific circumstances.

  • The first frame in the played status of OnStateEnter is called.

  • OnStateUpdate MonoBehaviour Updates is called after update. The animation or status of each frame is the state of this behavior during playing.

  • The last frame from OnStateExit to another state is called.

  • OnStateMove is called before OnAnimatorMove, and the state playing of each frame before MonoBehaviours calls. When OnStateMove is called, it stops MonoBehaviours from calling OnAnimatorMove.

  • 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.

  • OnStateMachineEnter is called on the first frame of a sub-state machine in the content of the animator plays.

  • OnStateMachineExit is called at the last frame of the transition from a sub-state machine.

Example of using the (State Machine Behaviours) State Machine Behavior

Taking beat 'em up style into consideration, when you perform special actions, you want to play particle systems and attack your own lives. This script can use an animation state that contains a special move's animation.

using UnityEngine;public class SpecialAttackParticlesSmb : StateMachineBehaviour{    public GameObject particles;            // Prefab of the particle system to play in the state.    public AvatarIKGoal attackLimb;         // The limb that the particles should follow.    private Transform particlesTransform;       // Reference to the instantiated prefab's transform.    private ParticleSystem particleSystem;      // Reference to the instantiated prefab's particle system.    // This will be called when the animator first transitions to this state.    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)    {        // If the particle system already exists then exit the function.        if(particlesTransform != null)            return;        // Otherwise instantiate the particles and set up references to their components.        GameObject particlesInstance = Instantiate(particles);        particlesTransform = particlesInstance.transform;        particleSystem = particlesInstance.GetComponent <ParticleSystem> ();    }    // This will be called once the animator has transitioned out of the state.    override public void OnStateExit (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)    {        // When leaving the special move state, stop the particles.        particleSystem.Stop();    }    // This will be called every frame whilst in the state.    override public void OnStateIK (Animator animator, AnimatorStateInfo stateInfo, int layerIndex)    {        // OnStateExit may be called before the last OnStateIK so we need to check the particles haven't been destroyed.        if (particleSystem == null || particlesTransform == null)            return;                // Find the position and rotation of the limb the particles should follow.        Vector3 limbPosition = animator.GetIKPosition(attackLimb);        Quaternion limbRotation = animator.GetIKRotation (attackLimb);                // Set the particle's position and rotation based on that limb.        particlesTransform.position = limbPosition;        particlesTransform.rotation = limbRotation;        // If the particle system isn't playing, play it.        if(!particleSystem.isPlaying)            particleSystem.Play();    }}

Communication between MonoBehaviours and StateMachineBehaviours

To correctly understand the differences between MonoBehaviours and StateMachineBehaviours, it is necessary to understand the differences between assets and scene objects scenario objects.

Scenario objects only exist in a single scenario. These include GameObjects game objects, their components, and prefabs preset instances. Assets exist in projects and can be referenced in any scenario. These include animation or Controllers, prefab assets, and StateMachineBehviours. Because scene and a specific object may not be loaded by loaded, asset assets cannot reference a scenario object. Because asset assets always exist in projects, the scenario objects can reference assets.

Remember that state machine behaviours is Assets and they cannot store references to scene objects. To reference a scene object in state machine behaviour state machine behavior, the referenced object must be found or passed to state machine behavior. For example:

override public void OnStateEnter (Animator animator, AnimatorStateInfo stateInfo, int layerIndex){    player = GameObject.Find(“Player”);}

(State machine behaviours) State machine behavior is not created in a way similar to MonoBehaviours. When a MonoBehaviours instance is created and added to a game object, it becomes a scenario object. The StateMachineBehaviour class is derived from ScriptableObject. Therefore, the state machine behavior is assets, not a scenario object. This means that if the state machine behavior needs to exist in one scenario, the state machine behavior will automatically create an instance at runtime and be called within the Animator's Awake. This means that it is not recommended to find references to MonoBehaviour during the Awake function, because it will produce unpredictable results.

To obtain a reference to StateMachineBehaviour in MonoBehaviour, you can use any animator. getBehaviour <> () or animator. getBehaviours <> (). getComponent <> () and GetComponents <> ().. GetBehaviour returns the specified StateMachineBehaviour for the first instance found on the animator. GetBehaviours returns an array of all StateMachineBehaviours of the specified type. Since the state machine behavior cannot be guaranteed to have been instantiated in Awake, these functions should be called in red in the Start function value.

Here is a short example of a query reference between a single StateMachineBehaviour and a MonoBehaviour.

using UnityEngine;public class ExampleMonoBehaviour : MonoBehaviour{    private Animator animator;                          // Reference to the Animator component on this gameobject.    private ExampleStateMachineBehaviour exampleSmb;    // Reference to a single StateMachineBehaviour.    void Awake ()    {        // Find a reference to the Animator component in Awake since it exists in the scene.        animator = GetComponent <Animator> ();    }    void Start ()    {        // Find a reference to the ExampleStateMachineBehaviour in Start since it might not exist yet in Awake.        exampleSmb = animator.GetBehaviour <ExampleStateMachineBehaviour> ();        // Set the StateMachineBehaviour's reference to an ExampleMonoBehaviour to this.        exampleSmb.exampleMb = this;    }}

using UnityEngine;public class ExampleStateMachineBehaviour : StateMachineBehaviour{    public ExampleMonoBehaviour exampleMb;}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.