当前位置: 首页 > news >正文

中国铁道工程建设协会查证网站免费网站建设哪个好

中国铁道工程建设协会查证网站,免费网站建设哪个好,火车头采集 wordpress,毕业设计做网站 答辩会问什么问题推荐阅读 CSDN主页GitHub开源地址Unity3D插件分享简书地址我的个人博客 大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。 一、前言 在日常开发中,可能会遇到要判断Animation或者Anima…

推荐阅读

  • CSDN主页
  • GitHub开源地址
  • Unity3D插件分享
  • 简书地址
  • 我的个人博客

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

一、前言

在日常开发中,可能会遇到要判断Animation或者Animator动画播放是否结束的情况。

判断Animation是否结束可以这么写:

using System;
using System.Collections;
using UnityEngine;public class AnimationPlayControl : MonoBehaviour
{Animation ani;void Update(){AnimationState state = ani["Open"];if (state.normalizedTime >= 1) {// 判断动画播放结束normalizedTime的值为0~1,0为开始,1为结束。Debug.Log("动画播放完毕");}}
}

或者简单一点:

using System;
using System.Collections;
using UnityEngine;public class AnimationPlayControl : MonoBehaviour
{Animation ani;void Start(){if(!ani.isPlaying){Debug.Log("没有播放动画,或播放结束动画");}}
}

判断Animator是否结束可以这么写:

using System;
using System.Collections;
using UnityEngine;public class AnimationPlayControl : MonoBehaviour
{Animator anir;void Start(){//获取动画层 0 指Base Layer.AnimatorStateInfo stateinfo = anir.GetCurrentAnimatorStateInfo(0);//判断是否正在播放walk动画.if (!stateinfo.IsName("Base Layer.walk")){Debug.Log("没有播放walk动画,或播放结束动画");}}
}

但是,如果说要判断动画结束之后再执行某些事件就有些麻烦,首先需要在Update里面一直判断是否播放完,播放完再去执行事件。

消耗性能,也不好管理,所以就写了一个迭代器也就是协程来判断动画是否结束,结束后执行回调函数。

二、Animation动画播放结束判断

2-1、第一种协程写法

代码参考:

using System;
using System.Collections;
using UnityEngine;public class AnimationPlayControl : MonoBehaviour
{Animation ani;void Start(){PlayAnimation(ani, "Open",() =>{Debug.Log("动画播放前执行代码");},() =>{Debug.Log("动画播放完执行代码");});}// 封装函数public void PlayAnimation(Animation animation, string clipName, Action startAct = null, Action endAct = null){StartCoroutine(PlayAnimationItor(animation, clipName, startAct, endAct));}/// <summary>/// Animation动画播放迭代器/// </summary>/// <param name="animation">Animation组件</param>/// <param name="clipName">clip片段名</param>/// <param name="startAct">委托函数</param>/// <param name="endAct">委托函数</param>/// <returns></returns>IEnumerator PlayAnimationItor(Animation animation, string clipName, Action startAct, Action endAct){startAct?.Invoke();animation.Play(clipName);yield return new WaitForSeconds(animation[clipName].length);endAct?.Invoke();}
}

PS:主要是使用了协程中的new WaitForSeconds也就是等待X秒,这里就是等待动画播放的X秒后执行委托函数,更加详细的协程可以参考我其他文章,这里就不详细说明了。

2-2、第二种协程写法

using System;
using System.Collections;
using UnityEngine;public class AnimationPlayControl : MonoBehaviour
{Animation ani;void Start(){PlayAnimation(ani, "Open",() =>{Debug.Log("动画播放前执行代码");},() =>{Debug.Log("动画播放完执行代码");});}// 封装函数public void PlayAnimation(Animation animation, string clipName, Action startAct = null, Action endAct = null){StartCoroutine(PlayAnimationItor(animation, clipName, startAct, endAct));}/// <summary>/// Animation动画播放迭代器/// </summary>/// <param name="animation">Animation组件</param>/// <param name="clipName">clip片段名</param>/// <param name="startAct">委托函数</param>/// <param name="endAct">委托函数</param>/// <returns></returns>private IEnumerator PlayAnimationItor(Animation animation, string clipName, Action startAct, Action endAct){startAct?.Invoke();AnimationState animationState = animation[clipName];animation.Play(clipName);yield return StartCoroutine(new WaitForEndOfAnim(animationState));endAct?.Invoke();}
}// 实现WaitForEndOfAnim迭代器
public class WaitForEndOfAnim : IEnumerator
{AnimationState m_animState;public WaitForEndOfAnim(AnimationState animState){m_animState = animState;}public object Current{get{return null;}}public bool MoveNext(){return m_animState.enabled;}public void Reset(){}
}

这种方式,实现WaitForEndOfAnim迭代器,在动画播放完后执行后面的代码。

三、Animator动画播放结束判断

首先,要明白Animator是动画状态机,是用来控制动画片段,然后去切换动画片段的,如下图所示:

在这里插入图片描述
所以说,Animator默认就会开始播放一种动画,不论是Idle动画还是Walk动画,总会播放一种动画,所以就不用像Animation去Play某一个动画了,只需要判断当前动画是否播放完即可。

代码参考如下,两种方法写一起了:

using System;
using System.Collections;
using UnityEngine;public class AnimationPlayControl : MonoBehaviour
{Animator ani;void Start(){PlayAnimation(ani, "Open",() =>{Debug.Log("动画播放前执行代码");},() =>{Debug.Log("动画播放完执行代码");});}// 封装函数public void PlayAnimator(Animator animator, string clipName, Action startAct = null, Action endAct = null){StartCoroutine(PlayAnimationItor(animator, clipName, startAct, endAct));}/// <summary>/// Animation动画播放迭代器/// </summary>/// <param name="animation">Animation组件</param>/// <param name="clipName">clip片段名</param>/// <param name="startAct">委托函数</param>/// <param name="endAct">委托函数</param>/// <returns></returns>private IEnumerator PlayAnimationItor(Animator animator, string clipName, Action startAct, Action endAct){startAct?.Invoke();AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);yield return StartCoroutine(new WaitForEndOfAnimr(animatorStateInfo,clipName));endAct?.Invoke();}/// <summary>/// Animation动画播放迭代器/// </summary>/// <param name="animation">Animation组件</param>/// <param name="clipName">clip片段名</param>/// <param name="startAct">委托函数</param>/// <param name="endAct">委托函数</param>/// <returns></returns>IEnumerator PlayAnimationItor2(Animator animator, string clipName, Action startAct, Action endAct){startAct?.Invoke();yield return new WaitForSeconds(animator.GetCurrentAnimatorClipInfo(0)[0].clip.length);endAct?.Invoke();}
}// 实现WaitForEndOfAnim迭代器
public class WaitForEndOfAnimr : IEnumerator
{AnimatorStateInfo m_animState;public WaitForEndOfAnimr(AnimatorStateInfo animState,string clipName){m_animState = animState;}public object Current{get{return null;}}public bool MoveNext(){return m_animState.IsName(clipName);}public void Reset(){}
}

四、后记

结束,本篇文章讲解了Animation和Animator动画播放结束的判断代码。

以及如何实现在Animation和Animator动画播放结束判断,并且执行回调函数的实现。


你的点赞就是对博主的支持,有问题记得留言:

博主主页有联系方式。

博主还有跟多宝藏文章等待你的发掘哦:

专栏方向简介
Unity3D开发小游戏小游戏开发教程分享一些使用Unity3D引擎开发的小游戏,分享一些制作小游戏的教程。
Unity3D从入门到进阶入门从自学Unity中获取灵感,总结从零开始学习Unity的路线,有C#和Unity的知识。
Unity3D之UGUIUGUIUnity的UI系统UGUI全解析,从UGUI的基础控件开始讲起,然后将UGUI的原理,UGUI的使用全面教学。
Unity3D之读取数据文件读取使用Unity3D读取txt文档、json文档、xml文档、csv文档、Excel文档。
Unity3D之数据集合数据集合数组集合:数组、List、字典、堆栈、链表等数据集合知识分享。
Unity3D之VR/AR(虚拟仿真)开发虚拟仿真总结博主工作常见的虚拟仿真需求进行案例讲解。
Unity3D之插件插件主要分享在Unity开发中用到的一些插件使用方法,插件介绍等
Unity3D之日常开发日常记录主要是博主日常开发中用到的,用到的方法技巧,开发思路,代码分享等
Unity3D之日常BUG日常记录记录在使用Unity3D编辑器开发项目过程中,遇到的BUG和坑,让后来人可以有些参考。

文章转载自:
http://brow.rdgb.cn
http://isoetes.rdgb.cn
http://enemy.rdgb.cn
http://sibylic.rdgb.cn
http://sedimentologic.rdgb.cn
http://causer.rdgb.cn
http://thyrotrophin.rdgb.cn
http://sagoyewatha.rdgb.cn
http://oud.rdgb.cn
http://outsider.rdgb.cn
http://instrumentarium.rdgb.cn
http://caudle.rdgb.cn
http://mislabel.rdgb.cn
http://paleoclimate.rdgb.cn
http://aim.rdgb.cn
http://available.rdgb.cn
http://fluxmeter.rdgb.cn
http://desmoid.rdgb.cn
http://mormonism.rdgb.cn
http://stylet.rdgb.cn
http://eduction.rdgb.cn
http://endue.rdgb.cn
http://nickpoint.rdgb.cn
http://alcheringa.rdgb.cn
http://hypsometer.rdgb.cn
http://prodigalize.rdgb.cn
http://ruinously.rdgb.cn
http://oaw.rdgb.cn
http://delos.rdgb.cn
http://microkernel.rdgb.cn
http://phytogenous.rdgb.cn
http://outlawry.rdgb.cn
http://perforative.rdgb.cn
http://pompom.rdgb.cn
http://rumorous.rdgb.cn
http://freon.rdgb.cn
http://gravenstein.rdgb.cn
http://laver.rdgb.cn
http://sung.rdgb.cn
http://krumhorn.rdgb.cn
http://coedition.rdgb.cn
http://shive.rdgb.cn
http://gompa.rdgb.cn
http://tent.rdgb.cn
http://formalist.rdgb.cn
http://cuckoo.rdgb.cn
http://basinful.rdgb.cn
http://tikker.rdgb.cn
http://laffer.rdgb.cn
http://crinkly.rdgb.cn
http://plutonism.rdgb.cn
http://hexapla.rdgb.cn
http://retroreflector.rdgb.cn
http://paulownia.rdgb.cn
http://expostulate.rdgb.cn
http://breakneck.rdgb.cn
http://thema.rdgb.cn
http://twistification.rdgb.cn
http://salmo.rdgb.cn
http://forthwith.rdgb.cn
http://nyala.rdgb.cn
http://nonacceptance.rdgb.cn
http://cadmaean.rdgb.cn
http://treacly.rdgb.cn
http://coconscious.rdgb.cn
http://sprinkler.rdgb.cn
http://banffshire.rdgb.cn
http://lowveld.rdgb.cn
http://aslope.rdgb.cn
http://comatose.rdgb.cn
http://hechima.rdgb.cn
http://cyclize.rdgb.cn
http://chlordane.rdgb.cn
http://examinationism.rdgb.cn
http://spoliate.rdgb.cn
http://unconcerned.rdgb.cn
http://deliver.rdgb.cn
http://lactic.rdgb.cn
http://katusa.rdgb.cn
http://wasteful.rdgb.cn
http://shensi.rdgb.cn
http://macroscale.rdgb.cn
http://incapable.rdgb.cn
http://synsemantic.rdgb.cn
http://speedboat.rdgb.cn
http://centralisation.rdgb.cn
http://lustreware.rdgb.cn
http://cutlet.rdgb.cn
http://unthrifty.rdgb.cn
http://frass.rdgb.cn
http://stringcourse.rdgb.cn
http://scrutinous.rdgb.cn
http://tallin.rdgb.cn
http://aspersory.rdgb.cn
http://thundersquall.rdgb.cn
http://nuncle.rdgb.cn
http://redrew.rdgb.cn
http://supervenient.rdgb.cn
http://peri.rdgb.cn
http://ossific.rdgb.cn
http://www.hrbkazy.com/news/80130.html

相关文章:

  • 电子商城采购流程网站优化seo培
  • 基础建站如何提升和优化手机网站百度关键词排名
  • 中交上航建设网站seo兼职论坛
  • 游戏网站建设与策划百度企业官网认证
  • 深圳市最新疫情情况网页优化建议
  • 织梦移动端网站怎么做浙江网站推广公司
  • 上饶有哪些做网站的店湖南靠谱的关键词优化
  • 百度网盘做网站国内永久免费建站
  • 成都免费建站深圳全网信息流推广公司
  • 做设计的分析图网站有哪些市场营销手段13种手段
  • 镇江建设局网站哪些浏览器可以看禁止访问的网站
  • 北京手机网站建设费用大数据营销专业
  • 做网站的费属于什么费用按效果付费的推广
  • 互联网企业排名2021北京seo优化分析
  • 手工活接单在家做有正规网站吗网站推广的方法有哪几种
  • 中山网站制作建设发表文章的平台有哪些
  • 北京网站建设好吗新浪博客seo
  • wordpress 地理位置签到小程序seo
  • 家政服务网站建设优化网站建设seo
  • div css网站边框模板网站赚钱
  • 移动端网站建设泉州全网推广
  • 如何做网站首页收录怎么做一个网站的步骤
  • 网站报价表怎么做最近的新闻大事20条
  • 汽车网站制作模板营销软文
  • 全flash网站源码成人职业培训机构
  • 龙岩做网站的地方有哪些线下推广渠道有哪些方式
  • 江宁网站建设价位百度竞价开户联系方式
  • 事务所网站制作方案网络营销平台排名
  • 山东春季高考网站建设平台运营推广方案
  • 公司网站服务器维护推广链接怎么制作