Game Development Reference
In-Depth Information
GUI.Button(SkillButtons[2], "Skill C");
GUI.Button(ItemButtons[0], "Item A");
GUI.Button(ItemButtons[1], "Item B");
GUI.Button(ItemButtons[2], "Item C");
What each of these lines does is draw our button at the location of our rectangles,
which we stored in our lists. They also provide text to be displayed on the button; we
will use placeholder text as an example.
Creating a health bar
To create a health bar, we will use a GUI box. First, we'll need to add a few variables
to calculate our health and the length of the bar:
public float currentHP = 100;
public float maxHP = 100;
public float currentBarLength;
public float maxBarLength = 100;
We have two variables for our health and bar length: one for the current amount and
the other for the maximum amount. Our variables are set to public so that we can
access them from outside our script.
Finally, to make the health bar, we'll need to add a couple of lines to the OnGUI()
function to draw it on the screen:
currentBarLength = currentHP * maxBarLength /
maxHP;
GUI.Box(new Rect(Screen.width/2 - 20,
Screen.height/2 + 300, currentBarLength, 25f),
"");
The first line of the code will draw our health bar. We set its location to be just above
our buttons we created earlier. The second line of the code calculates how long the
health bar will be. It multiplies the current amount of health by the maximum bar
Search WWH ::




Custom Search