Reducing the number of active objects by making objects inactive whenever possible

Optimization principal 1: Minimize the number of active and enabled objects in a scene.

Sometimes, we may not want to completely remove an object, but it is possible to go one step further than disabling a scripted component by making the parent GameObject that contains the scripted component inactive. This is just like deselecting the checkbox next to the GameObject in the Inspector, as shown in the following screenshot:

Reducing the number of active objects by making objects inactive whenever possible

How to do it...

To reduce computer processing workload requirements by making an object inactive when it becomes invisible, follow these steps:

  1. Copy the previous recipe.
  2. Remove the scripted component DisableWhenNotVisible from your Cube, and instead, add the following C# script class InactiveWhenNotVisible to Cube:
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    
    public class InactiveWhenNotVisible : MonoBehaviour {
      // button action
      public void BUTTON_ACTION_MakeActive(){
        gameObject.SetActive(true);
        makeActiveAgainButton.SetActive(false);
      }
    
      public GameObject makeActiveAgainButton;
    
      private GameObject player;
    
      void Start(){
        player = GameObject.FindGameObjectWithTag("Player");
      }
    
      void OnBecameInvisible() {
        makeActiveAgainButton.SetActive(true);
        print ("cube became invisible");
        gameObject.SetActive(false);
      }
    
      void Update(){
        float d = Vector3.Distance( transform.position, player.transform.position);
        print(Time.time + ": distance from player to cube = " + d);
      }
    }
  3. Create a new Button, containing the text Make Cube Active Again, and position the button so that it is at the top of the Game panel and stretches the entire width of the Game panel, as shown in the following screenshot:
    How to do it...
  4. With the Button selected in the Hierarchy, add a new On Click() event for this button, dragging the Cube as the target GameObject and selecting public function BUTTON_ACTION_makeCubeActiveAgain().
  5. Uncheck the active checkbox next to the Button name in the Inspector (in other words, manually deactivate this Button so that we don't see the Button when the scene first runs).
  6. Select the Cube in the Inspector and drag the Button into the MakeActiveAgainButton variable slot of its script class InactiveWhenNotVisible component, as shown in the following screenshot:
    How to do it...

How it works...

Initially, the Cube is visible and the Button is inactive (so not visible to the user). When the Cube receives an OnBecameInvisible event message, its OnBecameInvisible() method will execute. This method performs two actions:

  • It first enables (and therefore makes visible) the Button.
  • It then makes inactive the script's parent gameObject (that is, the Cube GameObject).

When the Button is clicked, it makes the Cube object active again and makes the Button inactive again. So, at any one time, only one of the Cube and Button objects are active, and each makes itself inactive when the other is active.

Note that an inactive GameObject does not receive any messages, so it will not receive the OnBecameVisible() message, and this may not be appropriate for every object that is out of sight of the camera. However, when deactivating objects is appropriate, a larger performance saving is made compared to simply disabling a single scripted Monobehaviour component of a GameObject.

The only way to reactivate an inactive object is for another object to set the GameObject component's active property back to true. In this recipe, it is the Button GameObject, which, when clicked, runs the BUTTON_ACTION_makeCubeActiveAgain() method, which allows our game to make the Cube active again.

See also

Refer to the following recipes in this chapter for more information:

  • Reducing the number of objects by destroying objects at a death time
  • Reducing the number of enabled objects by disabling objects whenever possible
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.202.203