- Home /
Is it possible to change an images source image by code? C#
Im making a game were I want my background image to change between hundreds of different images that I have in my assets folder. Is there a way to do this by code? I have my Image "testImage" decleared in my script and I have dragged the Image gameobject to the slot in inspector. In my project assets I have the other source image that I want to change to called "NewSourceImage". I have tried with this code but it doesnt work:
public Image testImage;
public void Change()
{
testImage = GetComponent<Image>("NewSourceImage");
}
I get the error message: error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments
Yes.Its possible to change the any image at runtime.For that you need to use SpriteRenderer.I have this code written in c#.I have used this code and its works..enjoy..
//Change sprite on runtime
Sprite spr = Resources.Load ("Required Image");
SpriteRenderer sprRenderer = (SpriteRenderer)renderer;
sprRenderer.sprite = spr;
Thanks pradeep, but I cant get it to work. Sorry but Im new to unity and C#... I have changed my code to this now:
public Image testImage;
public Sprite spr;
public void Change()
{
spr = Resources.Load ("newSourceImage");
sprRenderer = (SpriteRenderer)renderer;
sprRenderer.sprite = spr;
}
I now get error message: error CS0266: Cannot implicitly convert type UnityEngine.Object' to
UnityEngine.Sprite'. An explicit conversion exists (are you missing a cast?)
Also with this code I never assign the sprite to the Image testImage. Im not sure how to do that.
Answer by Kiwasi · Nov 27, 2014 at 08:33 AM
Here is code to change an image
public Image myImage;
public void Change(){
myImage.sprite = someOtherSprite;
}
Here is code to load a sprite from the resources folder.
public Sprite someOtherSprite;
public LoadSprite (){
someOtherSprite = Resources.Load<Sprite>("NewSourceImage");
}
Here is both of them put together in a sensible structure.
public void ChangeImage (string newImageTitle){
myImage.sprite = Resources.Load<Sprite>(newImageTitle);
}
With this code being just under 5 years old, does it still work? I've tried to get it to work in my game, but not only am I having trouble actually calling the "ChangeImage" function, but I'm not exactly sure how to use this code to actually change a UI panel's Source Image. A little help please!
Answer by hexagonius · Nov 26, 2014 at 04:16 PM
Resources.Load() is always returning an Object because it can nearly load anything, which means you need to cast the result into what the loaded resource will be.
spr = Resources.Load ("newSourceImage") as Sprite;
or
spr = (Sprite) Resources.Load ("newSourceImage");
My guess is that line 9 will also throw an error, because it does not match the preceding answer.
Thanx Hexagonius, but it still doesnt work for me :/. Now I get error message: "InvalidCastException: Cannot cast from source type to destination type." $$anonymous$$y sprite is a .jpg that I just have dragged from my coputer to my resources folder.
Sprite spr = Resources.Load("newSourceImage") as Sprite;
SpriteRenderer sprRenderer = (SpriteRenderer)renderer;
sprRenderer.sprite = spr;
testImage.sprite = spr; // Here I try to put in the new sprite in the Image (am I correct?).
Then I think your jpg is a Texture, not a sprite. Click on the jpg in the Project folder and look up the inspector window if it is and change it accordingly. What is testImage supposed to be? That line is not necessary because the 3rd one already does the assignment. 1. line: get the sprite 2. line: get the renderer 3. line: assign the sprite to the renderer
that's it
Honestly I dont understand why I need line two and three... testImage is my image that I need to change from code. I know how to change it manualy by draging a new image to the "Source Image" slot in my Image-script in inspector and thats what I want to do by code.
I dont know if it makes a diffrence but im using the unity 4.6 beta and my image is an UI image