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
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.
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.