Unity3d do not call functions if you do not need them.

Source: Internet
Author: User

The simplest and best of all optimizations is less workload execution. For example, when an enemy is far away, the perfect time is when the enemy falls asleep. When the player approaches, nothing is done. This is a slow processing method:

Function Update ()

{

// Early stages if the player is too far away.

If (Vector3.Distance (transform. position, target. position)> 100)

Return;

Perform real work...

}

This is not a good idea, because Unity must call the UPDATE function, and you are executing every frame of the work. A better solution is to disable behavior until players are near. There are three ways to do this:

1. Use OnBecameVisible and OnBecameInvisible. These Callbacks are all bound to the rendering system. As long as any camera can see the object, OnBecameVisible will be called. When no camera sees any one, OnBecameInvisible will be called. This method is very useful in many cases, but it is usually not particularly useful in AI, because as long as you leave the camera away from their enemies

Available.

Function OnBecameVisible (){

Enabled = true;

}

Function OnBecameInvisible ()

{

Enabled = false;

}

2. Use a trigger. A Simple spherical trigger will work very well. Once you leave this affected ball, you will get the OnTriggerEnter/Exit call.

Function OnTriggerEnter (c: Collider)

{

If (c. CompareTag ("Player "))

Enabled = true;

}

Function OnTriggerExit (c: Collider)

{

If (c. CompareTag ("Player "))

Enabled = false;

}

3. Use a collaboration program. The problem with Update calls is that they occur in each frame. It is likely that you only need to check the distance to the player every five seconds. This should save a lot of processing cycles.

Collaborators: special types of function programming meaning that allow blocking of its execution until some of its own conditions are met.

Function MyCoroutine (){

DoSomething ();

Yield; // stay at one frame

DoSomethingElse ();

}

If you call this function (start coroutine), it will act like any other normal function until it reaches the output. Explanation of the output command: In this sense, the command output stops the function and returns the code that controls execution, and calls the function to return the declared work. The main difference is that the output command causes you to delay the code to be executed (in the last example, the DoSomethingElse () Statement ).

Function MyCoroutine (){

DoSomething (); // immediately execute this

Yield; // return control to the caller

DoSomethingElse (); // This will be executed after one frame

}

Void Start (){

MyCoroution ();

}

If something happens to you after MyCoroutine calls for more code? Let's see some print examples:

Function MyCoroutine (){

Print ("This is printed second ");

Yield; // controls the return to start Function

Print ("This is printed one fourth, exactly one frame after the third ");

}

Void Start (){

Print ("This is printed first ");

MyCoroutine ();

Print ("This is printed third ");

}

When can you control the command, the output code will be executed. This depends on the output command parameters. according to the table below, there is nothing: it will wait for one frame and another coroutine to call: it will wait for the called coroutine to complete the execution of the WaitForSeconds object: it will wait for a while to get confused? The following is an example:

Function MyCoroutine (){

DoSomething (): // execute now

Yield WaitForSeconds (2); // control is returned to the caller

DoSomethingElse (); // This will be executed 2 seconds later

}

Void Start (){

MyCoroutine ();

}

Function MyCoroutine (){

DoSomething (): // execute now

Yield MyOtherCoroutine (); // execute MyOtherCoroutine!

DoSomethingElse (); // after the execution is complete, execute the MyOtherCoroutine command.

}

Function mythercoroutine (){

DoStuff (): // execute now

Yield WaitForSeconds (2); // control the return to the caller who starts the function () in this case ()

DoMoreStuff (); // This will be executed in 2 seconds

// After mythercoroutine is executed

}

Void Start (){

MyCoroutine ();

}

Coroutines & Yield

When writing game code, you often need to handle a series of events. This may lead to code like the following.

Private var state = 0;

Function Update ()

{

If (state = 0 ){

// STEP 0

State = 1;

Return;

}

If (state = 1 ){

// Step 1

State = 2;

Return;

}

//...

}

It is more convenient to use the yield statement. The yield statement is a special type of return, which ensures that the function will continue to be executed after the yield statement in the next call.

While (true ){

// STEP 0

Yield; // wait for one frame

// Step 1

Yield; // wait for one frame

//...

}

You can also pass a specific value to the yield statement to delay the execution of the Update function until a specific event occurs.

// Do something

Yield WaitForSeconds (5.0); // wait for 5 seconds

// Do more...

You can stack and connect coroutines.

In this example, Do is executed and continues immediately after the call.

Do ();

Print ("This is printed immediately ");

Function Do ()

{

Print ("Do now ");

Yield WaitForSeconds (2 );

Print ("Do 2 seconds later ");

}

This example will execute Do and wait until it completes before it continues to execute itself.

// Link coroutine

Yield StartCoroutine ("Do ");

Print ("Also after 2 seconds ");

Print ("This is after the Do coroutine has finished execution ");

Function Do ()

{

Print ("Do now ");

Yield WaitForSeconds (2 );

Print ("Do 2 seconds later ");

}

Any event processing handle can be a coroutine

Note that you cannot use yield in Update or FixedUpdate, but you can use StartCoroutine to start a function.

For more information about using yield, see YieldInstruction, WaitForSeconds, WaitForFixedUpdate, Coroutine and MonoBehaviour. StartCoroutine.

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.