Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Inventory System just works in Editor Mode not after compiling :-(

Discussion in 'Immediate Mode GUI (IMGUI)' started by DSchaub, May 28, 2019.

  1. DSchaub

    DSchaub

    Joined:
    Oct 12, 2016
    Posts:
    4
    The Inventory System of my Project is completely scripted, so I do not have any GameObjects for colliders or other components.
    Pressing Key "E" opens the GUIwindow with the itemstacks. Each itemstack (or slot) contains an Icon and the number of items per stack. With left MouseClick on a stack to select will Count down the number of items, rightclick puts the selected item back on stack.
    At the Bottom of the GUIwindow I want to display the itemname the Mouse is over. In Editormode it is displayed but after compiling there seems to be no more update of the Window. I have to click on the itemstack to get the itemname displayed ( doesn't matter left or right Mousebutton).
    Here are some code snippets of my Inventory:

    Code (CSharp):
    1. void OnGUI ()
    2. {
    3.         Event m_Event = Event.current;
    4.         if (isOpen)
    5.         {
    6.             GUI.Window(windowId, new Rect(10,10,180,760),InventoryWindow, windowTitle);
    7.             Cursor.lockState = CursorLockMode.None ;
    8.             Cursor.visible = true;
    9.             showItem();
    10.             if (m_Event.type == EventType.MouseDown && Event.current.button == 0)
    11.                 SelectItem();
    12.             if (m_Event.type == EventType.MouseDown && Event.current.button == 1)
    13.                 updateItem();
    14.         }
    15.     }
    Code (CSharp):
    1. void InventoryWindow (int id)
    2.     {
    3.         for (int i=0; i < holder.height; i++)
    4.         {
    5.             for (int j = 0; j < holder.width; j++)
    6.             {
    7.                 Texture2D icon = null;
    8.                 ItemStack itemStack = holder.GetStack(j,i);
    9.                 if (itemStack != null && itemStack.item != null)
    10.                 {
    11.                     icon = itemStack.item.itemIcon;
    12.                 }
    13.                 GUI.Box(new Rect(j * BOX_WIDTH + OFFSET_SIDES, i * BOX_HEIGHT + OFFSET_TOP, BOX_WIDTH, BOX_HEIGHT), icon);
    14.                
    15.                 if (icon != null)
    16.                     GUI.Label(new Rect(j * BOX_WIDTH + OFFSET_SIDES + 5, i * BOX_HEIGHT + OFFSET_TOP + 2, BOX_WIDTH, BOX_HEIGHT), itemStack.itemCount.ToString());
    17.             }
    18.         }
    19.     }
    Code (CSharp):
    1. void showItem()
    2. {
    3.     int x = -1;
    4.     int y = -1;
    5.     GetMousePos( ref x, ref y);    // x and y are now positions of the GUI.Box the mouse is over e.g. 0,2 or 1,7
    6.     if ( x>=0 && x<holder.width && y>=0 && y<holder.height)
    7.     {
    8.         guitooltip="";
    9.         if (holder.GetStack(x,y) != null)
    10.         {
    11.             dragSack=holder.GetStack(x,y);
    12.             guitooltip=dragStack.item.itemName.toString();
    13.         }
    14.     }
    15. }
    The variable guitooltip in function showItem() keeps the itemname. I tried to display it by using GUI.Label and GUI.Box and also with a GameObject Textfield outside the GUI. But nothing updated the itemname like in Editormode.
    Is there any possibility to get the itemname updated? Why does it work in EditorMode and not after compiling?