(iii) Unity5.0 of new features------animation statemachinebehaviours

Source: Internet
Author: User

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

Author: Sun Guangdong time: 2015.3.31

The behavior of state machine behaviours is that the animator Controller's script is capable of attaching to an animated state or a sub-state machine. Every time you enter a state, you can add a variety of state dependencies, such as playing sounds, and they can even be independent of the animated animation. and used for logic state machine (machines).

To add a state machine behaviours behavior to a state or sub-state machine, click Add Behaviourbutton in Inspector.


From here you are able to choose from existing (state machine behaviours) behavior, or create a new one.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdaxotcxnw==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>

The new state machine behaviours behavior is created in the C # language.


All state machine behaviours behavior is inherited from the same base class. Statemachinebehaviour.

Because they can support inheritance, suppose you want to join multiple classes. Of course, this feature can be easily implemented.

For a lot of other information about inheritance, please refer to the information on the links below.

Statemachinebehaviour function

At the core of the state machine behaviours behavior is 5 functions: the animator and the 2 functions that are actively invoked in the state of the animation: in a sub-state machine.

when they are created, adding to the behavior of the new state machine behaviours will stare out some of these functions beyond the version number. These functions are Onstateenter, Onstateupdate, Onstateexit, Onstatemove, and Onstateik. the sub-state machine transitions (transitions) For additional functions are onstatemachineenter and onstatemachineexit.

All of 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 ) animation parameters are specific Animator and are references to the behavior of this state machine. for example, this can be used to set the value of an animation parameter only in this state, for example, for a blend tree.

· The Animatorstateinfo is the state machine's behavior is the current information to the States.

it is equivalent to writing animator.  Getcurrentstateinfo (Layerindex); This can involve the operation of the clip clip's normal time is very practical.

· Layerindex are layer layers of state machine behavior states. for example. 0 is a base layer, 1 is used for the first, and so on.


With monobehaviours, the Statemachinebehaviour function is invoked under certain circumstances.

  • Onstateenter is being called at the first frame of the played state.

  • Onstateupdate monobehaviour updates are called, each frame animator is the state of this behavior when playing.

  • The onstateexit transitions to the last frame of a state that is called.

  • Onstatemove is called before Onanimatormove, and the state playing of each frame before the monobehaviours call.

    When Onstatemove is called, it stops monobehaviours call Onanimatormove.

  • Onstateik is called after Onanimatorik on monobehaviours for every frame the the and the State is being played. It is important to note that Onstateik would only be called if the state is on a layer of that have an IK pass. By default, layers does not has an IK pass and so this function would not be called. For more information in IK see the information linked below.

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

  • The onstatemachineexit is called from the last frame of the transition from a sub-state machine.

Example of a demo using state machine behaviours behavior

consider the beat ' em up style game when you perform a special action. You want to play the particle system particle systems and attack the rat. This script can be animated with a special move ' s animation state.

Using Unityengine;public class specialattackparticlessmb:statemachinebehaviour{public gameobject particles;    Prefab of the particle system to play on 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 is the called when the animator first transitions to the. Override public void Onstateenter (Animator Animator, animatorstateinfo stateInfo, int layerindex) {//If the PA        Rticle 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 would be called once the animator have transitioned out of the state. Override public void Onstateexit (Animator Animator, Animatorstateinfo stateInfo, int. layerindex) {//when Leav        ing the special move state, stop the particles.    Particlesystem.stop ();    }//This is called every frame whilst in the state.  Override public void Onstateik (Animator Animator, animatorstateinfo stateInfo, int layerindex) {//Onstateexit        May is 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 difference between monobehaviours and statemachinebehaviours, it is necessary to understand the differences between assets assets and scene objects scenes objects.

The scene object exists only in a single scene.

These include Gameobjects game objects and their components, as well as instances of prefabs presets.

Assets exists in project and can be referenced in no matter what scenario.

these contain the dynamic animator Controllers, prefab assets and statemachinebehviours. because scene and a particular object may not loaded loaded. Asset assets cannot reference a scene object. because asset assets are always present in project, scene objects can refer to assets.

remember that state machine behaviours is Assets and they cannot store references to scene objects. in order to refer to a scene object in state machine behaviour behavior, the referenced object must be able to be found or passed to the state machine behavior. to give a sample example:

Override public void Onstateenter (Animator Animator, animatorstateinfo stateInfo, int layerindex) {    player = Gameobje Ct. Find ("Player");}

State machine behaviours behavior they are not created in a manner like monobehaviours.

This becomes the scene object when the Monobehaviours instance is created and added to a game object. the Statemachinebehaviour class derives from Scriptableobject.    Therefore, the state machine behavior is assets, not the scene object. This means that the state machine behavior is to exist in a scene where the state machine behavior is created at the time of execution and is invoked internally by animator's internal awake. This means that it is not recommended to find references to them during the awake function of Monobehaviour. Because it will produce unpredictable results.

to get a reference to Statemachinebehaviour in Monobehaviour, you can use whatever animator. Getbehaviour<> () or animator.  Getbehaviours<> (). Getcomponent<> () and getcomponents<> (). Functions are similar. Getbehaviour The first instance found on animator returns the specified statemachinebehaviour. Getbehaviours Returns an array of all statemachinebehaviours of the specified type found. because the state machine behavior is not guaranteed to be instantiated in awake, these functions should be called at the Start function value red.

Here is a short example of a lookup reference between a single statemachinebehaviour and a monobehaviour.


Using Unityengine;public class examplemonobehaviour:monobehaviour{    private Animator Animator;                          Reference to the Animator component in 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 a examplemonobehaviour to this.        Examplesmb.examplemb = this;    }}

Using Unityengine;public class examplestatemachinebehaviour:statemachinebehaviour{public    Examplemonobehaviour EXAMPLEMB;}



????

(iii) Unity5.0 of new features------animation statemachinebehaviours

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.