- Unity 2018 Cookbook(Third Edition)
- Matt Smith
- 721字
- 2025-02-23 18:56:39
How to do it...
To display inventory total text for multiple pickups of different object types, follow these steps:
- Start with a new copy of the Simple2Dgame_SpaceGirl mini-game.
- Edit the tags, changing Star to Pickup. Ensure that the star GameObject now has the Pickup tag.

- Add the following C# Script PickUp to the GameObject star in the Hierarchy:
using UnityEngine; using System.Collections; public class PickUp : MonoBehaviour { public string description; }
- In the Inspector, change the description property of component Pick Up (Script) of GameObject star to the text star:

- Select the GameObject star in the Hierarchy panel and make a copy of this GameObject, renaming the copy heart.
- In the Inspector, change the description property of component Pick Up (Script) of GameObject heart to the text heart. Also, drag the health heart image from the Project panel (in the Sprites folder) into the Sprite property of GameObject heart. The player should now see the heart image on screen for this pickup item.
- Select the GameObject star in the Hierarchy panel and make a copy of this GameObject, renaming the copy key.
- In the Inspector, change the description property of component Pick Up (Script) of GameObject key to the text key. Also, drag the icon_key_green_100 image from the Project panel (in the Sprites folder) into the Sprite property of GameObject key. The player should now see the key image on screen for this pickup item.
- Make another one or two copies of each pickup GameObject and arrange them around the screen, so there are two or three each of star, heart, and key pickup GameObjects.
- Create the following C# Script PlayerInventory in the _Scripts folder.
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; public class PlayerInventory : MonoBehaviour { private PlayerInventoryDisplay playerInventoryDisplay; private List<PickUp> inventory = new List<PickUp>(); void Awake() { playerInventoryDisplay = GetComponent<PlayerInventoryDisplay>(); } void Start() { playerInventoryDisplay.OnChangeInventory(inventory); } void OnTriggerEnter2D(Collider2D hit) { if(hit.CompareTag("Pickup")){ PickUp item = hit.GetComponent<PickUp>(); inventory.Add( item ); playerInventoryDisplay.OnChangeInventory(inventory); Destroy(hit.gameObject); } } }
- Add a UI Text object (Create | UI | Text). Rename it Text-inventory-list. Change its text to the quick brown fox jumped over the lazy dog the quick brown fox jumped over the lazy dog, or another long list of nonsense words, to test the overflow settings you'll change in the next step.
- In the Text (Script) component, ensure that Horizontal Overflow is set to Wrap, and set Vertical Overflow to Overflow. This will ensure that the text will wrap onto a second or third line (if needed) and not be hidden if there are lots of pickups.
- In the Inspector panel, set its font to Xolonium-Bold (folder Fonts) and set its color to yellow. For the Alignment property, center the text horizontally and ensure that the text is top aligned vertically, set the Font Size to 28, and choose a yellow text Color.
- Edit its Rect Transform and set its Height to 50. Then, while holding down Shift + Alt (to set pivot and position), choose the top-stretch box. The text should now be positioned at the middle top of the Game panel, and its width should stretch to match that of the whole panel.
- Your text should now appear at the top of the game panel.
- Add the following C# Script PlayerInventoryDisplay to GameObject player-girl1 in the Hierarchy:
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; [RequireComponent(typeof(PlayerInventory))] public class PlayerInventoryDisplay : MonoBehaviour { public Text inventoryText; public void OnChangeInventory(List<PickUp> inventory) { // (1) clear existing display inventoryText.text = ""; // (2) build up new set of items string newInventoryText = "carrying: "; int numItems = inventory.Count; for(int i = 0; i < numItems; i++){ string description = inventory[i].description; newInventoryText += " [" + description+ "]"; } // if no items in List then set string to message saying inventory is empty if(numItems < 1) newInventoryText = "(empty inventory)"; // (3) update screen display inventoryText.text = newInventoryText; } }
- From the Hierarchy, select the GameObject player-girl1. Then, from the Inspector, access the Player Inventory Display (Script) component and populate the Inventory Text public field with the UI Text object Text-inventory-list.
- Play the game. Each time you pick up a star or key or heart, the updated list of what you are carrying should be displayed in the form carrying [key] [heart].