Image and Raw Image components show pictures in your game or app. They help you add visuals like buttons, backgrounds, or textures.
Image and raw image components in Unity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Unity
using UnityEngine.UI; public Image myImage; // Assign a sprite in the Inspector or by code myImage.sprite = someSprite;
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; } }
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.
Practice
1. Which Unity UI component is best suited for displaying a sprite with color tinting?
easy
Solution
Step 1: Understand the purpose of Image component
The Image component is designed to display sprites and supports color tinting, making it ideal for sprite display with color changes.Step 2: Compare with RawImage component
RawImage displays textures and allows UV adjustments but does not support sprite-specific features like tinting.Final Answer:
Image component -> Option BQuick Check:
Sprite + tinting = Image component [OK]
Hint: Image = sprites + tinting; RawImage = textures + UV [OK]
Common Mistakes:
- Confusing RawImage with Image for sprites
- Thinking Text or Button can display sprites
- Ignoring color tinting feature
2. Which of the following is the correct way to assign a texture to a RawImage component in C# script?
easy
Solution
Step 1: Recall RawImage property for texture
RawImage uses the 'texture' property to assign a Texture object.Step 2: Check syntax correctness
Assigning with 'rawImage.texture = myTexture;' is correct syntax in Unity C#.Final Answer:
rawImage.texture = myTexture; -> Option AQuick Check:
RawImage.texture = Texture [OK]
Hint: RawImage uses .texture, Image uses .sprite [OK]
Common Mistakes:
- Using .sprite with RawImage
- Calling non-existent methods like setTexture()
- Confusing Image and RawImage properties
3. Given this code snippet in Unity C#:
Image img = GetComponent<Image>(); img.color = new Color(1, 0, 0, 0.5f);What will be the visible effect on the Image component?
medium
Solution
Step 1: Analyze the color assignment
The color is set to red (1,0,0) with alpha 0.5, meaning 50% transparency.Step 2: Understand Image color effect
Image component applies color tint including alpha, so the image will appear red and semi-transparent.Final Answer:
The image will have a semi-transparent red tint -> Option DQuick Check:
Color RGBA(1,0,0,0.5) = semi-transparent red [OK]
Hint: Alpha < 1 means transparency; RGB sets tint color [OK]
Common Mistakes:
- Assuming alpha 0.5 means fully transparent
- Thinking color change won't affect Image
- Confusing solid color with tint
4. What is wrong with this code snippet that tries to assign a sprite to a RawImage component?
RawImage rawImg = GetComponent<RawImage>(); rawImg.sprite = mySprite;
medium
Solution
Step 1: Check RawImage properties
RawImage component uses 'texture' property, not 'sprite'. 'sprite' is for Image component.Step 2: Understand property mismatch
Assigning 'sprite' to RawImage causes error because it doesn't exist.Final Answer:
RawImage does not have a 'sprite' property -> Option CQuick Check:
RawImage lacks sprite property [OK]
Hint: RawImage uses .texture, not .sprite [OK]
Common Mistakes:
- Trying to assign sprite directly to RawImage
- Confusing Image and RawImage components
- Assuming RawImage has 'image' property
5. You want to display a UI element in Unity that shows a texture with custom UV coordinates to create a scrolling effect. Which component should you use and how do you adjust the UVs?
hard
Solution
Step 1: Identify component for texture with UV control
RawImage supports textures and allows UV adjustments via uvRect property.Step 2: Understand how to create scrolling effect
Changing uvRect offsets scrolls the texture on RawImage, enabling the effect.Final Answer:
Use RawImage component and modify its uvRect property -> Option AQuick Check:
Scrolling texture = RawImage + uvRect [OK]
Hint: Scroll textures by changing RawImage.uvRect [OK]
Common Mistakes:
- Trying to modify sprite UVs in Image component
- Using color changes to scroll texture
- Confusing Image and RawImage for UV control
