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