Sprites are 2D images used in games to show characters, objects, or backgrounds. Creating and importing sprites lets you add visuals to your game easily.
0
0
Sprite creation and import in Unity
Introduction
When you want to add a character image to your 2D game.
When you need to show buttons or icons in your game UI.
When you want to display backgrounds or scenery in a 2D scene.
When you want to animate a character using multiple images.
When you want to import artwork created in other programs into Unity.
Syntax
Unity
1. Import image file into Unity's Assets folder. 2. Select the image in Unity Editor. 3. In Inspector, set Texture Type to 'Sprite (2D and UI)'. 4. Click 'Apply' to confirm. 5. Drag the sprite into the Scene or Hierarchy to use it.
Make sure your image file is in a supported format like PNG or JPG.
Setting Texture Type to 'Sprite' tells Unity to treat the image as a 2D object.
Examples
This adds a player character image to your game scene.
Unity
Import 'player.png' into Assets. Select 'player.png' in Unity. Set Texture Type to 'Sprite (2D and UI)'. Click Apply. Drag 'player' sprite into Scene.
This uses a sprite for a button icon in the user interface.
Unity
Import 'button_icon.png'. Set Texture Type to 'Sprite (2D and UI)'. Use the sprite in UI Canvas as a button image.
This adds a background image to your 2D game scene.
Unity
Import 'background.jpg'. Set Texture Type to 'Sprite (2D and UI)'. Drag sprite into Scene as background layer.
Sample Program
This script assigns an imported sprite to a SpriteRenderer component to show the image in the game.
Unity
using UnityEngine;
public class SpriteExample : MonoBehaviour
{
public SpriteRenderer spriteRenderer;
public Sprite playerSprite;
void Start()
{
// Assign the imported sprite to the SpriteRenderer
spriteRenderer.sprite = playerSprite;
}
}OutputSuccess
Important Notes
Always check the sprite's Pixels Per Unit setting to control its size in the game world.
You can slice a sprite sheet in the Sprite Editor to create multiple sprites from one image.
Use transparent PNGs for sprites to avoid unwanted backgrounds.
Summary
Sprites are 2D images used to show visuals in Unity games.
Import images and set Texture Type to 'Sprite' to use them as sprites.
Drag sprites into the scene or assign them to SpriteRenderer components to display.