Version: 2021.3
public void SetInteger (string name, int value);
public void SetInteger (int id, int value);


name 参数名称。
id 参数 ID。
value 新的参数值。





注意:可以按名称或 ID 号识别参数,但名称或 ID 号必须与要在动画器中更改的参数相同。

//This script sends messages to an Animator component to tell it to make transitions based on an integer named “States”. You change and send this integer to the Animator by pressing the space and arrow keys.

//In order for this script to work, you have to set up your Animator Controller so the script can interact with it. //Create a new Animator Controller if you do not already have one you want to use. To do this, click on the GameObject you want to animate and go to its Inspector window. Click the Add Component button and go to Miscellaneous>Animator). //Double click the Animator to see the Animator Controller window. Open the Parameters tab and click the plus icon to add a new parameter. Choose Int from the dropdown. Name the new Integer (for this script, call it “States”). //Create a few animation states (right click the grid and choose Create State>Empty) and choose an Animation for each in the Motion field. //Next create transitions between each of the states (right click the state, choose Make Transition and click on the state you want it to transition to). //Finally, click on one of the arrows to bring up its Inspector. Click the + icon under the Conditions section and choose the parameter you made (“States”). Change Greater to Equals and choose a number that you want to represent this state. Do the same with any other states. //You may want to set up transitions back to the first animation state so that when the button is let go, it will return to the first state. You may also want to uncheck the Has Exit Time box for each transition. Otherwise transitions will wait for an animation to finish before proceeding.

using UnityEngine;

public class AnimatorSetIntExample : MonoBehaviour { Animator m_Animator;

void Start() { //Fetch the Animator from the GameObject you attached the script to m_Animator = GetComponent<Animator>(); }

void Update() { //Check if the horizontal buttons (A,D, left and right arrow keys) are being pressed if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0) //Set the integer named "States" in your Animator to 1. If the Animator is set up properly, this should trigger an animation. m_Animator.SetInteger("States", 1); //Press the down arrow key to start another animation transition else if (Input.GetKey(KeyCode.DownArrow)) //Set the "States" integer to 2. This triggers the animation that should start when "States" is equal to 2 m_Animator.SetInteger("States", 2); //Press the space key to set the "States integer to 3 else if (Input.GetKey(KeyCode.Space)) m_Animator.SetInteger("States", 3); else //If all the other keys are let go, set the "States" integer to 0. m_Animator.SetInteger("States", 0); } }
Copyright © 2020 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。