Let’s Make a 4X Space Game with Unity – Part 24 Star Bases 3

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.

Part024Pic001
Close Button

Change the buttons text to an X.

Part024Pic002.JPG
Button Text
Part024Pic003.JPG
Button

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).

Part024Pic004
Small Button
Part024Pic005.JPG
Anchor and Scale Point

Next create a script called CloseUI in the Scripts > UI folder. Attach this to the button we just created.

Part024Pic006.JPG
CloseUI

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.

Part024Pic007.JPG
Close Button Prefab

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.

Part024Pic008.JPG
CloseUI Setup

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.

Part024Pic009.JPG
Panel Closing Code

Now we just call this method from the buttons OnClick method in the inspector.

Part024Pic010.JPG
Button Setup

Click the apply button at the top of the Close Buttons inspector to apply these changes to the prefab.

Part024Pic011.JPG
Click Apply

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.

Part024Pic012.JPG
Child Close Buttons

Move the new one to a similar location on the Star Base Panel as the Planet Panel.

Part024Pic013.JPG
Star Base Panel Close Button

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.

Part024Pic014.JPG
Using Stuff

Now declare a new Dictionary. Call this Dictionary planetToGameObjectMap.

Part024Pic015.JPG
New Dictionary

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.

Part024Pic025.JPG
Setting a New Dictionary

Next after we create planetGO, in the for loop, add planet and planetGO to the Dictionary.

Part024Pic017.JPG
Adding 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.

Part024Pic016.JPG
ReturnPlanetFromGameObject

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. 

Part024Pic018
Checking for Left Mouse Click and 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).

Part024Pic019.JPG
Getting the Planet from the Click

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.

Part024Pic020.JPG
Check Planet is not Null

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.

Part024Pic021.JPG
Referencing the Panels
Part024Pic022.JPG
Setting the 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.

Part024Pic023.JPG
Enabling the Panels

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.

Part024Pic024
Disabling the Panels

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.

If you are enjoying these tutorials please help me create more by visiting my Patreon page. All your pledges will help me keep these tutorials free and create more content. I only ever charge patreons once per month if I post a tutorial (no matter if I post more than one).  I hope you will consider it 🙂 but even if you don’t become a patreon supporter all my tutorials will remain free and I thank you for taking the time to read them. I hope they help :).

Scripts_for_part_24 (zip file)

Scripts (Paste Bin)

Part 25 Star Bases 4

2 Comments »

Leave a Reply