I am trying to implement an inventory system in unity3d. They are several ways to realize an inventory on gui but i've chosen the easiest.
Every item on gui is represented as a cell(GUI.Button). Every empty cell means empty place in inventory(GUI.Box). When I press one button to chose an item all buttons are getting focused.
My question is why it happens and what to fix to make only one button to be focused? I created a GUISkin based on empty one. Thank you for attention
Here is the code to show an inventory.
void OnGUI(){
if (visible) {
GUI.skin = skin;
GUI.Window(0,new Rect((Screen.width-1024)/2,0,1024,600),InventoryBody,"Inventory");
}
}
void InventoryBody(int id){
GUIStyle style = new GUIStyle();
if (currentItem) {
GUI.DrawTexture (new Rect(550f,70f,80f,80f), currentItem.texture);
GUI.color = Color.red;
GUI.Label (new Rect(700f,50f,400f,300f), currentItem.name);
GUI.color = Color.black;
string desc = "Description: "+currentItem.description;
style.wordWrap=true;
style.fontSize=18;
GUI.Label (new Rect(650f,100f,300f,500f), desc,style);
if(GUI.Button(new Rect(700f, 290f, 150f,50f), "Cancel")) {
currentItem = null;
}
if(GUI.Button(new Rect(700f, 230f, 150f,50f), "Use")) {
currentItem.Use ();
}
}
//1st column
GUILayout.BeginArea (new Rect (80f,60f,600f,600f));
for (int i = 0; i<5; i++) {
if(items[i]!=null){
if(GUILayout.Button (items [i].texture, GUILayout.Width (80f), GUILayout.Height (80f)) ){
currentItem = items[i];
}
} else {
GUILayout.Box("", GUILayout.Width(80f),GUILayout.Height(80f));
}
}
GUILayout.EndArea ();
//2nd column
GUILayout.BeginAr...
}
Copyright Notice:Content Author:「Rinomancer」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30768232/press-one-button-focus-all-buttons