layerIndex | 层索引。 |
AnimatorStateInfo 一个 AnimatorStateInfo,其中包含有关当前状态的信息。
返回 AnimatorStateInfo,其中包含有关当前状态的信息。
从动画器中的当前状态获取数据。使用该函数可获取状态的详细信息,包括访问状态的速度、长度、名称和其他变量。要从状态持有的剪辑中收集信息,请参阅 Animator.GetCurrentAnimatorClipInfo。
//Create a GameObject and attach an Animator component (Click the Add Component button in the Inspector window, go to Miscellaneous>Animator). //Create an Animator by going to Assets > Create > Animator Controller. Attach this Controller to the Animator attached to your GameObject //In the Animator Controller, create a Trigger parameter in the Parameters tab and name it “Jump”. Then create states and transition arrows that use this parameter.
//This script triggers an Animation parameter when you press the space key.
using UnityEngine;
public class Example : MonoBehaviour { Animator m_Animator; //Use to output current speed of the state to the screen float m_CurrentSpeed;
void Start() { //Get the Animator, which you attach to the GameObject you intend to animate. m_Animator = gameObject.GetComponent<Animator>(); //The current speed of the first Animator state m_CurrentSpeed = m_Animator.GetCurrentAnimatorStateInfo(0).speed; }
void Update() { //Press the space bar to tell the Animator to trigger the Jump Animation if (Input.GetKeyDown(KeyCode.Space)) { m_Animator.SetTrigger("Jump"); }
//When entering the Jump state in the Animator, output the message in the console if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump")) { Debug.Log("Jumping"); } }
void OnGUI() { //Output the first Animation speed to the screen GUI.Label(new Rect(25, 25, 200, 20), "Speed of State : " + m_CurrentSpeed); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.