"Unity" understands the principle of co-operation to achieve a waitforseconds

Source: Internet
Author: User

The effect that the process can achieve is to execute the code that needs to execute at a specified point in time, the function that starts a co-process in unity is Startcoroutine, and the classes that provide the delay are the following:

            New Waitforendofframe;      Wait for a frame            new Waitforfixedupdate;     Wait for a fixedupdate (fixed interval)            new Waitforseconds;         Wait x seconds            new WWW;                    Wait for external resources to finish loading

This paper explores the implementation of waitforseconds. Because in the development process, many times will encounter a situation is that the time-out or meet some conditions to continue to run, using the system to provide waitforseconds has been unable to meet the requirements, there are two solutions, one is to use Stopcoroutine to stop the process, But for unity, this behavior can be costly, so rewrite the waitforseconds so that it can meet our requirements. Here's what I think of the waitforseconds implementation:

    <summary>///Task extension///    </summary>    static class Ctaskextend    {        static public IEnumerator Waitforseconds (float second)        {            DateTime init_dt = DateTime.Now;            TimeSpan time;            while (true)            {time                = Datetime.now-init_dt;                if (time. TotalSeconds <= Second)                {                    yield return null;                }                Else                {break                    ;                }    }}}


The method called is similar to unity:

Yield return ctaskextend.waitforseconds (delaytime);

It seems to be very simple and really simple, so what kind of changes do you need to make if you encounter a previously mentioned situation (a time-out or a certain condition to keep running)? As follows:

    <summary>///Task extension///    </summary>    static class Ctaskextend    {public        delegate bool Conddelegate ();        static public IEnumerator Waitforseconds (float second, conddelegate cond = null)        {            DateTime Init_dt = datetime.no W;            TimeSpan time;            while (true)            {time                = Datetime.now-init_dt;                if (time. TotalSeconds <= Second &&!cond ())                {                    yield return null;                }                Else                {break                    ;                }    }}}

A callback function is added, each time checking whether the function is true, or if true, stops waiting.


Unity understands the principle of co-operation to implement one's own waitforseconds

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.