Game Development Reference
In-Depth Information
Managing your inventory
Now that you have got to grips with building a GUI with a 2D system as it stands, what
about the existing GUI framework that Unity has? Well, as a comparison, let's put in a
small player inventory viewer for the player and cover off the difficulties of using 2D with
the GUI framework.
To start off, create a new script named PlayerInventoryDisplay in the Scripts
root folder, Assets\Scripts , and replace its contents with the following code, adding
some basic variables:
using UnityEngine;
public class PlayerInventoryDisplay : MonoBehaviour
{
bool displayInventory = false;
Rect inventoryWindowRect;
private Vector2 inventoryWindowSize = new Vector2(150,
150);
Vector2 inventoryItemIconSize = new Vector2(130, 32);
float offsetX = 6;
float offsetY = 6;
}
The names of each property should be fairly self-explanatory:
• A flag to confirm whether the inventory window is displayed or not
• Some sizes for the window and the inventory content sizes
• Offsets to space things out in the window
Next, we add an Awake function to set up the display of the inventory window as fol-
lows. Based on the size of the screen, it will be displayed on different devices (we don't
want it to take up the whole screen):
void Awake()
{
inventoryWindowRect = new Rect(
Screen.width - inventoryWindowSize.x,
Search WWH ::




Custom Search