We will now learn how to create the popup list, see its parameters, and create a difficulty selector for our game.
Difficulty
. It's our difficulty box container.A Popup List GameObject has just been created. Let's look at its parameters:
Above
or Below
the Popup List's button. If this parameter is set to Auto
, NGUI will choose one of both depending on the available space.Below the UIPopup List
component, we have the usual UIButton
and UIPlay Sound
components that we have already seen before.
We will now use our new Popup List GameObject to select the game's difficulty level as shown in the following screenshot:
Let's create this difficulty selector as shown in the following steps:
420
, 43
}.[AAFFFF]Difficulty.
Popup
.Normal
and Hard
separated by a line.190
, G to 250
, B to 255
, and A to 255
.70
, G to 250
, B to 255
, and A to 255
.70
, G to 255
, B to 150
, and A to 255
.70
, G to 255
, B to 150
, and A to 255
.-76
, -20
}.170
, G to 255
, B to 190
, and A to 255
.3
.135
, G to 255
, B to 170
, and A to 255
.4
.Ok, we now have a Popup List GameObject that lets us select the game's difficulty level. Your Hierarchy panel should look like the following screenshot:
Now it's time to link it to a method that will take that difficulty into account. Do this by performing the following steps:
GameManager.cs
script.public enum Difficulties { Normal, Hard }
Difficulty
variable to store the current difficulty as follows:public static Difficulties Difficulty = Difficulties.Normal;
We used a static variable because it won't be destroyed when loading the game scene. By default, the difficulty level is set to Normal.
Now we need to add the OnDifficultyChange()
method that will change our Difficulty
variable when Popup List changes state as follows:
public void OnDifficultyChange() { //If Difficulty changes to Normal, set Difficulties.Normal if(UIPopupList.current.value == "Normal") Difficulty = Difficulties.Normal; //Otherwise, set it to Hard else Difficulty = Difficulties.Hard; }
Our method is ready; we need to call it when Popup List changes state. Do so by performing the following steps:
GameManager.OnDifficultyChange
.Now, the Popup List GameObject will change the Difficulty
variable according to its value. We will be able to access this static variable once we're in the game.
18.217.199.122