Extended Coroutine: Custom YieldInstruction,

Source: Internet
Author: User

Extended Coroutine: Custom YieldInstruction,


The Coroutine mechanism of Unity is undoubtedly a highlight of this engine. It is important to implement many asynchronous logic in a sequential writing mode.

With regard to the principles of Coroutine, to be honest, there is no source code, so it is hard to say how it is implemented. Seeing that he used IEnumerator, he imagined that he used the. Net enumeration mechanism. There are posts on the Internet that do not discuss the implementation principles of Coroutine. If you are interested, you can open them.

Coroutine is so powerful that it lacks a series of YieldInstruction Derived classes, including WaitForSeconds, WaitForFixedUpdate, and AsyncOperation. I have been wondering if I can implement my YieldInstruction derived class? Today, I took some time to try something. The answer is "No, you can do it." Haha. Why do you say "no"? It is because YieldInstruction does not have any virtual functions to override. Why do you say "yes! We can use a special YieldInstruction derived class to implement equivalent functions. This class is Coroutine.

Download the complete Demo project:Http://pan.baidu.com/s/1jGGhRKE


This project is as follows:
Suppose we want to implement a YieldInstruction: After an animation is played, the program continues.

The specific implementation is also very simple. First, we need a derived class of IEnumerator and implement its three interfaces. The specific code is as follows:

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()    {    }}

The core logic is in the "MoveNext" function. I use m_animState.enabled to determine whether the animation has been played.

With this class, if we write: yield return new WaitForEndOfAnim (animState) in the coroutine function body, it does not work. Later changed:Yield return StartCoroutine (new WaitForEndOfAnim (animAttack ));OK. The complete test code is as follows:

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 test"); if (GUILayout. button ("START") {StartCoroutine (DoTest ();} GUILayout. endVertical (); GUILayout. endArea ();} IEnumerator DoTest () {Animation anim = Get ComponentInChildren <Animation> (); 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. start playing the attack animation. "+ Time. time * 1000); anim. play (animAttack. name); yield return StartCoroutine (new WaitForEndOfAnim (animAttack); Debug. log ("2. start playing the animation. "+ Time. time * 1000); anim. play (animHit. name); yield return StartCoroutine (new WaitForEndOfAnim (animHit); Debug. log ("3. start playing the animation of death. "+ Time. time * 1000); anim. Play (animDie. name); yield return StartCoroutine (new WaitForEndOfAnim (animDie ));}}
Finally, check the running result with the timestamp:




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.