Game Development Reference
In-Depth Information
Creating GUI buttons
Our first step in creating 2D buttons in our GUI will be to add these variables:
List<Rect> SkillButtons = new List<Rect>();
List<Rect> ItemButtons = new List<Rect>();
We will use these two List arrays as containers to hold the rectangles for our but-
tons. For now, they aren't public but if you wanted to expose them to Inspector , you
could make them public.
Next, we'll need to add rectangles to our lists. Add this block of code to our Start()
function:
SkillButtons.Add(new Rect(Screen.width/2 + 50,
Screen.height/2 + 333, 55, 55));
SkillButtons.Add(new Rect(Screen.width/2 + 105,
Screen.height/2 + 333, 55, 55));
SkillButtons.Add(new Rect(Screen.width/2 + 160,
Screen.height/2 + 333, 55, 55));
ItemButtons.Add(new Rect(Screen.width/2 - 160,
Screen.height/2 + 333, 55, 55));
ItemButtons.Add(new Rect(Screen.width/2 - 105,
Screen.height/2 + 333, 55, 55));
ItemButtons.Add(new Rect(Screen.width/2 - 50,
Screen.height/2 + 333, 55, 55));
Here, we will add three buttons to each of our lists. We place our skill buttons to the
right of the center of the screen, and we place the item buttons to the left of the cen-
ter of the screen. Also, all of our buttons have a width and height of 55 .
Our last step in creating our buttons is to draw them. We will add this code to our
OnGUI() function:
GUI.Button(SkillButtons[0], "Skill A");
GUI.Button(SkillButtons[1], "Skill B");
Search WWH ::




Custom Search