How to set a GUI.Box "OnMouseEnter" effect

void OnGUI()
{
GUI.Box(RectABC, “”);
}
When mouse enter I want it print(“enter”),and when mouse leave I want to print(“leave”).
What should I do?Thx!

Make use of GUI.tooltip

sorry,gui.tooltip can’t help me

Isn’t your goal to have a message displayed on the screen when your mouse enters the button ?

There is currently no way to do it as a function of GUI.Box; however, as long as you know the rect you can easily do it with:

And

Event.current.MousePosition (or if event.current is not appropriate for your case, you can use Input.mousePosition - but with input.MP you have to invert the “y” location).

If you need any more help, let me know.

sorry~

if(RectABC.Contains(MousePosi))
  DoSth();

This is not what I want.Because it will DoSth() every frame.I want DoSth() ONLY happens when mouse enter/leave this GUI.Just like event/delegate in C#, CallBack in C++;

No,my goal is not to display a message But to call a function.

Viskag, i don’t know if works what i’m going to tell you for a GUIbutton, but it does for GUItext: attaching a collider and selecting it as a trigger; you could use OnTriggerEnter.

The problem is that i never use a GUI to make a button and, of course, i don’t know if you could attach a collider O_o

EDITED::::::::::::::::::::::::::::::::::
CHEK THIS OUT!

Returns true if the x and y components of point is a point inside this rectangle.
If mouse’s X and Y are contained, you can call a function.

Crazy idea with lots of hypothesis :

GUI.tooltip contains a string. It should be empty when your mouse is over nothing.

You can define what the tooltip is when your mouse is over a GUI element.

Why don’t you make a test function in Update that checks for the content of GUI.tooltip ?

When its contents becomes something, you call a precise function.

Edit : Well, I overlooked the lower part of the page.

Seems my idea was correct !

If you have not found a solution yet, something like this should work:

bool _mouseOverButton = false;
Rect _buttonRect = new Rect(0, 0, 100, 100);

if (GUI.Button(_buttonRect, buttonContent)){
     // Do what you want to do when you click this button
}

if (_buttonRect.Contains(MPosition)){
     if (!_mouseOverButton){
          DoMouseEnter();
          _mouseOverButton = true;
     }
} else {
     if(_mouseOverButton){
          DoMouseExit();
          _mouseOverButton = false;
     }
}

It’s a convoluted solution - but it should work exactly how you want it to. When you mouse over the rect it should trigger DoMouseEnter() only once, and when you mouse out it should trigger (DoMouseExit) only once.