Changing Image in unity by C# script

 There are two case when you want to change image one is UI image and other is a sprite in scene so in this post we will take a look at both cases how to change images.

Changing UI Image

There are very few steps to change UI image scripts
  • First create a UI image gameObject in scene whose image we want to change
  • create a script and attach with an other empty gameObject and open script
  • In script declare a public GameObject for referencing changing image and a declare a private Image variable for refencing Image component in this gameObject
  • also declare a public Sprite for which image that we want in replacement.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class test : MonoBehaviour
{
    public GameObject image_object;
    public Sprite mySprite;
    private Image myImage;

    void Start()
    {
        myImage = image_object.GetComponent<Image>();
        myImage.sprite=mySprite;
    }
}


  • Drag and drop UI image gameObject in image_object and also drag and drop sprite image from project window with that you want to change in mySprite. 
  • myImage = image_object.GetComponent<Image>();  will assign image component in myImage. then we can change image by using this variable.
  • myImage.sprite=mySprite; get script property of image and change to mySprite. 
  • alternatively you call also use image_object.GetComponent<Image>().sprite=mySprite; code to get same result but GetComponent is resources consuming method so if we change image at many time it is good to reference it. otherwise it will effect performance.   
You can also declare an array of sprites if you want to replace with one in many sprites.

Changing Sprite Image

Second case is when you are creating a 2D game and you want to change sprite image. 
  • UI image and sprites both are different because in UI image sprite are hold by Image component by in sprites images sprite are hold by an sprite renderer.
  • So in place of Image component we have to access SpriteRenderer to change sprite and everything else is same as previous.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class test : MonoBehaviour
{
    public GameObject sprite_object;
    public Sprite mySprite;
    private SpriteRenderer myRenderer;
    // Start is called before the first frame update
    void Start()
    {
        myRenderer = sprite_object.GetComponent<SpriteRenderer>();
        myRenderer.sprite=mySprite;
    }
}

 
  • myRenderer = sprite_object.GetComponent<SpriteRenderer>(); will access sprite renderer component of sprite image.
  •  myRenderer.sprite=mySprite;  will change sprite to our desired sprite mySprite.
  • In this method you can also use  sprite_object.GetComponent<SpriteRenderer>().sprite=mySprite;  to get same result but again it is also resource consuming if you use it many times.


Popular posts from this blog

Positioning GameObjects in unity editor

Creating count down timer in unity by c# script