Unity Animation 之 快速获得当前播放动画名称
Unity Animation 之 快速获得当前播放动画名称。游戏中有时候要获得当前动画的名称,以便针对性的进行处理事件,本节介绍如何快速获得当前动画的名称的简单案例,具体如下
工具/原料
Unity
Animation
一、知识要点
1、Animation:class in UnityEngineThe animation component is used to play back animations.You can assign animation clips to the animation component and control playback from your script. The animation system in Unity is weight-based and supports Animation Blending, Additive animations, Animation Mixing, Layers and full control over all aspects of playback.
2、方法提要: foreach (AnimationState a in anim) { if (anim.IsPlaying(a.name)) { return a.name; } }
二、Animation 之 快速获得当前播放动画名称
1、打开Unity,新建一个空工程,具体如下

3、新建一个脚本“AnimationTest”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

5、“AnimationTest”脚本具体了内容如下:using UnityEngine;public class AnimationTest : MonoBehaviour { public Animation anim; // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.R)) { anim.Play("Run"); } if (Input.GetKeyDown(KeyCode.G)) { Debug.Log("正在播放的动画名为:"+ GetAnimationClipName()); } } public string GetAnimationClipName() { foreach (AnimationState a in anim) { if (anim.IsPlaying(a.name)) { return a.name; } } return null; }}
6、脚本编译正确,回到Unity界面,在场景中新建一个“GameObject”,把脚本“AnimationTest”赋给“GameObject”,并把模型的“Animation”赋给脚本,具体如下图

8、到此,《Unity Animation 之 快速获得当前播放动画名称》讲解结束,谢谢