Animation State code control and code generation animation controller for Mecanim animations in Unity

Source: Internet
Author: User

Use code generation for multi-volume, complex, and regular controllers

Animation Status Code Control

1: Each animation state, such as entering the state, leaving the state, etc. may require code to participate in and processing, such as what to do after entering the animation unit, to open the animation unit to do what to do, in order to solve this problem, unity allows each animation unit to bind a script code, This script code must inherit from Statemachinebehaviour;
2: You can add a script to an animated state on the add behaviour of the animated State;
3:statemachinebehaviour Main interface:
(1) Onstateenter: Called when the animation starts to play;
(2) Onstateexit: Called when the animation finishes playing;
(3) Onstatemove: Called when the animation is moved;
(4) Onstateik: This method is called when the animation triggers the inverse motion;
(5) Onstateupdate: Each frame will be called;


Animation Status Code Control case

1. Create a Unity project and file directory

2. Import the resource file Package Body_anim.unitypackage (58th)

3. Open the Models folder, click Model Boy, set rig---->animation tyoe---->humanoid (humanoid animation)

4. Configure Avatar

5. Drag the model boy into the scene and mount a animator component yourself

6. Creating an animation controller under the Res folder create---->animator controller, called Anim_ctrl, double-click to open, you can press the middle mouse button to drag the background

7. Drag the Assets\animations\anisforfight [email protected] directly into the state machine in animator view, set as default, and connect a line to exit

8. Controller that associates Anim_ctrl to the animator component of the boy model

9. Run, the character will kick the leg and now we're going to add a code control to it

10 Click on the Forwardkick inside the state machine, there is an add behaviour under the right property panel, and a new script called Anim_test is created.

11. At this time anim_test script file outside the file directory, we can drag it into the Scripts folder

Open Script Anim_test

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classAnim_test:statemachinebehaviour {//Layerindex is the layer where the animation is located, and the Base layer Layerindex is 0//components to get other nodes can be done like Monobehaviour//Onstateenter is called if a transition starts and the state machine starts to evaluate this state    Override  Public voidOnstateenter (Animator Animator, Animatorstateinfo StateInfo,intLayerindex) {Debug.Log ("Onstateenter"); }    //each frame is executed//Onstateupdate is called on each Update frame between Onstateenter and Onstateexit callbacks    Override  Public voidOnstateupdate (Animator Animator, Animatorstateinfo StateInfo,intLayerindex) {Debug.Log ("onstateupdate"); }    //Onstateexit is called if a transition ends and the state machine finishes evaluating    Override  Public voidOnstateexit (Animator Animator, Animatorstateinfo StateInfo,intLayerindex) {Debug.Log ("Onstateexit"); }    //not much for the next two.//Onstatemove is called right after Animator.onanimatormove (). Code that processes and affects root motion should is implemented here//override public void Onstatemove (Animator Animator, animatorstateinfo stateInfo, int layerindex) {//    //}    //Onstateik is called right after Animator.onanimatorik ().    Code that sets up animation IK (inverse kinematics) should is implemented here. //override public void Onstateik (Animator Animator, animatorstateinfo stateInfo, int layerindex) {//    //}}

Code Generation Animation Controller

1:10 states, each 2 states need to establish a link between 22, then the animation controller will have 100 transition conditions, then the best way is the code automatically generated animation controller;
2: Extended editor, to dynamically generate this animation controller, do not have to manually modify;
3: Extended Editor related API:
(1) Create an animation controller: Createanimationcontrolleratpath ("path");
(2) Get animation state machine: Animatorstatemachine Controller.layers[index].statemachine;
(3) Set the default animation state: Statemachine.defaultstate
(4) Load AnimationClip:AssertDatabase.LoaderAssetAtPath ("path", typeof (Animationclip))
As Animationclip;
(5) Specify the animation for each state: State.motion = Animclip;
(6) Animcontroller.addparameter (name, Animationparamstype);
(7) Add a transition: Addtransition (Dst_state, false);
(8) The transition adds a condition: trans. Addcondition ();
(9) Last add exit state:addexittransition (); Make output animation;

Animation State code control and code generation animation controller for Mecanim animations in Unity

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.