- Home /
OnMouse menu
I have an idea for a menu where when you click on a button a bunch of other buttons appears:
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
{
//Do stuff
}
}
But I want it so that if you click again outside of the buttons the menu closes. I've tried:
private void OnMouseExit()
{
if (Input.GetMouseButtonDown(0))
{
//Do stuff
}
}
But this doesn't work as it only registers when the mouse leaves the object and nothing else.
I'm not sure if this works in Unity but the control might have an event handler for when it loses focus? e.g. LostFocus()
$$anonymous$$aybe but how is it different from On$$anonymous$$ouseExit()?
Answer by highpockets · Dec 08, 2020 at 10:05 AM
I think you need to store a state somewhere. This can be done simply with a bool:
bool isMouseOver = false;
void OnMouseEnter()
{
isMouseOver = true;
}
void OnMouseExit()
{
isMouseOver = false;
}
void Update()
{
if(!isMouseOver)
{
//Mouse is ooff the object
}
}
Yes I tried this but there are two problems with this:
void Update() { if(Input.Get$$anonymous$$ouseButtonDown(0) && !is$$anonymous$$ouseOver) { Debug.Log("d"); } }
The first problem is that eventhough I click once the Log message shows up 2-3 times in the console. I don't know why this is. The second problem is that if I try to disable the buttons like this: void Update() { if(Input.Get$$anonymous$$ouseButtonDown(0) && !is$$anonymous$$ouseOver) { //Disable menu buttons } }
Then they never appear. I don't think there's any way to do this using On$$anonymous$$ouseExit at all.
The function Get$$anonymous$$ouseButtonDown() should return true during the first frame that the button is pressed. If you are getting multiple log messages, maybe you have more than one instance?? I would really have to see more of your logic to see what’s happening here. There will certainly be a reasonable explanation to why you aren’t getting the expected results
private void On$$anonymous$$ouseExit()
{
is$$anonymous$$ouseOver = false;
}
private void On$$anonymous$$ouseOver()
{
if (Input.Get$$anonymous$$ouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
{
is$$anonymous$$ouseOver = true;
//Enable buttons
}
else if (Input.Get$$anonymous$$ouseButtonDown(0) && !is$$anonymous$$ouseOver)
{
//Disable buttons
}
}
I converted your answer to a comment because it’s clearly not an answer. This logic doesn’t seem to make much sense. I think this should be more or less what is needed.
private void On$$anonymous$$ouseEnter()
{
is$$anonymous$$ouseOver = true;
}
private void On$$anonymous$$ouseExit()
{
is$$anonymous$$ouseOver = false;
}
private void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
if(is$$anonymous$$ouseOver && !button.gameObject.activeInHierarchy)
{
//Enable buttons
}
else if(!is$$anonymous$$ouseOver && button.gameObject.activeInHierarchy)
{
//Disable buttons
}
}
}
else if (!is$$anonymous$$ouseOver && EventSystem.current.IsPointerOverGameObject())
{
//Disable buttons
}
This is the part that doesn't activate.