Forum rules - please read before posting.

Changing menu image

I'm not sure if I am missing something obvious here, but I've been working with AC for a while now and this is the first time this issue came up. I've developed a menu that pops up when the player picks up an item. Basically a static heading (You have found a new item!!!), a text box that changes depending on the item (i.e. Remote Control) and a Graphic element to show the item sprite. I think I can change the text by using a dialog action, but how would I change the sprite to show in the Graphic element, depending on which item the player is picking up?

Comments

  • There's a couple of ways you can do this:

    1. Use an InventoryBox element whose Inventory box type is set to Display Last Selected, and then select/deselect the added item with a pair of Inventory: Select Actions before turning the menu on.
    2. Use a Graphic element and hook a custom script into the OnInventoryAdd custom event to update the element's Texture based on the added item.

    The second way would be the cleanest. This technique is actually used by the "Chamber" demo game on the Downloads page. Here's an extract of the relevant code from its NewItemMenuSetup script:

    using UnityEngine;
    using AC;
    
    public class NewItemMenuSetup : MonoBehaviour
    {
    
        private const string menuName = "NewItem";
        private const string graphicElement = "ItemGraphic";
    
        private void OnEnable () { EventManager.OnInventoryAdd += OnInventoryAdd; }
        private void OnDisable () { EventManager.OnInventoryAdd -= OnInventoryAdd; }
    
        private void OnInventoryAdd (InvItem invItem, int count)
        {
            Menu newItemMenu = PlayerMenus.GetMenuWithName (menuName);
            MenuGraphic itemGraphic = newItemMenu.GetElementWithName (graphicElement) as MenuGraphic;
            itemGraphic.SetNormalGraphicTexture (invItem.tex);
        }
    
    }
    
  • Thanks Chris.

    I'll give method 2 a go first, although I've tried my best to avoid scripting up to now. I've managed the first series of online tutorials, but they lost me when things became more complex.

  • The script above should do it. It works by updating the Graphic element supplied in its Inspector with the texture of the last item added to the inventory.

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.