Forum rules - please read before posting.

Changing images in a custom menu.

edited December 2017 in Technical Q&A
Hello,

I am creating a new menu( using unity ui ) for a 2d game, which will act as a preview/close up when a user interacts with a hotspot.

Because I will only have one preview active at a time, the text of the preview will be set using global variables (label and description).

My main question is:
1) How do I change the image? 

Would love to hear anyone's thoughts/suggestions.








Comments

  • edited December 2017
    Welcome to the community, @lulitd.

    There are a number of ways you can do it, largely depending on how many such close-ups you will have.  If it's not too many, you could attach an Animator to the Image GameObject, create an animation for each image texture, then control it using the "Object: Animate" Action.

    If it's a lot, probably easier to just change it through script, e.g.:

    UnityEngine.UI.Image image = GameObject.Find ("MyImage").GetComponent <UnityEngine.UI.Image>();
    Sprite mySprite = Resources.Load ("MySprite") as Sprite;
    image.sprite = mySprite;


    Where "MyImage" is the name of your Image gameobject, and "MySprite" is the name of the new sprite to give it, which is placed in a folder named Resources in your Assets directory.

    If you wanted to incorporate this into a custom Action, see this tutorial.  Such an Action could expose the "MySprite" as a string variable in the UI for you to change, and you could also parameterise that to work with string parameters following this tutorial.  If you'd like help writing such an Action, just say.
  • Hi Chris.

    Thank you for the response and the warm welcomes. 

    Originally, I had attempted going the custom action route. But for the life of me couldn't figure out how to parameterize the sprite.  Oh "Resources.Load" how did I ever forget that... I blame the lack of sleep. 

    In the end up doing it via script attached to the particular hotspots and using onHotspotInteracted. 

    I've been using AC for about a week now and loving it. I haven't had to touch visual studio for a majority of that time. I think I  forgot how to code and was trying to figure it out the AC way. 

    Glad to have discovered that AC is super extendable with custom actions and delegates. Time to start digging through the scripting guide. 

    ps. Thank you for all the comments in the AC source code. 



  • Regarding parameterising the sprite: there's no "Sprite" parameter type but there is a String one, which is really why I was suggesting to use Resources.Load.

    A tutorial on getting a custom Action to work with parameters can be found here.  Paramaterising a string value is easier because you don't have a ConstantID number.  For a more practical example, see the ActionPlayMaker.cs script.
  • For anyone reading this and using Unity UI for the AC menus, here's a script that creates a new AC Action under "Menu" which allows you to change the image elements of that menu.
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.