1
\$\begingroup\$

So I am drawing an inventory with slots like so:

private void DrawItemSlotAndAttachActions(Item currentItem, Rect slotRectangle, Event e, int itemSlotPosition)
    {
    if (!(currentItem is EmptyItem))
        {

            GUI.depth = 0;
            GUI.DrawTexture(slotRectangle, currentItem.Icon);
            if (slotRectangle.Contains(e.mousePosition))
            {
                _tooltip = currentItem.CreateTooltip();
                _showTooltip = true;

                DragAndDropItem(e, itemSlotPosition);
                UseConsumable(e, currentItem);
            }
        }
        else
        {
            DropItemOnEmptySlot(slotRectangle, e, itemSlotPosition);
        }
    }

    public void OnGUI()
    {
        _tooltip = ""; //reset every time...can this be done better?
        GUI.skin = Skin;

        DrawItemSlotAndAttachActions(.....);
        ShowTooltip();
    }

    private void ShowTooltip()
    {
        if (_showTooltip)
        {
            GUI.Box(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 200, 150), _tooltip,
                Skin.GetStyle("Tooltip"));
        }
    }

    private void HideToolTipWhenEmpty()
    {
        if (string.IsNullOrEmpty(_tooltip))
            _showTooltip = false;
    }

In here I have the problem that when I hoven over an item, all the boxes that are drawn after are on top of my tooltip Box.

How can I make sure that the box is drawn on top of all the rest?

Screenshot:

Screenshot

As you can see, it is still behind the rest of the buttons.

\$\endgroup\$
4
  • \$\begingroup\$ I assume you have good reasons for sticking to the legacy UI instead of the new UI. \$\endgroup\$
    – SanSolo
    Oct 26, 2015 at 10:16
  • \$\begingroup\$ Since I am new to Unity, mind explaining what you mean by "new" UI? \$\endgroup\$
    – Tikkes
    Oct 27, 2015 at 8:11
  • 1
    \$\begingroup\$ This ONGUI method of creating Unity is old one. With Unity 4.6, we have new UI feature. In the learn section of Unity3D website, you will find tutorials about it. Main benefit is you don't place the elements through code. You create your UI in the editor itself. unity3d.com/learn/tutorials/modules/beginner/… \$\endgroup\$
    – SanSolo
    Oct 27, 2015 at 8:21
  • \$\begingroup\$ dont use unity's old onGui, its a bad practice to draw UI elements in runtime. \$\endgroup\$ Jun 25, 2016 at 21:22

1 Answer 1

1
\$\begingroup\$

Look into GUI.depth

The sorting depth of the currently executing GUI behavior. It's the equivalent of CSS's z-index.

Set this to determine ordering when you have different scripts running simultaneously. GUI elements drawn with lower depth values will appear on top of elements with higher values (ie, you can think of the depth as "distance" from the camera).

Your box should have the lowest depth value.

// slots
GUI.depth = 0;
GUI.DrawTexture(...);

// box; to be rendered on top of the slots
GUI.depth = -1;
GUI.DrawTexture(...);
\$\endgroup\$
7
  • \$\begingroup\$ But this requires me to have an OnGUI on all my slots right? And then pushing them all back. Is there a way to do it on the fly? Because I want to be able to only draw the box on mouseovers. \$\endgroup\$
    – Tikkes
    Feb 25, 2015 at 10:04
  • \$\begingroup\$ You should be able to use GUI.depth in your case as well. Check my updated answer. \$\endgroup\$ Feb 25, 2015 at 10:22
  • \$\begingroup\$ So I did it as you suggested, I'll update my question with a screenshot. \$\endgroup\$
    – Tikkes
    Feb 25, 2015 at 10:28
  • \$\begingroup\$ Does that box has higher depth than all the rest? Have you tried drawing it last? \$\endgroup\$ Feb 25, 2015 at 10:41
  • \$\begingroup\$ drawing it last does work, thank you. But this makes me put my string of tooltip which I use to check whether or not to hide the tooltip to be put back to an empty string every time in OnGUI. Is there a better way to go about this? See my question for updates. Also why does the depth not work as you suggest? I tried it alot. \$\endgroup\$
    – Tikkes
    Feb 25, 2015 at 11:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .