- Home /
Add a GUIBox if Key picked up
Hello! I have found a nice script for picking up keys, but now I wonder if there is a way to add a blue texture or icon in the upper richght corner if I pick up the blue key for example. Here is the script: using UnityEngine; using System.Collections;
public class Key : MonoBehaviour
{
public enum KeyType
{
Blue,
Green,
Yellow,
None
}
public KeyType type;
public AudioClip audioClip;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
Inventory inventory = other.transform.GetComponent<Inventory>();
inventory.keys.Add(type);
Destroy(gameObject);
}
}
}
And here is the inventory script: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Inventory : MonoBehaviour
{
public List<Key.KeyType> keys;
}
Has anybody an idea if it is possible to get this working?
Best regards
Answer by samtperrin · Oct 19, 2013 at 01:57 PM
You could instantiate a GameObject that is a GUITexture or has a GUI script attached. When you want to stop displaying that blue texture icon just destroy the GameObject.
Answer by RyanZimmerman87 · Oct 19, 2013 at 10:16 PM
Sounds like a basic how do I use GUI question? You could also use other methods but for something like this I always use OnGUI.
Code Example:
//I don't know your game logic/assets so all these are just basic examples
//easily format and set up all the GUI stuff with this.
public GUIStyle keyStyle;
public Texture2D keyIcon1Texture;
public Texture2D keyIcon2Texture;
//etc..
//matrix to easily work with onGUI for all screen sizes
vector3 matrixVector;
//position and size for key icon
vector2 keyIconPositionVector = new Vector2 (1180, 0);
vector2 keyIconSizeVector = new Vector2(100, 100);
//you can use a different method to set GUI for the screen size
//this method allows automatic formatting to any screen size
//useful for mobile devices
float native_width = 1280;
float native_height = 720;
float rx;
float ry;
int keyInt;
void Start()
{
float rx = Screen.width/native_width;
float ry = Screen.height / native_height;
matrixVector = new Vector3 (rx, ry, 1);
//sets the size of key icon texture to scale with matrix
keyIconSize.x = keyIconSize.x * rx;
keyIconSize.y = keyIconSize.y * ry;
keyInt = 0;
//or if you want to save keys
keyInt = PlayerPrefs.GetInt("Key Int", 0);
}
void OnGUI()
{
if (keyInt == 0)
{
return;
}
else if (keyInt != 0)
{
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, matrixVector);
//blue key
if (keyInt == 1)
{
//add blue texture icon to top right
GUI.Label (new Rect(keyIconPositionVector.x, keyIconPositionVector.y, keyIconSizeVector.x, keyIconSizeVector.y), keyIcon1Texture, keyStyle);
}
//red key
else if (keyInt == 2)
{
//add red texture icon to top right
GUI.Label (new Rect(keyIconPositionVector.x, keyIconPositionVector.y, keyIconSizeVector.x, keyIconSizeVector.y), keyIcon2Texture, keyStyle);
}
//etc..
}
}
Your answer
Follow this Question
Related Questions
how can i get GUI.Button presse(or down not click) 3 Answers
Where do I assign GUI scripts? 1 Answer
Intro GUI Text Script... 3 Answers
GUI Resolution Ajust 1 Answer
Why does my GUI health bar move sideways when it I lose health? 1 Answer