Forum rules - please read before posting.

Hide Inventory Item when examining it

I've set up the following functionality:

-Open inventory
-Click on item
-View item object up close and spin it around
-Click anywhere to stop viewing the object

To implement this functionality I mostly followed your https://adventurecreator.org/tutorials/examining-objects-close tutorial.

In additional I'd like to hide the inventory icon of the item being examined so that you can't click or drag it again while viewing it and to give the appearance the item has moved to the center of the screen. Hiding an inventory items seems to be a function that exists because this is what happens when you drag an item. The item in the inventory is hidden and the cursor is changed to the item icon. I've having trouble finding this function (hide and then restore inventory slot) in the code... can you point me in the right direction?

Thanks,
-MIke

Comments

  • edited July 2022

    The built-in function of hiding the selected item is purely cosmetic - the item's still there and interactive. You'd need to rely on a separate technique to have it be properly hidden and non-interactive.

    To my mind, this is best done through the use of Unity's Canvas Group component. If your Inventory uses Unity UI for its source, you can attach this component to each of your item slot Button objects, so that their visibility and interactivity can then be controlled via its Alpha and Interactable fields respectively.

    These fields can then be set by accessing the MenuInventoryBox element through custom script function, e.g.:

    public void SetItemVisibility (int itemID, bool show)
    {
        MenuInventoryBox inventoryBox = (MenuInventoryBox) PlayerMenus.GetElementWithName ("Inventory", "InventoryBox");
        int slotIndex = inventoryBox.GetItemSlot (itemID);
        GameObject slotObject = inventoryBox.GetObjectToSelect (slotIndex);
        CanvasGroup canvasGroup = slotObject.GetComponent<CanvasGroup> ();
        canvasGroup.alpha = show ? 1f : 0f;
        canvasGroup.interactable = show;
    }
    

    Adapted to a custom Action, that accepts an inventory item parameter:

    using UnityEngine;
    using System.Collections.Generic;
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionHideItem : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Inventory; }}
            public override string Title { get { return "Set item visibility"; }}
    
            public int itemID;
            public int itemParameterID = -1;
            public bool show;
    
            public override void AssignValues(List<ActionParameter> parameters)
            {
                itemID = AssignInvItemID (parameters, itemParameterID, itemID);
            }
    
            public override float Run ()
            {
                MenuInventoryBox inventoryBox = (MenuInventoryBox) PlayerMenus.GetElementWithName ("Inventory", "InventoryBox");
                int slotIndex = inventoryBox.GetItemSlot (itemID);
                GameObject slotObject = inventoryBox.GetObjectToSelect (slotIndex);
                CanvasGroup canvasGroup = slotObject.GetComponent<CanvasGroup> ();
                canvasGroup.alpha = show ? 1f : 0f;
                canvasGroup.interactable = show;
                return 0f;
            }
    
            #if UNITY_EDITOR
    
            public override void ShowGUI (List<ActionParameter> parameters)
            {
                itemParameterID = ChooseParameterGUI ("Item:", parameters, itemParameterID, ParameterType.InventoryItem);
                if (itemParameterID < 0)
                    itemID = UnityEditor.EditorGUILayout.IntField ("Item ID:", itemID);
                show = UnityEditor.EditorGUILayout.Toggle ("Show?", show);
            }
    
            #endif
    
        }
    
    }
    

    More on accessing menus through script can be found in the Manual's "Menu scripting" chapter.

  • Above and beyond - thank you Chris!
    Please be aware that changing the CanvasGroup interactable flag to false does not disable the ability to drag the item from the now visibly empty space. Nor does making the Button interactable flag false. Nor does disabling the button.

  • You're referring specifically to drag-and-drop behaviour, or is there an issue when this option is unchecked?

  • Yes the drag-and-drop behavior. You are still able to drag the item from the blank and disabled slot (and see the item cursor) even when the CanvasGroup and Button have interactable set to false.

  • Thanks, recreated.

    I will include a fix as part of the upcoming v1.75.6 update.

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.