Forum rules - please read before posting.

Automatically Close Inventory Menu After Activating It With a Button

edited June 2015 in Technical Q&A
I have a fixed icon in the bottom of the game screen of a suitcase. Clicking this brings up the standard inventory bar/menu at the top which is ideal and what I want.

However, how can we have it so that when the mouse cursor leaves the inventory bar, it automatically closes (like originally). At the moment there's just no way of closing it.

I have the inventory menu's appear type set to 'Manual'.

«1

Comments

  • Try setting the Inventory bar's Appear type to Mouse Over, but locked on start.  Then when you click on the icon, use the Menu: Change state Action to unlock it, rather than turn it on.  This will cause the inventory bar to appear so long as the mouse is over it.

    Then assign an ActionList into the Inventory bar's "ActionList when turn off" field, which uses the same action to lock it again.  This will mean that the inventory bar won't appear again until you re-click the icon.
  • Hi Chris, thanks, but the problem is that the Inventory bar is at the top of the window and the button to turn it on is at the bottom, hence the inventory menu doesn't appear as the cursor is off it once the button's clicked.
  • In that case, try having an "invisible" menu that covers the same space as the inventory menu, with an Appear type of Mouse Over.  Set the Inventory's Appear type to During Gameplay, and then assign the invisible menu's ActionList when turn off to an Action that locks the Inventory menu again.
  • Can I get some clarification on this from someone please?
    As per the original post, I want my inventory to appear when the player pushes the "inventory open" button. The inventory is on the other side of the screen to the button.
    I'm trying to wrap my head around how the invisible menu helps here. What unlocks the (non invisible) inventory menu? And what does the "inventory open" button action list contain?
  • And you also want the Inventory menu to disappear when the mouse leaves it?

    AC has improved a lot since this thread was started, and the "cleanest" way to achieve this is probably with custom events, which allow you to modify menu behaviour at runtime.

    Try this:
    http://pasteall.org/1045635/csharp

    It's a custom script that dynamically changes the Inventory menu's Appear type between Mouse Over and During Gameplay.  To use it:
    1. Have two Menus, InventoryOpen (at e.g. the bottom) and Inventory (at e.g. the top).  No need for a third "invisible" one.
    2. Set the Inventory menu's Appear type to During Gameplay, and check Start game locked off?.
    3. Have the InventoryOpen menu include a Button that calls a Menu: Change state Action to unlock the Inventory menu.
    4. Attach the script as a component on a GameObject in the scene
    When the InventoryOpen button unlocks the Inventory menu, the Menu turns on because its Appear type is set to During Gameplay.  When the mouse then hovers over it, it changes to Mouse Over.  When the mouse then leaves it, it turns off - and changes to During Gameplay + locked to bring things back to their default state.
  • Hi Chris
    I tried setting it up as you described and got to work somewhat when I worked out the script attached to the GameObject has to be called "InvMouseOver" for it to work.

    What I've found though is that the first time I click the InventoryOpen button, the inventory opens. I mouse over it then off it and it disappears as desired. If I then click the InventoryOpen button again, the inventory does not appear until I mouse over the top of where it will be. Any thoughts?
  • Awesome work Chris! Cheers!
  • The custom script link doesn't work anymore...any chance we can get a link that does? I would like to try this effect out myself. Thanks!

  • edited August 2019

    I'm afraid not on my end - unfortunately I assumed pasteall.org links were relatively future-proof.

    Perhaps @Frogtied still has it?

  • I would be interested in this script also, just to try...any chance to put this to Community wiki?

  • edited August 2019

    I never finished my project unfortunately, but luckily I have a snapshot of it from around that time, and I think I found the code - let me know if this is it.

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class InvMouseOver : MonoBehaviour
    {
    
        public string inventoryMenuName = "Inventory";
    
    
        private void OnEnable()
        {
            EventManager.OnMenuTurnOff += OnMenuTurnOff;
            EventManager.OnMouseOverMenu += OnMouseOverMenu;
        }
    
    
        private void OnDisable()
        {
            EventManager.OnMenuTurnOff -= OnMenuTurnOff;
            EventManager.OnMouseOverMenu -= OnMouseOverMenu;
        }
    
    
        private void OnMenuTurnOff(Menu menu, bool isInstant)
        {
            if (menu.title == inventoryMenuName)
            {
                Debug.Log("Turning off " + inventoryMenuName + ", so locking it and setting its Appear type to During Gameplay");
                menu.isLocked = true;
                menu.appearType = AppearType.DuringGameplay;
            }
        }
    
    
        private void OnMouseOverMenu(Menu menu, MenuElement element, int slot)
        {
            if (menu.title == inventoryMenuName && !menu.isLocked)
            {
                Debug.Log("Mouse is over " + inventoryMenuName + ", so settings its Appear type to Mouse Over");
                menu.appearType = AppearType.MouseOver;
            }
        }
    
    }
    
  • Great,many thanks @Frogtied :) :)

  • @ChrisIceBox - In order to make this work across the whole game, does it mean I would have to attach this script to a game object in every scene?

  • edited September 2019

    Thanks for the assist, @Frogtied.

    @Steve_Ince - It just has to be present - you can add Unity's DontDestroyOnLoad command to the Enabled function and just add it to your game's first scene - or add to a persistent object, e.g. the PersistentEngine prefab, or your Player.

  • Hello everyone in this thread - thanks for your help so far!

    My predicament is slightly different - what I'm trying to achieve is to have a small mouse over area unlock a much bigger inventory menu, and then mousing off the (bigger) menu close it.

    What I have so far is an Inventory Open menu that consists of an invisible button (made with unity UI) that responds to "pointer enter" - this runs an actionlist that unlocks my inventory - set up as above, starting locked and appear type During Gameplay. Said inventory has the script shared by @Frogtied which changes it to mouse-over, and locks it when it closes.

    The problem I have is disabling the pointer enter Inventory Open functionality and then restarting it once the Inventory menu closes.

    I've tried various approaches where the pointer enter button checks the visibility or lock state of the Inventory, unlocking it if it's invisible/locked but stopping if not, but I always seem to create a situation where the Inventory flickers on and off as long as the pointer remains in the button, or it fires once and then the Inventory Open menu doesn't trigger again.

    Any ideas or pointers?

  • Is the "Pointer Enter" event set directly in the UI Button's Inspector, or from an AC Button element that's linked to this Button component?

    And does the ActionList that it triggers run in the background, or block gameplay? If the latter, then the Inventory menu won't display until its completed (causing a flicker) if it's set to appear During Gameplay.

    You could try changing its "Appear type" to Manual so that you have full control over it - and then turn it on as opposed to unlocking it, and precede such Actions with a check to see if it's currently on.

  • Hi,
    I am pretty new to AC, I've just started to work on my first game. I'm trying to achieve the same effect (closing the inventory after the mouse left it). I made the above steps and it worked well when I had a button that unlocks the menu. But...I want to make my inventory appear when I click with the right mouse button. The problem is that the above script locks the inventory menu and it cannot reappear again when clicking with the right mouse, because it is locked. I tried to replace the line "menu.isLocked = true;" in the script to "menu.TurnOff();" but in this case the game froze when the mouse left the menu. Is there a way to just turn off the inventory menu and not lock it?
    (I' am working on a 2D game, Unity version: 2020.1.0f1, AC version: 1.71.8)
    Thanks!

  • Welcome to the community, @placyd.

    Locking a menu is appropriate here because it prevents it from turning on even when its "Appear type" condition is met.

    A Menu can be unlocked just as easily, though - in this case, when the user right-clicks:

    void Update ()
    {
        if (Input.GetMouseButtonDown (1) && KickStarter.stateHandler.IsInGameplay ())
        {
            PlayerMenus.GetMenuWithName ("Inventory").isLocked = false;
        }
    }
    

    (This also checks to see if you're in regular gameplay, but you can remove that if desired).

    If this doesn't give you quite the behaviour you're looking for, best to cover things from the top - where your menu is positioned, and how/when you want it to behave exactly.

  • Thank you, Chris! It is also a possible solution and it is working now as I wanted.

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.