[Unity]   Unity3D教程-自定义YieldInstruction

查看:767 |回复:2 | 2015-12-3 16:51:03

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 幺九 于 2019-3-26 11:13 编辑

  关于Coroutine的原理,说实话,这个没有源代码,不好说具体是怎么实现的。看他用到了IEnumerator,想象是使用了.Net的枚举机制。网上有不是探讨Coroutine实现原理的帖子,有兴趣的同学可以翻开看看。
  Coroutine之所以强大,缺不了一系列YieldInstruction的派生类,包括:WaitForSeconds , WaitForFixedUpdate,AsyncOperation等等,我一直在琢磨能不能实现自己的YieldInstruction派生类呢?今天花时间尝试了一些,答案是“不能,也能”,哈哈。为什么说“不能”呢,是因为YieldInstruction这个类可没什么虚函数可以去override的,为什么说“能”呢!我们可以借用一个特殊的YieldInstruction派生类,来实现等同的功能,这个类就是Coroutine。
  假设我们想实现这样一个YieldInstruction:当一个动画播放完事儿之后,程序再继续。
  具体的实现也很简单,首先我们需要一个IEnumerator的派生类,并实现其3个接口,具体代码如下:
  using UnityEngine;
  using System.Collections;
  public class WaitForEndOfAnim : IEnumerator
  {
  AnimationState m_animState;
  public WaitForEndOfAnim(AnimationState animState)
  {
  m_animState = animState;
  }
  //-- IEnumerator Interface
  public object Current
  {
  get
  {
  return null;
  }
  }
  //-- IEnumerator Interface
  public bool MoveNext()
  {
  return m_animState.enabled;
  }
  //-- IEnumerator Interface
  public void Reset()
  {
  }
  }
  这里面核心的逻辑就在“MoveNext”函数中,我通过m_animState.enabled来判断动画是否播放完了。
  有了这个类时候,如果我们在协程函数体中写:yield return new WaitForEndOfAnim(animState),发现并没有其作用。后来改为:yield return StartCoroutine(new WaitForEndOfAnim(animAttack));就OK了。完整的测试代码如下:
  using UnityEngine;
  using System.Collections;
  public class UnitTest : MonoBehaviour
  {
  // Use this for initialization
  void Start()
  {
  }
  void OnGUI()
  {
  GUILayout.BeginArea(new Rect(6, 6, 200, 300));
  GUILayout.BeginVertical();
  GUILayout.Box("Conrountinue测试");
  if (GUILayout.Button("启动"))
  {
  StartCoroutine(DoTest());
  }
  GUILayout.EndVertical();
  GUILayout.EndArea();
  }
  IEnumerator DoTest()
  {
  Animation anim = GetComponentInChildren();
  AnimationState animAttack = anim["attack"];
  animAttack.speed = 0.1f;
  AnimationState animHit = anim["hit"];
  animHit.speed = 0.1f;
  AnimationState animDie = anim["die"];
  animDie.speed = 0.1f;
  Debug.Log("1.开始播放攻击动画。" + Time.time * 1000);
  anim.Play(animAttack.name);
  yield return StartCoroutine(new WaitForEndOfAnim(animAttack));
  Debug.Log("2.开始播放受击动画。" + Time.time * 1000);
  anim.Play(animHit.name);
  yield return StartCoroutine(new WaitForEndOfAnim(animHit));
  Debug.Log("3.开始播放死亡动画。" + Time.time * 1000);
  anim.Play(animDie.name);
  yield return StartCoroutine(new WaitForEndOfAnim(animDie));
  }
  }
  最后,运行结果看下图,注意时间戳:
SouthEast.jpg   好了,本篇unity3d教程关于自定义扩展Coroutine的讲解到此结束,下篇我们再会。


评分

参与人数 1活跃度 +1 展开 理由
幺九 + 1 【封面】或展示不规范,请尽快补充,否则会被移出资源区!

查看全部评分

2015-12-3 16:51:03  
 赞 赞 2

使用道具 登录

2个回答,把该问题分享到群,邀请大神一起回答。
2#
66666,流弊!!
回复 收起回复
2016-10-16 06:38:19   回复
 赞 赞 2

使用道具 登录

3#
回复 收起回复
2023-2-28 11:11:38   回复
 赞 赞 2

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表