Item / Object Setup
v4.0
Last updated
v4.0
Last updated
The Inventory module requires your items to be set up with specific scripts attached to them. You will also need to make a "UI" version of each object, which will be used exclusively in the UI panels and "held item" operations.
World Items In many cases, you may already have items in your project. Setting those, or new items, to work with the Inventory module is easy.
Add the ItemWorld
script to each item that is used in the 3D world. The only variable on this script is a public Item item
, which is used by the Inventory module when instantiating the item from the inventory into the 3D world, like when a user "drops" the item.
If items in your world are able to be picked up by the player and placed into inventory, then you will need to ensure that your control scripts which spawn item objects into the world populates the Item
variable, otherwise, errors will occur when the player picks up the item.
Remember, Items module is required to make use of the Inventory module. It holds key information about each item, including its height and width in the grid, the inventory and world prefabs, and for runtime, the current spot the item is in, on the grid.
Inventory Items Next, for each item, create a new version for the inventory. In most cases, you can likely bring your world item in as a child of the new "Inventory" version, and simply turn off or remove things like any rigidbody or script components, which aren't used in this context. This way, if you update your world object prefab, the Inventory version will update as well.
Check the demo folder for "Item Objects - World" and "Item Objects - Inventory" to see how we set up the demo objects.
Tip: You can make a copy of our demo object, rename it, and replace the children to skip most of the steps below.
Add the ItemInventory
script to the new inventory version of your item.
Make sure you add a RectTransform
component, replacing the defualt Transform
component.
Populate the inGamePrefab
field with the world version of the item. This is what will be spawned when the item is "dropped" into the world.
Include the Top Left
prefab object as a child as well. The position should be set to 0.05, -0.05, 0
. This object is not required, but can be helpful when positioning the item during setup.
Finally, set the layer of the inventory item to UIInventory
.
Next, you will need to scale and position the item to properly fill the inventory grid. We will do this using the demo scene.
Position the UI Item for Inventory use
First, open the "Inventory Demo" demo scene. Toggle on the Inventory Panel
object.
Next, expand the Inventory Panel
, and Inventory Area
objects, and turn on all of the Inventory Grid Button
objects. You may notice in the Scene and Game view that these create a checker pattern on the inventory area. This is set up purposely to make it easier to visualize the grid spaces.
Use the gizmo in the Scene view to force the scene camera to look directly at the inventory area, and select "Orthographic" view. This will make the scene look more flat.
Collapse the Inventory Area
, and expand Inventory Objects
. This is where UI objects are instantiated during run time. You may notice the demo objects already present, and turned off. Bring your new inventory object into the scene, as a child of Inventory Objects
. In this tutorial, we will use one of the existing demo inventory objects.
Optionally, turn on the Top Left
object, which is a small white cube, then bring your inventory object so that the top left of the cube fits perfectly with the top left of the invenetory area.
Now you can turn off Top Left
, and position the child object -- your item -- so that it fits centered on the grid. You can also scale it to better fit the grid, and even rotate it on any axis to change how it appears. Make note of the number of grid spaces, Y
and X
, the item occupies.
Some parts of the object may extend out of the grid. Depending on your preference, this may e OK.
Important: Make sure there are no colliders active on your UI Inventory objects, or if they are, that they are set to not interact with each other.
When you are satistifed, save your prefab modifications! Don't forget this step :)
Also, toggle off the Inventory Panel
object.