Forum rules - please read before posting.

Register mouseclick in menu over script

Hello!
I currently have a script, that adds an object when my mouse performed a right-mouse-button-click (Input.GetMouseButtonDown(0)). The problem now is, that if any menu is enabled and i navigate trough the menus, the click is still registered and an object gets added. How can I check in my script, if the click was performed while the menu is on? I already tried PlayerMenus.GetMenuWithName("NAME").IsEnabled() but it doesnt work.
Also, the click seems only to be registered, if I click a menu-button that turns off the menu and starts another or disables the menu and goes back to the game.
Does anyone have an idea how to fix that?

Comments

  • Welcome to the community, @Pluh.

    How can I check in my script, if the click was performed while the menu is on?

    Calling a Menu's IsEnabled() or IsOn() function should reveal if it's currently on.

    However, to determine if the mouse is currently over the menu itself, you can read its IsPointInside() function:

    Vector2 invertedMouse = KickStarter.playerInput.GetInvertedMouse ();
    Menu menu = PlayerMenus.GetMenuWithName ("MyMenu");
    if (menu.IsOn () && menu.IsPointInside (invertedMouse))
    {
        // Mouse is inside menu, do nothing
        return;
    }
    

    If you want to check if the mouse is over any menu, you can avoid checking each menu individually and just read:

    if (KickStarter.playerMenus.IsMouseOverMenu ())
    {
        return;
    }
    

    If your Menu relies on Unity UI instead of AC's built-in menu rendering (see the "Source" field in the Menu's list of properties), then you can also just read Unity's EventSystem's IsPointerOverGameObject() function:

    if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ())
    {
        return;
    }
    

    Also, the click seems only to be registered, if I click a menu-button that turns off the menu and starts another or disables the menu and goes back to the game.

    I'm not sure I follow this too well - can you elaborate on this with screenshots?

  • Hello Chris!
    First, thank you for your super fast reply und also sorry for my late reply.
    Actually I fixed my problem with pausing my script. Whenever I open my menu, I just call a function in my script that sets in on pause and if I press my resume-button my script is continued.
    I also already tried the IsMouseOverMenu function and it doesnt work. How I see it, Unity first executes the action list behind my menu-buttons and then my script. In that case the menu is always deactivated for my script.

  • If you want to run custom code before a Menu Button's ActionList is run, you need to hook into the OnMenuElementClick custom event, run your own code, and then trigger the ActionList asset through script.

    If you set the Button element's Click type to Custom Script, that'll prevent the ActionList from being run automatically. You can then rely on a custom script similar to the one below to handle your intended order of things:

    using UnityEngine;
    using AC;
    
    public class MenuClickExample : MonoBehaviour
    {
    
        public ActionListAsset assetOnClick;
        public string menuName = "MyMenu";
        public string buttonName = "MyButton";
    
        private void OnEnable () { EventManager.OnMenuElementClick += OnMenuElementClick; }
        private void OnDisable () { EventManager.OnMenuElementClick -= OnMenuElementClick; }
    
        private void OnMenuElementClick (Menu menu, MenuElement element, int slot, int buttonPressed)
        {
            if (menu.title == menuName && element.title == buttonName)
            {
                // Run custom code here, then:
    
                if (assetOnClick) assetOnClick.Interact ();
            }
        }
    
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.