Game Development Reference
In-Depth Information
ensures that the subsequent x coordinates vary horizontally across the array,
as shown in the following code:
float xcoord = totalCellLength -
0.5f*(totalCellLength) +(width*i);
8. The rectangle that corresponds to the shape of the button we want to display
is then calculated with the following formula. Note that its position on the
screen is a function of i , the loop index, as well as y , the screen width and
height, and the button width and height:
Rect r = new Rect(totalCellLength -
0.5f*(totalCellLength) + (width*i),
yPosition*sh, width, height);
With all of these quantities now calculated, we will display the button with
the GUI.button(r, buttonTexture) method, as shown in the following
code. We will check for a true return value from this function because this
is how we track when the user clicks on a button:
if (GUI.Button(r, buttonTexture))
{
// to do - handle clicks there
}
9. Recall that we need to display the number of items with each button. We do
this with the GUI.Label method in a way analogous to the previous code.
We will compute a second rectangle for the quantity that mirrors the cell for
the button, but we will use half the cell width and height to position the rect-
angle in the upper-left corner for a nice effect!
10. We will convert the quantity field of the current InventoryItem class to
a string with the built-in function called ToString() that the integer imple-
ments, and we will pass this to the GUI.Label method, as shown in the fol-
lowing code:
Istring s = quantity.ToString()
GUI.Label(r2, s);
Search WWH ::




Custom Search