Forum rules - please read before posting.

Suggestion: Changing Menu Graphic Textures

Hey there :),
I had a suggestion for changing menu's using actionlists, I'm not sure if this is possible but it came to mind,

Through an actionlist you can show/hide menu elements, and at the moment I've been using that by making about 30 copies of a graphic and assigning a texture to all of them and then turning each graphic on and off as I need them (basically it's an interact menu with a graphic of each object as you approach it)

But it clutters up the actionlists and menus and I wondered if it'd be possible to have a "change graphic texture" option where I can simply assign the new texture in the actionlist and use that as I go, instead of creating loads of menu elements?

If not it's totally cool, was just an idea :)
Thank you!
«1

Comments

  • It's not present because it there would be knock-on issues with the save system, but it's definitely possible through custom Actions or custom scripts.

    See the "Menu scripting" chapter of the Manual for more on accessing menu elements through script.  To change a Graphic element's texture, it's basically this:

    MenuElement myElement = AC.PlayerMenus.GetElementWithName ("MyMenu", "MyGraphic");
    MenuGraphic myGraphic = (MenuGraphic) myElement;
    myGraphic.graphic.texture = myNewTexture;

    For a non-script solution, you can rely on a Unity UI-based Menu that doesn't have a Graphic element, but instead has an Image component that you modify with Unity's Animator system.  You can then have AC control the animation's playback with the Object: Animate Action.
  • edited March 2018
    Hey Chris,
    Thanks a lot for that! I need to ask though, in your custom action guide, what do I put in ShowGUI, SetLabel and Run if I'm dealing with Menu's and textures instead of gameobject's?
  • SetLabel can be ignored - it's just to give more detail in a collapsed Action's header.

    The code above would go in the Run method.  Since the change occurs instantaneously, you can follow it up with "return 0f;" and nothing else.

    ShowGUI only needs to be edited if you want any fields in the Action's UI, so that you can e.g. change which graphic / element is affected for each Action.  While you're not dealing with GameObjects, you are dealing with texture assets and menu names - which can all be set in UI fields, i.e:

    myNewTexture = (Texture2D) EditorGUILayout.ObjectField ("Texture:", myNewTexture, typeof (Texture2D), false);

    You can look up Unity's own docs for more on the EditorGUILayout class, as AC relies on Unity's own editor code to expose such fields.
  • edited August 2018
    Hi,
    Sorry to bump an old thread but while messing with actions again I found myself wanting to try this again (I gave up back in March).

    So today I've got to a point where the action appears under Custom, and there is a "texture" image field, but the only thing I can't figure out is how to get the 'menu' and 'menu element' field to appear.

    The long way I've been doing it is adding the normal ChangeMenuIcon script (not the action script) to a game object and then just adding and removing it to a scene.
    This is how it looks in the inspector:



    With the action I made with the code you sent me, this is how it looks so far as an action:

    The menu name and element name options are missing.

    This is the script:

    image

    I'm really new to this action stuff as it's a bit different to shoving everything in 1 place, so bare with me...could you please advise me on what to do next?
    Thanks!
  • If you'd like help editing your code, you'll need to paste it in e.g. pasteall.org

    The values of public variables you define in an Action do not appear automatically in Action UIs as they do with regular component Inspectors.  You need to instead force their display in the ShowGUI method below, as the template comments explain.  This is why your Texture field displays.

    You can create a field that displays e.g. your menu name with:

    MenuName = EditorGUILayout.TextField ("Menu name:", MenuName);
  • Hi Chris,
    That works! Thanks so much, this will make everything sooo much easier. :D

    If you don't mind, for some reason if I put the new code below myNewTexture instead of above it , it does this.
    I'm just curious why it does this? Just as future reference as this came up yesterday and I wouldn't have needed to come here if I knew why haha
  • It's impossible to tell without seeing the code that it's referencing.
  • Hey
    I used this again after 2 months and found a problem with it but not sure if it's just me.
    When I use the change menu icon action and then turn on said menu on the same actionlist, the menu flashes, the icon changes but it deactivates again. Probably because of the recalculate all part of the code? 
    https://pastebin.com/FJG2qf0B
    I'm wondering if you could please take a look at this for me if you had time? :)
  • Make sure that the Graphic element's Graphic type property is set to Normal, so that it isn't changed by AC.

    The latest release does away with the need to recalculate the menu/element - just use the new SetNormalGraphicTexture method:

    myGraphic.SetNormalGraphicTexture (myNewTexture);
  • Hi again,
    Back at this again a year later...
    Do you think it's possible to change a button's graphic?

  • To change the property of a Menu, right-click it in the Menu Manager to get an API reference.

    For example, the API reference for the default Inventory menu's "ShiftLeft" button's background texture is:

    AC.PlayerMenus.GetElementWithName ("Inventory", "ShiftLeft").backgroundTexture
    

    You can use this to change the background texture of an AC Menu's Button.

  • edited May 3

    Hi Chris,
    I'm trying to make it so that it can be changed in an action
    But it's giving me the error: "Assets/AdventureCreator/Scripts/Actions/ActionChangeMenuButtonIcon.cs(60,83): error CS0029: Cannot implicitly convert type UnityEngine.Texture2D to `AC.MenuElement'"
    This is my code:
    https://pastebin.com/ddKm6nY2

    Is it not doable for menu button textures?

  • It is. There is no need for a conversion with the API reference, and there's also no Graphic element involved.

    You just need to set the texture directly:

    AC.PlayerMenus.GetElementWithName (MenuName, ElementName).backgroundTexture = myNewTexture;
    
  • Hi Chris,
    Well I've changed it to this,

    if (!isRunning)
    {
    MenuElement myElement = AC.PlayerMenus.GetElementWithName (MenuName, ElementName).backgroundTexture = myNewTexture;
    MenuGraphic myGraphic = (MenuGraphic) myElement;
    myGraphic.graphic.texture = myNewTexture;
    KickStarter.playerMenus.RecalculateAll ();
    myGraphic.SetNormalGraphicTexture (myNewTexture);
    }
    return 0f;
    }

    But I'm still getting the same error?

  • Again: there is no MenuGraphic element involved. Replace your code with mine:

    MenuElement myElement = AC.PlayerMenus.GetElementWithName (MenuName, ElementName).backgroundTexture = myNewTexture;
    return 0f;
    

    That is all that needs to go in your Run function, provided that your Menu is an AC one.

  • edited May 5

    So this?

    if (!isRunning)
    {
    MenuElement myElement = AC.PlayerMenus.GetElementWithName (MenuName, ElementName).backgroundTexture = myNewTexture;
    }
    return 0f;
    }

    I've changed it to yours and it's still saying "Assets/AdventureCreator/Scripts/Actions/ActionChangeMenuButtonIcon.cs(60,103): error CS0266: Cannot implicitly convert type UnityEngine.Texture' to UnityEngine.Texture2D'. An explicit conversion exists (are you missing a cast?)"

    I tried changing the variable from public Texture myNewTexture; to public Texture2D myNewTexture;
    But then it says " error CS0029: Cannot implicitly convert type UnityEngine.Texture2D' to AC.MenuElement'"

    Sorry that I'm being a pain Chris

  • Try this:

    using UnityEngine;
    using System.Collections;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionChangeMenuButtonIcon : Action
        {
    
            // Declare variables here
            [TextArea(1,1)] public string MenuName = "";
            [TextArea(1,1)] public string ElementName = "";
            public Texture2D myNewTexture;
    
            public ActionChangeMenuButtonIcon ()
            {
                this.isDisplayed = true;
                category = ActionCategory.Custom;
                title = "Change Menu Icon";
                description = "Change menu icon.";
            }
    
    
            override public float Run ()
            {
                AC.PlayerMenus.GetElementWithName (MenuName, ElementName).backgroundTexture = myNewTexture;
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            override public void ShowGUI ()
            {
                // Action-specific Inspector GUI code here
                MenuName = EditorGUILayout.TextField ("Menu name:", MenuName);
                ElementName = EditorGUILayout.TextField ("Element name:", ElementName);
                myNewTexture = (Texture2D) EditorGUILayout.ObjectField ("Texture:", myNewTexture, typeof (Texture2D), false);
    
                AfterRunningOption ();
            }
    
            #endif
    
        }
    
    }
    
  • Hi Chris,
    Thanks very much for that,
    It works the first time, but when trying to use it again to change the icons back to how they were it does this:
    "NullReferenceException: Object reference not set to an instance of an object
    AC.ActionChangeMenuButtonIcon.Run () (at Assets/AdventureCreator/Scripts/Actions/ActionChangeMenuButtonIcon.cs:42)"

  • That does not match up with the line endings in the script I wrote for you. What is on line 42, and is this coming from a new Action? Check that the menu/element names are correct.

  • Hi Chris,
    AC.PlayerMenus.GetElementWithName (MenuName, ElementName).backgroundTexture = myNewTexture;
    Is the line and yes it's coming from a new actionlist

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.