The previous recipe communicated whether or not the player was carrying a key via text on screen. The use of graphical icons often results in a more engaging GUI.
In the 0423_04_12
folder, you'll find a key icon image, and an empty inventory icon image. In the 0423_04_11
folder, you'll find some key images, and a crisscross image to apply to the terrain.
To display graphical icons for individual inventory objects, please follow these steps:
Cube–key
at position (0, 1, 5) and with scale (2, 2, 2).Cube–key
with the key
string, and tick its IsTrigger checkbox.Cube–key
.// file: PlayerInventoryIcon.cs using UnityEngine; using System.Collections; public class PlayerInventoryIcon : MonoBehaviour { public Texture keyIcon; public Texture emptyIcon; private bool isCarryingKey = false; private void OnGUI() { if( isCarryingKey ) GUILayout.Label( keyIcon ); else GUILayout.Label( emptyIcon ); } private void OnTriggerEnter(Collider hitCollider) { if( "key" == hitCollider.tag ) { isCarryingKey = true; Destroy ( hitCollider.gameObject ); } } }
The two public Texture
variables hold the icons for a key and an empty inventory. The OnGUI()
method uses an if
statement to test the value of isCarryingKey
. If it is true, then a key icon image is displayed, if not, then an icon representing an empty inventory is displayed. The following screenshot shows an example of the statement being true:
18.116.60.62