Game Development Reference
In-Depth Information
In this window, we loop through all the items in the player inventory (if there are any) and
display them as buttons. You could then wire up these buttons to actions, if the item has
any, such as potions to be drank or bombs to be dropped.
Now, one thing to note is that the existing GUI system
does not natively support sprites
;
we have to use the GUI
Texture2D
drawing function to pick up the specific sprite out
of the spritesheet manually.
Note
The texture on the sprite is purely a reference to the full image the sprite came from. So, if
your image is a single sprite, this is OK. However, in most cases, it will be from a
spritesheet and will show the entire spritesheet.
So, we have to use the
GUI.DrawTextureWithTexCoords
function to grab the spe-
cific image region from the spritesheet. You should also note that we have to scale the re-
gion to the size of the full image because the coordinates given for the specific sprite are
unscaled.
Tip
Thanks to one of the topic reviewers, Trond Abusdal, for finding the preceding solution. I
tried and couldn't get it to work. Fantastic find!
One warning is that if you try to read the pixels from the sprite's texture, you will get an
Access denied
message because of the way sprites are imported.
Tip
All is not lost, as in future, we will have the
all new
Unity GUI
(
uGUI
), which will sup-
port sprites fully.
Alternatively, if you want, you can use separate sprites (losing some of the performance
you get from spritesheets) for buttons, which makes the GUI implementation easier; al-
ternatively, you can have separate textures for things you want to draw with the GUI sys-
tem (just alter the import settings from
Sprite
to
Texture
and click on
Apply
).
With the script in place, just add it to the
Player
game object in the
Home
scene so the
player can rummage through their pockets.