Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Image and Raw Image Components in Unity
📖 Scenario: You are creating a simple Unity scene to display pictures using UI components. You will use the Image and RawImage components to show different types of images on the screen.
🎯 Goal: Build a Unity script that sets up two UI elements: one with an Image component showing a sprite, and another with a RawImage component showing a texture. You will learn how to assign images to these components through code.
📋 What You'll Learn
Create a GameObject with an Image component and assign a sprite to it.
Create a GameObject with a RawImage component and assign a texture to it.
Use public variables to assign the sprite and texture in the Unity Editor.
Write a script that sets the Image and RawImage components to display the assigned images.
💡 Why This Matters
🌍 Real World
Displaying images and textures is common in game menus, HUDs, and interactive UI in games and apps.
💼 Career
Understanding how to use Image and RawImage components is essential for UI design roles in game development and interactive media.
Progress0 / 4 steps
1
Create public variables for Image and RawImage components
Write a Unity C# script called ImageSetup. Inside the class, create two public variables: one called imageComponent of type UnityEngine.UI.Image and another called rawImageComponent of type UnityEngine.UI.RawImage.
Unity
Hint
Use public Image imageComponent; and public RawImage rawImageComponent; inside the class.
2
Add public variables for Sprite and Texture2D
Add two more public variables to the ImageSetup class: one called spriteToShow of type Sprite and another called textureToShow of type Texture2D.
Unity
Hint
Declare public Sprite spriteToShow; and public Texture2D textureToShow; inside the class.
3
Assign the sprite and texture in the Start method
Inside the ImageSetup class, write a Start() method. In it, assign spriteToShow to the sprite property of imageComponent. Also assign textureToShow to the texture property of rawImageComponent.
Inside the Start() method, after assigning the images, add two Debug.Log statements. One should print "Image component set with sprite." and the other should print "RawImage component set with texture.".
Unity
Hint
Use Debug.Log("Image component set with sprite."); and Debug.Log("RawImage component set with texture."); inside Start().
Practice
(1/5)
1. Which Unity UI component is best suited for displaying a sprite with color tinting?
easy
A. RawImage component
B. Image component
C. Text component
D. Button component
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.
A. The sprite must be converted to a texture first
B. You must use 'rawImg.texture' to assign a sprite
C. RawImage does not have a 'sprite' property
D. The code should use 'rawImg.image' instead of 'rawImg.sprite'
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 C
Quick 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
A. Use RawImage component and modify its uvRect property
B. Use Image component and change its sprite's UVs directly
C. Use RawImage component and change the color property
D. Use Image component and animate its color alpha
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 A
Quick Check:
Scrolling texture = RawImage + uvRect [OK]
Hint: Scroll textures by changing RawImage.uvRect [OK]