- Home /
Inventory GUI
Hello,
I am trying to create an inventory system and GUI using UnityGUI.
So far I have an array of invetory Items witch contains GUIContent objects with a texture and name of each object. I first tried to use a SelectionGrid but that didn't display the correct selection and I was trying to make it functionate so if I left click on a button I consume the item and if I right click on the item I drop it.
I am now trying to do this by creating first a group and in side of it having smaller groups in which I display the texture and the text. I save the smaller groups rectangles and try to use those to check if I click inside them, but the clicks don't register correctly, I initially don't get anything and then I receive a bunch of random click messages.
The following code is what I use to display the gui and check for clicks, this is called in an OnGUI() function.
public void drawInventoryGUI(int windowId)
{
GUI.FocusWindow(windowId);
GUI.BeginGroup(new Rect(windowPosition.x,windowPosition.y,windowSize.x,windowSize.y));
if(!checkNoInventory()){
scroll = GUI.BeginScrollView(new Rect(scrollPosition.x,scrollPosition.y,scrollSize.x,scrollSize.y),scroll,new Rect(0,0,scrollSize.x,enlargeFactorScrollEquipment * inventoryItem.Length/xGrid));
//clickedItem = GUI.SelectionGrid(new Rect(gridPosition.x,gridPosition.y,gridSize.x,enlargeFactorGridEquipment * inventoryItem.Length/xGrid),clickedItem,inventoryItem,xGrid);
drawRectGUI();
GUI.EndScrollView();
}
GUI.EndGroup();
}
private void drawRectGUI()
{
buttonPosition = new Rect[inventoryItem.Length];
int i=0;
int j=0;
GUI.BeginGroup(new Rect(gridPosition.x,gridPosition.y,gridSize.x,enlargeFactorGridEquipment * inventoryItem.Length/xGrid));
foreach(GUIContent item in inventoryItem)
{
if(i%5 == 0)
{
j++;
i = 0;
}
buttonPosition[i] = new Rect(i*100,j*100,60,90);
GUI.BeginGroup(buttonPosition[i]);
GUI.DrawTexture(new Rect(0,0,50,50),item.image);
GUI.Label(new Rect(0,55,50,10),item.text);
GUI.EndGroup();
//Debug.Log(buttonPosition[i].ToString());
i++;
}
GUI.EndGroup();
for(int k=0 ; k< buttonPosition.Length ; k++)
{
if(Event.current.isMouse && buttonPosition[k].Contains(Event.current.mousePosition))
{
Debug.Log("Clicked on button " + k);
}
}
}
Does anybody have any idea on how to do it?
http://answers.unity3d.com/page/newuser.html
a) Asking someone to write some script for you
Your answer
Follow this Question
Related Questions
Emulate RightArrow GUI Event 0 Answers
How to add UnityEvent using Custom Inspector? 1 Answer
OnGUI Event 0 Answers
EditorWindow not properly drawing 1 Answer