- Home /
How do I change the texture of a RawImage?
Hello! Thank you for reading this post. I've recently started a project were I'm making a game for kids with my accomplice. The point of the game is that a country and its name is supposed to light up in an orange color when that country is picked through a script that spits out a random number.
The point is then to match the name of country by choosing between three flags on the left. These three will be randomzied, then one will be replaced with the correct answer. How we've done it up to this point is that we've used the UI element RawImage then used flags which we've painted as the texure. Now to my real question, is it possible in anyway to manipulate what texture the RawImage has through scripting? Any suggestions or anwsers are accepted, or if you have a better idea of how to create the system please leave a comment. :D
Answer by TinyDev · Feb 09, 2015 at 10:12 PM
Several ways to set this up.
Here is one: Create a class (say, ImageClass) that exposes properties for your RawImage and Textures. You can use arrays for the textures, create your own custom editor property, etc.
Quick and dirty just to show how to change the texture...your code would need to handle the swap, presumably based on game logic:
The texture property of the RawImage gets/sets the Texture (different from Image, which gets/sets Sprite)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ImageClass : MonoBehaviour {
public GameObject ImageOnPanel; ///set this in the inspector
public Texture NewTexture;
private RawImage img;
void Start () {
img = (RawImage)ImageOnPanel.GetComponent<RawImage>();
img.texture = (Texture)NewTexture;
}
// Update is called once per frame
void Update () {
}
}
,
Your answer
Follow this Question
Related Questions
Why the text and the UI code is can't applying 0 Answers
How do you make a method that gets called when a button gets highlighted using the arrow keys? 0 Answers
Make UI element or button where a physics raycast can´t pass? Is that possible? 1 Answer
Unity 5.4.0f3 Personal - UI not updating after Build. 1 Answer