How to make a simple unity2d inventory system

Started by
1 comment, last by frob 9 years, 11 months ago

Trying to make a VERY simple inventory for unity2D. I would like to be able to pick up objects, drop them and have them fill up an inventory screen.

Can be made in either JS or C#

Thanks

Advertisement

This question would probably best be asked on a Unity related forum, I don't have Unity experience in particular but a bit of advice about a generic inventory system.

Usually an inventory is little more than an array/collection of references to game objects, the actual implementation of building the inventory itself doesn't have anything to do with graphics or the platform really. Usually you'd make a class that represents an inventory, it'd have specific "interface" methods that allow you to interact with the inventory(sort it, add items to it, remove items, get an item in a particular slot) and your player object or something may contain an inventory object as part of its definition.

The only real part that would really be different in unity would be the graphical aspect, this varies a lot based on the game. For instance if you wanted to do something like Minecraft or a typical MMO or something where you have a "slot" based inventory where you drag pictures around, then you'd have an object representing an image slot of an item that could be dragged between slots in an inventory gui window, releasing an item would instruct the inventory to move the item(in code) so on, so forth.

The mechanical backend of any inventory will be similar across the board however, that should give you an idea of where to begin looking at least.

As he wrote, it is just a collection.

You could store references to the individual GameObject items in a collection. Or you could store references to the prefab accompanied with a count of those items. Or you could just store a count of items in an array and then use a lookup table to figure out which prefab is associated with the array index. Or you could do something else entirely, whatever you dream up.

For the graphics associated with the items, you could build your own base class that uses some function or even a public variable to get the desired Texture2D associated with it, so instead of storing the base GameObject you store the subclass object and then use the accessor, which you could name GuiTexture or whatever else you wanted.

This topic is closed to new replies.

Advertisement