- Home /
Inventory GUI Box click
Hey guys,
I try to create an inventory system for my game and everything works so far. But there ist still a problem left! I draw my inventory by:
C#:
public void OnGUI() {
GUI.skin = skin;
if(draw) {
int i = 0;
for(int x = 0; x < slotsY; x++) {
for(int y = 0; y < slotsX; y++) {
GUI.Box(new Rect(y * 42, x * 42, 40, 40), inventory[i].itemName, skin.GetStyle("slot"));
GUI.DrawTexture(new Rect(y * 42, x * 42, 40, 40), inventory[i].texture);
i++;
}
}
}
}
and now I want to know how to listen for a click on the boxes and how to find out which box / slot was clicked.
Hope you can help me! Thanks in advance
You can also do this with the 4.6 beta. The general philosophy is quite different. Check out my tutorial here if you are interested.
Answer by Seneral · Nov 21, 2014 at 10:49 PM
Use GUI.Button instead, using your icon texture: http://docs.unity3d.com/ScriptReference/GUI.Button.html You might even want to differ between left/right click, so heres what you might end up with:
if (GUI.Button(new Rect(y * 42, x * 42, 40, 40), inventory[i].itemName, inventory[i].texture)
{
if (Event.current.button == 0)
UseItem(inventory[i]);
else if (Event.current.button == 1)
ShowItemInfo(inventory[i]);
}
Have a great day and good coding!
-Seneral
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
adding inventory items is half-working 0 Answers
Help with "inventory" system c# 1 Answer
Find which part of an image was clicked 3 Answers