Currently, our
Options
page is at 2000 in X
position. This means that we can't see it in the Game view of the editor. Since we are going to work on the Options
page again, it would be great to have a preview of what we're doing shown in the Game view.
In order to see our
Options
page in the Game view again, we need to re-enable it and set it back to the center of the screen and then hide our main menu:
UIRoot
| Options
GameObject and enable it.UIRoot
| Main
GameObject and disable it.OK, we can now see our Options
page in the Game view again. Sometimes, this kind of manipulation can be repetitive and annoying. Of course, you could simply move around your camera in the Scene view, but sometimes it's better to have a more accurate preview using the Game view.
In this case, we can have an Initialization.cs
script attached to both windows that could enable/disable them and position them correctly at the start.
Let's create this script that will position our windows automatically at the start:
UI Root
| Main
and Options
GameObjects.Initialization
and hit the Enter key on your keyboard.Open Initialization.cs
and declare this new global variable:
// Define GameObject's initial position within Inspector
public Vector3 position = Vector3.zero;
Now, replace the existing Start()
method with this one:
// Apply initial position at Start
private void Start ()
{
//Set the local position to requested value
transform.localPosition = position;
}
Save the script and go back to Unity. Select our UI Root
| Options
GameObject, and set the Position parameter of Initialization
to {2000, 0, 0}.
OK. The Options
page will be automatically moved to the required position at the start. We still need to handle which page is enabled at the start (main menu) and which is not (options).
Now, let's open our MenuManager.cs
script to make it simple to choose which GameObjects need to be enabled or disabled at the start.
Add these new global array variables to contain GameObjects to enable or disable:
// Assign in this array objects to enable public GameObject[] enableAtAwake; // And here ones to disable public GameObject[] disableAtAwake;
Now, add this new Awake()
method to the script:
// Before anything happens private void Awake () { // Enable all objects in enableAtAwake array foreach(GameObject currentGO in enableAtAwake) { if(currentGO != null) currentGO.SetActive(true); } // Disable all objects in disableAtAwake array foreach(GameObject currentGO in disableAtAwake) { if(currentGO != null) currentGO.SetActive(false); } }
Save the script and go back to Unity to configure this script correctly. Select our UI Root
GameObject, and for its attached MenuManager
component:
Main
GameObject inside the Enable At Awake array.Options
GameObject inside the Disable At Awake array.OK. Now, hit Unity's play button. Our main menu is enabled automatically at start, while the Options
page is disabled and moved outside of the screen. We can now enable/disable any page or move them around in the editor; they will always be reinitialized correctly at the start!
Great. We can now move on to see how we can fade in or fade out UI elements.
18.224.59.145