Unity3d Don't call function

  
if you don't need it

The simplest and most optimized is the least amount of execution. For example, when an enemy is far away and the perfect time is acceptable when the enemy falls asleep. Nothing is done until the player approaches. This is a slow way of handling: function Update () { //Early on if the player is too far away. If (Vector3.Distance(transform.position, target.position) > 100) return; perform real work work… } This is not a good idea, because Unity must call the update function while you are performing every frame of work. A better solution is to disable the behavior until the player is close. There are 3 ways to do this: 1. Use OnBecameVisible and OnBecameInvisible. These callbacks are tied to the rendering system. As long as any camera can see the object, OnBecameVisible will be called, and when no camera sees any one, OnBecameInvisible will be called. This method is very useful in many situations, but it is usually not particularly useful in AI, as it will not be available as long as you leave the camera away from them. Function OnBecameVisible () { enabled = true; } function OnBecameInvisible () { enabled = false; } 2. Use a trigger. A simple spherical trigger will work very well. Once you leave this effect ball you will get an 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 collaborative program. The problem with Update calls is that they happen every frame. It is likely that you will only need to check the distance to the player every 5 seconds. This should save a lot of processing cycles.

Collaborative Program: A special type of functional programming meaning that allows it to be prevented from executing until certain conditions of its own are met. Function MyCoroutine(){ DoSomething(); yield; //Stay one frame DoSomethingElse(); } When you call this function (start coroutine) it will behave like any other normal function until the yield is reached. The yield instruction interprets: as the output of the instruction in this sense it stops the function and returns control to execute the code, calling the function to return the declared work. The main difference is that the yield instruction lets you delay the execution of the code, after (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 you have What happened after MyCoroutine called for more code? Let's see some examples of printing: function MyCoroutine(){ print(“This is printed second”); yield; //Control returns to the start function print(“This is printed one fourth, exactly one frame after the third&rdquo ;); } void Start(){ print(“This is printed first”); MyCoroutine(); print(“This is printed third”); } When you can control the instruction, the yield code will be executed. This depends on the yield instruction parameter, according to the table below. There is nothing: it will wait for one frame to call another coroutine: it will wait until the called coroutine finishes executing. WaitForSeconds object: Will it wait for a while? Here is an example: function MyCoroutine(){ DoSomething(): //Immediately execute yield WaitForSeconds(2); //Control returns to the caller DoSomethingElse(); //This will be executed after 2 seconds } void Start(){ MyCoroutine(); } function MyCoroutine(){ DoSomething(): //Immediately execute yield MyOtherCoroutine(); //Go to MyOtherCoroutine! DoSomethingElse(); //This will be executed after the execution of MyOtherCoroutine } function MyOtherCoroutine(){ DoStuff(): //Immediately execute yield WaitForSeconds(2); //Control returns to the caller of the start function in this case () DoMoreStuff(); //This will be executed after 2 seconds //MyOtherCoroutine is done here } void Start(){ MyCoroutine(); }

Coroutines & Yield When writing game code, often Need to deal with a series of events. This can lead to code like the one below. Private var state = 0; function Update() { if (state == 0) { //Do step 0 state = 1; return; } if (state == 1) { //Do 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 continues to execute from the yield statement on the next call. While(true) { //do step 0 yield; //wait for a frame //do step 1 yield; //wait for a 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 things … can stack and connect coroutines. This example executes Do 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 is finished before continuing to execute itself. //link coroutine yield StartCoroutine(“Do”); print(“Alsafter 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 handler can be a coroutine. Note that you can't use yield in Update or FixedUpdate, but you can use StartCoroutine to start a function. Refer to YieldInstruction, WaitForSeconds, WaitForFixedUpdate, Coroutine and MonoBehaviour.StartCoroutine for more information on using yield.

Copyright © Windows knowledge All Rights Reserved