UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Animator.SetInteger
# [Animator](Animator.html).SetInteger public void **SetInteger** (string **name**, int **value**); public void **SetInteger** (int **id**, int **value**); ## 参数 | name | 参数名称。 | | ------- | -------------- | | id | 参数 ID。 | | value | 新的参数值。 | ## 描述 设置给定整数参数的值。 可以使用该方法触发动画器状态之间的过渡。使用整数而不是浮点数或布尔值的一种方法是将其用于具有多个状态的对象,例如方向(向左转、向右转等)。每个方向可以对应一个数字,而不必每次重置的多个布尔值。 有关设置动画器的更多信息,请参阅有关[动画](../Manual/Animator.html)的文档。 注意:可以按名称或 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); } } ```
da
2022年5月24日 00:09
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码