Image and Raw Image components show pictures in your game or app. They help you add visuals like buttons, backgrounds, or textures.
0
0
Image and raw image components in Unity
Introduction
To display a button icon or UI element in your game.
To show a background picture behind your game content.
To display a photo or texture loaded from a file or URL.
To add decorative images to menus or HUDs.
To show raw textures that need no extra processing.
Syntax
Unity
Image imageComponent; RawImage rawImageComponent;
Image uses sprites, which are optimized 2D images.
RawImage uses textures directly, useful for videos or dynamic images.
Examples
This example shows how to declare an Image component and assign a sprite to it.
Unity
using UnityEngine.UI; public Image myImage; // Assign a sprite in the Inspector or by code myImage.sprite = someSprite;
This example shows how to declare a RawImage component and assign a texture to it.
Unity
using UnityEngine.UI; public RawImage myRawImage; // Assign a texture in the Inspector or by code myRawImage.texture = someTexture;
Sample Program
This script sets an Image and a RawImage component to show a sprite and a texture when the game starts. Assign the sprite and texture in the Unity Editor.
Unity
using UnityEngine; using UnityEngine.UI; public class ShowImages : MonoBehaviour { public Image imageComponent; public RawImage rawImageComponent; public Sprite exampleSprite; public Texture exampleTexture; void Start() { // Set the Image component's sprite imageComponent.sprite = exampleSprite; // Set the RawImage component's texture rawImageComponent.texture = exampleTexture; } }
OutputSuccess
Important Notes
Use Image for UI elements that need to be sliced or tinted easily.
Use RawImage when you want to display textures that are not sprites, like video frames.
Remember to assign your images in the Inspector or by code before running.
Summary
Image component displays sprites optimized for UI.
RawImage component displays textures directly.
Both are used to show pictures in your Unity UI.