How to Detect The Mouse is NOT On a GUI Layout

Hello i got a problem with GUILayout.
This Code Works :

                if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
                {
//mouse is on the guilayout
                }

But it doesn’t :

                if (!GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
                {
//mouse is not on the guiLayout
                }

Please Answer my Q.

Thanks.

hey guy

may be true way :

// define variable
bool flag = false ;

 if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))        flag = true ;

else  flag = false ;

if(flag)
{
//Mouse is on the GUILayout
}
else 
{
//Mouse is not on the GUILayout
}

I’m sry, it didn’t work :frowning:

    Rect layoutRect = new Rect(0, 0, 100, 100);
    void OnGUI()
    {
        GUILayout.BeginArea(layoutRect);
            // Components in layout...
        GUILayout.EndArea();

        if (layoutRect.Contains(Event.current.mousePosition))
        {
            // Your code....
        }
    }

The Problem is it in a Scroll View Sorry i didn’t say about that. :

The Correct Code Is this But It print beep when the mouse is over the first element .

        for (int i = 0; i < Objects.Count; i++)
        {
            Rect layoutRect = new Rect(40, 20 + 30 * i, 150, 180);

            GUILayout.BeginArea(layoutRect);

            GUILayout.Box(Objects[i].name);

            GUILayout.EndArea();

            if (OnGUIOver( layoutRect))
            {
                print("beep");
            }
        }
    bool OnGUIOver(Rect rect)
    {
        Vector2 mousePos = Event.current.mousePosition;

        return (rect.Contains(mousePos));
    }

What exactly you want? a scrollview that have inside buttons, labels?

If you want make a clickable label then you can create button instead of label, and set GUIStyle to your custom-defined GUIStyle were you will put only that you want(text color, font, fontSize).

The good with buttons is that you don’t need to create array with theyr position to detect when mouse is over or when mouse is clicking.

This code below is a small example.

using UnityEngine;
using System.Collections;

public class CV : MonoBehaviour 
{
    public Font myFont;
    GUIStyle gStyle;
    Vector2 svPos = Vector2.zero;

    void Start()
    {
        gStyle = new GUIStyle();
        gStyle.font = myFont;
        gStyle.fontSize = 15;
        gStyle.normal.textColor = Color.white;

    }

    void OnGUI()
    {
        string[] str = { "One", "Two", "Three" };

        svPos = GUI.BeginScrollView(new Rect(0, 0, 400, 300), svPos, new Rect(0, 0, 400, 500));
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (GUI.Button(new Rect(0, i* 50, 100, 30), "Button " + i, gStyle))
                    print("You clicked Button " + i);
            }

        }
        GUI.EndScrollView();
    }

}

No i don’t want a button i want this :
if the mouse is over a Box show box1 that you can do something in that and if the mouse is not over that Box Hide the Box1;
Please Help

Try This.

Rect box = new Rect(200,0,200,20);
.
.
.
void OnGUI()
{
     for(int i = 0; i < 5; i++)
     {
           bool can = false;
           box.y = i*30;
           GUILayout.BeginArea(box);
           GUILayout.Box(i.ToString());
           GUILayout.EndArea();
           if(box.Contain(Event.current.mousePosition))
           {
                  can = true;
           }
           if(can)
               // The Work You Want to do here
     }
}

The problem might just be in Event.current.mousePosition being in screen coordinates, and GUI.Button bounds being in coordinates local to ScrollView.

GUI systems solve this problem by having conversion methods - LocalToGlobal and GlobalToLocal, converting points between coordinate systems prior to the actual check.