Scripting the buttons

The obvious next step is to get those buttons working. Of course, we need to add a little bit of scripting, shown as follows:

  1. Open the SceneController script we created earlier in your code editor. Just before the Update method, insert the following section of code:
private List<GameObject> m_sceneObjects = new List<GameObject>();
private List<GameObject> m_scenePlanes = new List<GameObject>();
private bool m_planeOnState;
public void ClearScene()
{
foreach(var obj in m_sceneObjects)
{
Destroy(obj);
}
m_sceneObjects.Clear();
}
public void Planes()
{
m_planeOnState = !m_planeOnState;
//turn plane visibility on or off
foreach(var plane in m_scenePlanes)
{
plane.SetActive(m_planeOnState);
}
}
  1. In this code, we first create some lists to store scene objects (m_sceneObjects) and planes (m_scenePlanes), with a new boolean to track the state of the planes m_planeOnState (visible or not). Next, we add two new methods (ClearScene and Planes). ClearScene iterates over m_sceneObjects using foreach and removes the object from the scene with the Destroy method. Destroy is the method used to remove and clean up game objects from a scene. The Planes method flips the state of m_planeOnState and then loops through the planes and sets their state with SetActive. If an object is active, it means that it is visible and being updated in a scene. An inactive object is disabled and does not render.
We are staying consistent with the same naming conventions in this example in order to match the code style. If using m_ to denote a private member variable is not your style, don't use it. You may also want to refactor this code and replace names such as andyObject with something more appropriate. Visual Studio has a great set of refactoring tools that make tasks like this easy.
  1. Scroll down in the Update method and add the line after the line identified:
var andyObject = Instantiate... //after me
m_sceneObjects.Add(andyObject);
  1. This line of code just adds the andyObject (poorly named now) to our list of scene objects. The andyObject is first instantiated with the Instantiate method. Think of Instantiate as the opposite of Destroy.

 

  1. Scroll back up and add the line after the line identified:
GameObject planeObject = Instantiate... //after me
m_scenePlanes.Add(planeObject);
  1. The same thing here, we are adding the newly instantiated planeObject to our list of scene planes.
  2. Save the file and return to Unity. We now need to hook up the buttons. As always, wait for the compiler to finish in order to ensure that you didn't create a syntax error.
  3. Select the Clear button and in the Inspector window, scroll to the Button component. Click on the + button at the bottom to add a new event handler, and then set the properties of the handler to those shown here:
Adding the button event handler
  1. Repeat the process for the Planes button. This time though, connect the Planes method.
  2. Connect, build, and run. Try to place an object and then use the buttons to clear it.

Now, you should be able turn on and off the plane visibility and clear any objects you created. In the next section, we will extend our UI to allow the user to interact with the objects.

..................Content has been hidden....................

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