Unity Animation 之 快速获得当前播放动画名称

 时间:2024-10-27 14:21:56

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,新建一个空工程,具体如下

Unity Animation 之 快速获得当前播放动画名称

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

Unity Animation 之 快速获得当前播放动画名称

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”赋给脚本,具体如下图

Unity Animation 之 快速获得当前播放动画名称

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

  • 按键精灵不暂停获取按键码GetLastKey代码的使用
  • win10如何给电脑配置IPV6的地址?
  • 《星际争霸2》攻略:[1]第一关:自由日
  • window7系统注册表查找方法
  • 如何利用SQL Server 2012查询以及带条件查询
  • 热门搜索
    承德避暑山庄旅游 西安旅游职业中专 旅游卫视行者 北京旅游景点路线 杭州灵隐寺旅游攻略 江西婺源旅游景点 去丽江旅游必备物品 北京旅游年票 湖南凤凰城旅游 去尼泊尔旅游多少钱