- Home /
How do I change the texture of a Raw image?
I have created a UI RawImage of a battery, and throughout the game as the player collects batteries the life on the battery grows. This requires me the change the texture of my Raw Image. How do I script that using Javascript in Unity 4.6.
Here is what I have
#pragma strict
static var charge : int = 0;
var charge1tex: Texture2D;
var charge2tex : Texture2D;
var charge3tex : Texture2D;
var charge4tex : Texture2D;
var charge0tex : Texture2D;
function Start () {
guiTexture.enabled = false; //exécutre au début du niveau du jeu la pile n'est pas visible au début du jeu
charge = 0; //Aucun pile été collectionner des le début du jeu
}
function Update () {
if (charge ==1){
UI.RawImage.texture = charge1tex;
UI.RawImage.enabled = true; //I only want it visible once I gain some battery power.
}
else if (charge ==2){
guiTexture.texture = charge2tex;
}
else if (charge ==3){
guiTexture.texture = charge3tex;
}
else if (charge ==4){
guiTexture.texture = charge4tex;
}
else{
guiTexture.texture = charge0tex;
}
}
Thanks for your help
Answer by DwaynePritchett · May 13, 2015 at 10:01 PM
Don't use texture 2d. Use, Sprites. Make an image by right clicking the hierarchy view and selecting create-image. Then, add a public variable to your script, public Image battery. Add using Unity.UI; at the top of course. Now, change the Image.fillAmount, to equal charge/fullcharge. Inside the inspector, set the image to fill horizontally/vertically. Now, you have a nice "Gauge" like hp, but for the battery. If you want it to look like a battery, and fill the inside. Make 2 images. The parent should be the battery shape... add a mask component to it. The child will be the first image I explained to you. If this doesn't make sense. Go check out a Unity UI 4.6 tutorial. Sorry, if this isn't that helpful.
-Dwayne Pritchett
O$$anonymous$$ so I changed my images to Sprites and am able to put the Image source on my UI Image to the first one I want. Now how do I code the change of the Image source of my UI Image when it hits a battery. $$anonymous$$y UI image is called battery_life.
static var charge : int = 0;
var charge1 : Sprite;
var charge2 : Sprite;
var charge3 : Sprite;
var charge4 : Sprite;
var charge0 : Sprite;
function Update () {
if (charge ==1){
//What goes here to make the Image source to be the image stored in charge1?
}
static var charge : int = 0;
var charge1 : Sprite;
var charge2 : Sprite;
var charge3 : Sprite;
var charge4 : Sprite;
var charge0 : Sprite;
public var batteryImage : Image;
function Update () {
if (charge ==1){
batteryImage.sprite = charge1;
}
You have to drag and drop the Image onto your public image variable in the inspector though. Or you can find it in code... if you want to "find it"... look up referencing objects.
I don't use UnityScript so there might be an error. Also, this isn't really a good way to do this... but it should work none the less. Hope this helps, -Dwayne Pritchett
Your answer
Follow this Question
Related Questions
How can I add a distance cap to this script? 3 Answers
How can i show the texture in the array? 2 Answers
DrawTexture GUI iPhone 1 Answer
GUI Texture fixed on Object 1 Answer
How do I trigger GUI elements 1 Answer