Reputation: 61
I want to be able to hover over a GameObject (agent) and on either right or left click, create a floating menu similar to the windows right click menu. I have tried using a combination of OnGUI() and OnMouseOver() but I either don't get the behaviour I need or get nothing at all. Here is what I have so far:
private void OnMouseOver()
{
mouseOver = true;
mousePos = Input.mousePosition;
}
private void OnMouseExit()
{
mouseOver = false;
}
private void OnGUI()
{
if (mouseOver && Input.GetMouseButtonDown(0))
{
GUI.Box(new Rect(mousePos.x, mousePos.y, 200f, 100f), "this is a test");
}
}
mouseOver and mousePos are initialy set to false.
Upvotes: 4
Views: 2415
Reputation: 61
Input.GetMouseButtonDown() only executes for a single frame so the GUI.Box is being drawn but only for a single frame.
To fix this Input.GetMouseButton() can be used instead as it executes for as long as the button is pressed.
Upvotes: 2
Reputation: 1963
What I do is create my desired menu in a Canvas, add an animator controller and an animation and put it outside of the camera view (the animation moves the menu inside the camera view), finally add a bool condition trigger to hide or show the menu
Then i just attach a click function into a button to fire the animation when somebody clicks it
public class ShowMenu : MonoBehaviour
{
public Animator ObjectBtnAnimations;
public void HideObjControls()
{
if (ObjectBtnAnimations.GetBool("hide"))
{
ObjectBtnAnimations.SetBool("hide", false);
}
else
{
ObjectBtnAnimations.SetBool("hide", true);
}
}
}
Upvotes: 0