Forum rules - please read before posting.

Auto select the last hovered element in inventory UI when it is opened last time

Hi, I am using Unity Ui Prefab for Inventory, and my inventory is only for viewing items but no using or selecting the items. And I am also using keyboard to direct control the menu. So I wish to use scripts to read the item being hovered when the inventory menu is closed, and then set the cursor to hover over that item again when the inventory menu is opened again. What scripts may I use?

I tried PlayerMenus.GetMenuWithName ("CustomInventory").selected_element but it seems to reference nothing; and PlayerMenus.GetMenuWithName ("CustomInventory").selected_slot but it is always equal to 0.

Comments

  • I also hope to make it:
    If the previously hovered slot is disabled (e.g. the item is gone because of the story), the cursor will go back to the first slot when the inventory menu is opened again.

  • The selected_slot property is specifically reserved for AC-rendered menus.

    To determine which slot is currently-selected, you can try hooking into the OnMouseOverMenu custom event (which should also work with direct-navigation) to record the last-set slot index. You can then use this to select that same index in the OnMenuTurnOn event.

    Something like this:

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class ReselectLastSlot : MonoBehaviour
    {
    
        private int slotToSelect;
    
        private void OnEnable ()
        {
            EventManager.OnMenuTurnOn += OnMenuTurnOn;
            EventManager.OnMouseOverMenu += OnMouseOverMenu;
        }
    
        private void OnDisable ()
        {
            EventManager.OnMenuTurnOn -= OnMenuTurnOn;
            EventManager.OnMouseOverMenu -= OnMouseOverMenu;
        }
    
        private void OnMenuTurnOn (Menu menu, bool isFading)
        {
            if (menu.title == "CustomInventory")
            {
                menu.Select ("InventoryBox", slotToSelect);
                Debug.Log ("Select slot: " + slotToSelect);
            }
        }
    
        private void OnMouseOverMenu (Menu menu, MenuElement element, int slot)
        {
            if (element == null) return;
    
            if (menu.title == "CustomInventory" && element.title == "InventoryBox")
            {
                slotToSelect = slot;
                Debug.Log ("Last-selected slot: " + slotToSelect);
            }
        }
    
    }
    
  • Thanks. How about if the previously hovered slot is now disabled? Any script can get the enable state of the slot?

  • It seems the OnMouseOverMenu is not doing anything. My inventory doesn't pause the game, and use the Engine > Manage System: Enable Control in-game menus to hover across slots. This are my settings:
    https://i.imgur.com/Yr5AVfl.png
    https://i.imgur.com/K2UghC6.png
    https://i.imgur.com/hT4Sj5h.png

  • It seems the OnMouseOverMenu is not doing anything.

    Apologies, I was mistaken - OnMouseOverMenu is indeed for mouse-only.

    Since you're using Unity UI, you can instead hook into the ISelectHandler event. Replace the above script with:

    using UnityEngine;
    using AC;
    
    public class ReselectLastSlot : MonoBehaviour
    {
    
        public static int slotToSelect;
    
        private void OnEnable ()
        {
            EventManager.OnMenuTurnOn += OnMenuTurnOn;
        }
    
        private void OnDisable ()
        {
            EventManager.OnMenuTurnOn -= OnMenuTurnOn;
        }
    
        private void OnMenuTurnOn (Menu menu, bool isFading)
        {
            if (menu.title == "Inventory")
            {
                menu.Select ("InventoryBox", slotToSelect);
            }
        }
    
    }
    

    Then attach the following to each Inventory UI Button, setting the "This Slot Index" value as appropriate:

    using UnityEngine;
    using UnityEngine.EventSystems;
    
    namespace AC
    {
    
        public class DetectSelection : MonoBehaviour, ISelectHandler
        {
    
            public int thisSlotIndex;
    
    
            public void OnSelect (BaseEventData eventData)
            {
                ReselectLastSlot.slotToSelect = thisSlotIndex;
            }
    
        }
    
    }
    
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.