// 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
}
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....
}
}
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
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.