Forum rules - please read before posting.

Inventory to open and highlight new item added

Hi,

Do you have a tutorial or can help me understand the best way to have an inventory item appear in the centre of the screen then shrink and zoom into the inventory when an item is picked up?

«1

Comments

  • You'll want to animate it as part of a Unity UI-based Inventory, with the item's graphic being a separate RawImage object in the canvas hierarchy.

    When the player takes an item, you can use scripting to update the RawImage's texture with that of the item:

    public UnityEngine.UI.RawImage rawImage;
    
    private void OnEnable () { AC.EventManager.OnInventoryAdd += OnInventoryAdd; }
    private void OnDisable () { AC.EventManager.OnInventoryAdd -= OnInventoryAdd; }
    
    private void OnInventoryAdd (AC.InvItem invItem, int amount)
    {
        rawImage.texture = invItem.tex;
    }
    

    It's a lot easier if you have the item zoom to the same position each time, i.e. a "bag" icon rather than the actual position of the item in the inventory. This way, you can animate the RawImage's position through a regular Animator component.

  • ok thanks! So what do i attach this script to?

  • A RawImage component that's enabled at the time of the inventory addition.

  • i think i may have got this script slightly wrong? Getting errors:

    (Must derive from monobehaviour)

    using UnityEngine;

    public class InventoryZoom
    {
    public UnityEngine.UI.RawImage rawImage;

    private void OnEnable() { AC.EventManager.OnInventoryAdd += OnInventoryAdd; }
    private void OnDisable() { AC.EventManager.OnInventoryAdd -= OnInventoryAdd; }
    
    private void OnInventoryAdd(AC.InvItem invItem, int amount)
    {
        rawImage.texture = invItem.tex;
        //trigger the animation here
    }
    

    }

  • How would i incorporate code to set off my animation and maybe a sound too?
  • Use:

    public class InventoryZoom : MonoBehaviour
    

    How would i incorporate code to set off my animation and maybe a sound too?

    You can use another script, or the Object: Animate / Sound: Play Actions.

  • ok, so i have attached it to the RawImage within my inventory, but nothing is happening when I pick up my item? I don't think my object animate is linking in scene as the Inventory is a prefab? Am i doing this wrong?

    https://www.dropbox.com/sh/5g5lgw6euc4uq4x/AADY16ZhEA9d4rO40MrQb7EUa?dl=0

  • You'll want to run the Action from an ActionList asset, since that way you can use parameters to run the same list each time you take an item - see this tutorial.

    If you assign the Animator from the prefab into an asset, it should record a Constant ID that can be used to find it in the scene.

    Alternatively, the script can also be updated to trigger the animation directly:

    GetComponent<Animator>().SetTrigger ("MyTriggerParameter");
    
  • This works like a charm, can you advise on script tweak so the same things happens for combine event? I want to show old item becoming new item

  • If the interaction makes use of the Inventory: Add or remove Action to add or replace an item, the OnInventoryAdd event should still be called. It should just be a case of also triggering the animation - how are you doing so currently?

  • I would like to show the old item becoming the new item in the animation though, so for example, matches sprite and a candle sprite appear next to each other, then the animation merges them and a candle lit sprite is shown. Here is how I am currently doing it:

    https://www.dropbox.com/s/u64cgg5uomv7o0d/PickUpItemActionLIst.png?dl=0

  • If it's a separate effect, you'll need to repeat the process of creating an animation, and then affecting it's appearance through script.

    Start by creating the animation you want to show, i.e. two objects becoming one, which will be needed before any script can affect it.

  • ok created, would you be so kind as to help with the script? I am animating two different raw images becoming one raw image

  • I'd need to see what the animation look like, and the Action(s) you're using to update the inventory.

  • edited October 2021

    If you're using an ActionList asset with parameters to set what items are added/removed, you can have a custom script hook into those parameters to update the RawImages with:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class SetCombineTextures : MonoBehaviour
    {
    
        public ActionListAsset combineActionList;
        public int itemToAddParameterID;
        public int itemToCombine1ParameterID;
        public int itemToCombine2ParameterID;
        public RawImage itemToAddImage;
        public RawImage itemToCombine1Image;
        public RawImage itemToCombine2Image;
    
        private void OnEnable () { EventManager.OnBeginActionList += OnBeginActionList; }
        private void OnDisable () { EventManager.OnBeginActionList -= OnBeginActionList; }
    
        private void OnBeginActionList (ActionList actionList, ActionListAsset actionListAsset, int startingIndex, bool isSkipping)
        {
            if (actionListAsset && actionListAsset == combineActionList)
            {
                ActionParameter itemToAddParameter = actionList.GetParameter (itemToAddParameterID);
                InvItem itemToAdd = KickStarter.inventoryManager.GetItem (itemToAddParameter.intValue);
                itemToAddImage.texture = itemToAdd.tex;
    
                ActionParameter itemToCombine1Parameter = actionList.GetParameter (itemToCombine1ParameterID);
                InvItem itemToCombine1 = KickStarter.inventoryManager.GetItem (itemToCombine1Parameter.intValue);
                itemToCombine1Image.texture = itemToCombine1.tex;
    
                ActionParameter itemToCombine2Parameter = actionList.GetParameter (itemToCombine2ParameterID);
                InvItem itemToCombine2 = KickStarter.inventoryManager.GetItem (itemToCombine2Parameter.intValue);
                itemToCombine2Image.texture = itemToCombine2.tex;
            }
        }
    
    }
    

    Attach this to the UI and assign the RawImage components and the ActionList that triggers the animation. Assign also the IDs of the "Item to add" / "Item to remove" parameters.

  • Getting an error:

    Can't add script component 'SetCombineTextures' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.

  • Name your script SetCombineTextures.

  • It already was, I get this - Assets/Sleepytime Village/Scripts/SetCombineTextures.cs(31,36): error CS0029: Cannot implicitly convert type 'AC.ActionParameter' to 'int'

  • I've updated the above - try again.

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.