Let’s go over how to add a simple shop system.
This article will continue to build on the UI and Inventory system from this article.
First, create the shop interface:
Add a game object to the scene which will be the shop and add the sprite, collider and rigidbody:
Create a script called Shop and attach to the game object:
Open up the Shop script. Add two ints, one for the currently selected item and the other for the cost of the selected item. Add OnTriggerEnter2D and OnTriggerExit2D for closing and opening the shop:
Add a public function for selecting item. This will be controlled via UI buttons and it will also set the position of the selection graphics:
Add a public function for buying the selected item:
Open up the UIManager script. Add a GameObject for the shop, Text for the gem amount shown on the shop interface and Image for the selection graphics:
Add the text from the shop to the UpdateUI function. Add the functions for opening and closing the shop interface and then a function for moving the selection graphics:
Open the InventoryScript. Add a bool property for each item and also the function to add those items:
Lastly, add the UI elements to the UIManager script on the inspector: