(iii) new features of Unity5.0------state machine Behaviours

Source: Internet
Author: User

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

Author: Sun Guangdong time: 2015.3.31

The behavior of the state machine behaviours is in the script of the animator controller that can be attached to an animation 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 so on, they can even be independent of the animated animation. and used for logic state machine (machines). To add a state machine behaviours behavior to state or sub-state machines, click the Add Behaviour button in Inspector.


From here you can choose to behave from existing (state machine behaviours) states, or create a new one.

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, if you want to add multiple classes, of course, this feature can be easily implemented. For more information about inheritance, see the information that is linked below.

Statemachinebehaviour function

The core of the state machine behaviours behavior is 5 functions: automatically called and 2 functions in (animator State) animation states: Automatically called in a sub-state machine. When they are created, they are commented out in the behavior of new (state machine behaviours) states machines that are beyond the version of these functions. 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 be useful to involve the operation of the clip clip's normal time.

· Layerindex are layer layers of state machine behavior states. For example, 0 is a base layer, and 1 is used for the first one, 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 last frame of the onstateexit transition to another state is called.

  • Onstatemove is called before Onanimatormove, and the state of each frame before the monobehaviours call is playing. 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 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 dead. 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 difference 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 any scenario. These include moving animator Controllers, prefab assets and statemachinebehviours. because scene and a particular object may not loaded loaded, the asset asset 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. as An example:

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

           (state machine Behaviours) state machine behavior They are not created in a way like monobehaviours. When the Monobehaviours instance is created and added to a game object, it becomes the scene 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, the state machine behavior is automatically created at run time, and is called within 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 obtain a reference to Statemachinebehaviour in Monobehaviour, you can use any of the 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 that are found for the specified type. Since 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) new features of Unity5.0------state machine Behaviours

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.