- Home /
GUI focus control - inventory system issue
Hello everyone, I'm trying to build a game with only keyboard controllers so I wanna create an inventory where you can use arrow keys to "jump" from a box to another. After I created the base for my inventory, it looks like this (without appropriated graphic, ofc ahaha)
The problem is I don't have any idea how to assign a decent GUI.SetNextControlName, because (for what I understood by debug) I can't use it in a for loop.
So, I have these
private Rect invRect;
private string[] invBoxes;
private int boxIndex;
That's the OnGUI part
void OnGUI(){
GUI.skin=skin;
if(showInventory){
invRect=GUI.Window(0,invRect,DrawInventory,"");
GUI.FocusControl(invBoxes[boxIndex]);
if(Input.GetKey(KeyCode.Escape))
showInventory=false;
InventoryInput();
Debug.Log (GUI.GetNameOfFocusedControl());
}
}
And, finally, this is the DrawInventory method
void DrawInventory(int windowID){
//[...]things not related to the problem
int i=0;
int BoxIndex=0;
GUIStyle invBox="BaseBox";
invBox.fixedHeight=invRect.height/18;
invBox.fixedWidth=invRect.width/2.05f;
for(int x=0;x<slotsX;x++){
GUILayout.BeginHorizontal();
for(int y=0;y<slotsY;y++){
GUILayout.BeginVertical();
GUILayout.Box(inventory[i].ItemIcon, invBox);
GUI.SetNextControlName("Box"+(BoxIndex));
GUI.Box(GUILayoutUtility.GetLastRect(), inventory[i].ItemIcon, invBox);
invBoxes.SetValue("Box"+BoxIndex,BoxIndex);
GUIStyle textGUI="ObjectName";
textGUI.fontSize=(int)(invRect.height/22);
GUI.Box(GUILayoutUtility.GetLastRect(), " "+inventory[i].Name,"ObjectName");
if(inventory[i].IsStackable&&inventory[i].ItemCounter>1){
GUIStyle quantityGUI="ItemCounter";
quantityGUI.padding.right=(int)(invRect.width/50);
quantityGUI.fontSize=(int)(invRect.height/22);
GUI.Box(GUILayoutUtility.GetLastRect(), "x"+inventory[i].ItemCounter.ToString(),"ItemCounter");}
BoxIndex++;
GUILayout.EndVertical();
i++;
}
GUILayout.EndHorizontal();
}
GUIStyle DescrBox="DescriptionBox";
DescrBox.fontSize=(int)(invRect.height/22);
GUILayout.Box("-Select an item-","DescriptionBox");
}
I've started to develop in unity 2 weeks ago, so if the code is messy, pardon ahaha I've also used a couple of tutorials to understand better some aspects. Btw, I've created a GUILayout.Box and after that a GUI.Box because (I think) that the GUI.SetNextecc. method doesn't consider the first option, so I just used it for the coords.. And all seems to work, BoxIndex increments itself properly, but
GUI.SetNextControlName("Box"+(BoxIndex));
always remains "Box0". I've tried to edit my boxIndex (with the lowerCase) but it returns an empty string and not "Box1", so it's pointing to nothing.
Am I getting wrong something? Does another method exist to achieve this issue?
Thank you :)
Answer by $$anonymous$$ · Jan 21, 2015 at 11:40 AM
I solved it switching GUI.Box to GUI.Button. GUI.SetNextControlName doesn't work on Boxes ^^