Game Development Reference
In-Depth Information
4. We also keep a
Texture
variable to store the icon that will be displayed in
the inventory for this GameObject as follows:
public Texture tex;
5. The
HandleInteraction()
method of this class works on the interactive
object that this script is attached to. To begin, we get the
InventoryMgr
component off the player if it can be found. Don't worry that we haven't cre-
ated the
InventoryMgr
yet; we will!
if (player)
iMgr =
player.GetComponent<InventoryMgr>();
6. As we extend the number of interaction types that our game supports, this
method will grow. For now, if
PutIninventory
is the type, we will delegate
i=InventoryMgr
to add this
InteractiveObj
to its collection as follows:
if (interaction ==
InteractionAction.PutInInventory)
{
if (iMgr)
iMgr.Add(this.gameObject.GetComponent<interactiveObj
();
}
Congratulations! You have implemented an
ObjectInteraction
class that oper-
ates on the
InteractiveObj
class. Let's continue by implementing the
Invent-
oryItem
class.
Implementing the InventoryItem script
The
InventoryItem
class is the base item container that the
InventoryMgr
col-
lection is built from. It contains a reference to
GameObject
that has been inserted in
the inventory (via the
ObjectInteraction
class). It also has a copy of the texture
to display in the inventory as well as the number of objects that a particular
Invent-
oryItem
represents, as shown in the following code: