Hi Guys and welcome to part 24. In the last part we started to set up some UI for our planets and star bases. In this part we will get to a stage where we can open the planet UI by clicking on a planet and also create some buttons to close the UI again.
Lets start with the easy bit and that is add the close buttons.
Right click on the Canvas and create a new Button. Call it Close Button.
Change the buttons text to an X.
Shrink the button down and move it so it roughly matches the picture below. Also make sure the button is anchored and scaled from the centre of the scene (to do this click on the rect transform square picture, hold shift and alt down and select the centre picture – see pic below).
Next create a script called CloseUI in the Scripts > UI folder. Attach this to the button we just created.
As the close button will close UI panels it will be the same on all of the UI we create. Therefore we can make it a prefab by dragging it from the hierarchy into our prefabs folder.
Open up the CloseUI script. Delete both the Start and Update methods and add a new public void method called ClosePanel. Do not pass it anything.
Add an if statement to check the close button has a parent object. To do this we need to use the transform.parent function and check it is not equal to null. In the if statement disable the parent by using the gameObject.SetActive function.
Now we just call this method from the buttons OnClick method in the inspector.
Click the apply button at the top of the Close Buttons inspector to apply these changes to the prefab.
Make the button a child of the Planet Panel and then duplicate it in the hierarchy and make the new one a child of the Star Base Panel.
Move the new one to a similar location on the Star Base Panel as the Planet Panel.
Hit play and test the buttons out. The panels should disable when the buttons are pressed :).
Obviously the star base panel should close when the planet panel closes but we can fix that later when we come to tidy up the UI.
Let’s start looking at how we can click on planets for the planet panel to appear.
We need to set up a Dictionary in our SolarSystem script much like the one in our Galaxy script. This time however it will have Planet as the Key instead of Star. Before we do this however we will need to add “using System.Collections.Generic” and “using System.Linq” to the top of the SolarSystem script.
Now declare a new Dictionary. Call this Dictionary planetToGameObjectMap.
In the CreateSolarSystem method set this Dictionary equal to a new Dictionary<Planet, GameObject>. We do this to make sure it is cleared everytime a new solar system is created.
Next after we create planetGO, in the for loop, add planet and planetGO to the Dictionary.
Now we need to create a new method of type Planet called ReturnPlanetFromGameObject. Make it public and pass it a GameObject called go. This Method will be exactly the same as the ReturnStarFromGameObject method in our Galaxy script except it will look for a Planet instead of a Star in planetToGameObjectMap instead of starToGameObjectMap. To make this easier just copy and past the contents of the Galaxy method here and make the necessary changes. Use the picture below if you are confused.
We currently use the SolarSystem Update function to look for any clicks on the mouse (I want to change this in the near future as I don’t really feel it belongs here). But currently it only looks for stars when we are in the Galaxy view. To add the ability to click on planets we need to add a new if statement below the one which checks for a left mouse click and the galaxy view is true. This one should check if the left mouse button is down and the galaxy view is false.
In this new if statement declare a new Planet called planet and set it equal to ReturnPlanetFromGameObject. Pass it hit.transform.gameObject (i.e the gameobject hit has hit).
Underneath this add another if statement checking that planet is not equal to null. We need to do this because ReturnPlanetFromGameObject can return a null value and if we do not check the value is not null we get some errors in the console when we click on the star.
What do we put in this if statement? Well we need to enable the planet panel and if the planet has a star base we also need to enable that panel as well. However we need to reference these panels first.
I don’t think the SolarSystem script is the best place to reference these panels however. For that I think the GUI Manager is a much more suitable object.
So open up GUIManagementScript and declare two new public GameObjects called planetPanel and starBasePanel. Set these to the corresponding panels in the inspector.
Now we can easily reference these from the SolarSystem script as the GUIManagementScript already has an instance created.
Return to the if statement we just left and add a Debug.Log to output the planet name and type in the console. Then enable the planet panel. The star base panel requires an if statement to check the planet has a star base before it becomes enabled.
Make sure the panels are disabled again when the solar system is destroyed as we do not want the panels appearing in the galaxy view.
Hit play and test everything is working. There are obviously some improvements we need to make to our UI, however at this stage, it will do the job.
That concludes this part of the tutorial in the next part we will look at making a build cue when we hit the build ship button.
Scripts_for_part_24 (zip file)
Scripts (Paste Bin)