Unity delays implementation of a piece of code in a better way

Source: Internet
Author: User

Welcome to Unity Learning, unity training, Unity Enterprise training and education zone, there are many u3d resources, u3d training videos, u3d tutorials, U3d Frequently asked questions, U3d Project source code, we are committed to creating the industry Unity3d training, learning the first brand.

In unity, it is very common to delay the execution of a piece of code or a method or several methods.

The Invoke and Invokerepeating methods are generally used. As the name implies, the first one is executed once and the second is repeated execution.

Look at the definition:

void Invoke (String methodName, float time);


The first parameter is the method name (note is in the form of a string) and is not a more convenient delegate. The second one is how many seconds to delay. Executes only once.

void Invokerepeating (String methodName, float time, float repeatrate);


Invokerepeating The second parameter is the number of seconds after which the delay begins, and the third parameter is the second of each execution interval.


Have you found that these two methods have a disadvantage, is to enter the method name! That is, I said, if I want to delay the execution of a piece of code, I have to put the code in a method, and then use the Invoke or Invokerepeating method to execute.

This can be particularly inconvenient for reference to context variables, properties, and cannot pass parameters!!! What's the use of him?



I guess you must have used such a method. Yes, "synergy" sounds pretty big on the top of the name AH.

Use Startcoroutine to execute a method that takes IEnumerator as the return value, usually for asynchronous download Ah, and so time-consuming and can not let the game card dead.

There is also a good class waitforseconds, yes, it is a constructor that is used to delay the (delay .....). Is it better than million-ai? Bishier useful? )。

Okay, no nonsense, here is my own time delay method, placed in a class in a static method exists. The code that specifies the number of seconds can be delayed anywhere at any time.

Using Unityengine;
Using System.Collections;
Using System;

public class Delaytoinvoke:monobehaviour
{

public static IEnumerator Delaytoinvokedo (Action action, float delaySeconds)
{
Yield return new waitforseconds (delaySeconds);
Action ();
}
}
How to use it?

For example, if I click on a button in Ngui,


void OnClick ()
{
Startcoroutine (Delaytoinvoke.delaytoinvokedo () =
{
Application.loadlevel ("Option");
}, 0.1f));
}
You see that?

Application.loadlevel ("Option"), which is the code snippet that you want to delay execution.

You can write it for a long, long time. Action, whatever.

For more highlights, please click http://www.gopedu.com/


Unity delays implementation of a piece of code in a better way

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.