- Home /
GUI.Window Not Appearing on button click
I have a scroll area of boxes with buttons and I would like to click on the view button and open a Gui.Window. The view button is registering the click, but is never getting into the openItemWindow() function and the GUI.Window isn't opening either. Any help would be appreciated.
void AutoItemLayout() { GUI.enabled = windowNotOpen; GUI.BeginGroup(new Rect(0, 0, SHOP_WIDTH, SHOP_HEIGHT)); GUI.Box(new Rect(0, 0, SHOP_WIDTH, SHOP_HEIGHT), "");
category = GUI.Toolbar(new Rect(5, 5, SHOP_WIDTH - 10, 85), category, categoryName);
GUI.Box(new Rect(5, 95, (SHOP_WIDTH - 10), (SHOP_HEIGHT - 100)), "");
scrollViewVector = GUI.BeginScrollView(new Rect(10, 100, (SHOP_WIDTH - 20), (SHOP_HEIGHT - 110)), scrollViewVector, new Rect(0, 0, 0, scrollHeight));
if (category != currentCategory)
{
switch (category)
{
case 0:
displayItems = ShopManager.Instance.clothes;
currentCategory = 0;
break;
case 1:
displayItems = ShopManager.Instance.pencilPlayground;
currentCategory = 1;
break;
case 2:
displayItems = ShopManager.Instance.music;
currentCategory = 2;
break;
case 3:
displayItems = ShopManager.Instance.zopet;
currentCategory = 3;
break;
}
setUpShopLayout();
}
for (int i = 0; i < itemBoxPositions.Count; i++)
{
GUI.Box(itemBoxPositions[i], displayItems[i]);
GUI.Box(new Rect(5 + itemBoxPositions[i].x, 20 + itemBoxPositions[i].y, itemBoxPositions[i].width - 10, itemBoxPositions[i].height - 50), "");
GUI.Label(new Rect(5 + itemBoxPositions[i].x, itemBoxPositions[i].height - 25 + itemBoxPositions[i].y, itemBoxPositions[i].width / 2 - 10, 20), "Price");
if (GUI.Button(new Rect(itemBoxPositions[i].width / 2 + itemBoxPositions[i].x, itemBoxPositions[i].height - 25 + itemBoxPositions[i].y, itemBoxPositions[i].width / 2 - 10, 20), "View"))
{
Debug.Log("View " + i + " got clicked.");
windowRect = GUI.Window(0, windowRect, openItemWindow, name);
}
}
GUI.EndScrollView();
GUI.EndGroup();
GUI.enabled = true;
}
public void openItemWindow(int id) { Debug.Log("In openItemWindow(" + id + ")"); if (GUI.Button(new Rect(windowRect.x + 5, windowRect.y - 25, 10, 20), "BUY")) { ShopManager.Instance.buyItem(currentCategory, displayItems[id]); Debug.Log(InventoryManager.Instance.playerInventory.Count); Debug.Log(InventoryManager.Instance.playgroundInventory.Count); itemBoxPositions.RemoveAt(itemBoxPositions.Count - 1); setUpShopLayout(); } }
Thanks, Hans Unity Newbie
Answer by HHameline · Apr 29, 2011 at 02:34 PM
I added a boolean variable (render) and in the OnGUI() function I added
if (render)
{
openItemWindow(0);
}
then under the view button I put
if (GUI.Button(new Rect(itemBoxPositions[i].width / 2 + itemBoxPositions[i].x, itemBoxPositions[i].height - 25 + itemBoxPositions[i].y, itemBoxPositions[i].width / 2 - 10, 20), "View"))
{
render = true;
}
I still need to tinker a little with it but for my immediate purposes that seems to be working.
public void openItemWindow(int id) { windowNotOpen = false; windowRect = GUI.Window(id, windowRect, itemWindow, displayItems[id]); }
public void itemWindow(int id) {
if(GUI.Button(new Rect(windowRect.width - 30, windowRect.y - 50, 30, 30), "X")) { render = false; } }
That's working perfectly now.
Thanks for the insight
Answer by Uzquiano · Apr 29, 2011 at 02:15 PM
Hi,
I always do it uisng GUILayout.Button(...) in your openItemWindow(). Is it a big issue for you to change that?
That would cause quite a bit of grief actually. I did some tinkering and might have found a solution which I will post as an answer so I can properly display the code.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Destroy a Game object on collision and add it to my Gui??? 1 Answer
PasswordField help. 1 Answer
Two Unity GUI questions 2 Answers