0
0
UnityHow-ToBeginner ยท 4 min read

How to Use Sprite in Unity: Simple Guide for Beginners

In Unity, a Sprite is a 2D graphic object used for characters, backgrounds, and UI elements. To use a sprite, import an image as a sprite asset, then add it to a SpriteRenderer component on a GameObject to display it in your scene.
๐Ÿ“

Syntax

To use a sprite in Unity, you typically follow this pattern:

  • Import an image and set its Texture Type to Sprite (2D and UI).
  • Create a GameObject in the scene.
  • Add a SpriteRenderer component to the GameObject.
  • Assign the imported sprite to the sprite property of the SpriteRenderer.

This setup displays the sprite in the game view.

csharp
using UnityEngine;

public class SpriteExample : MonoBehaviour
{
    public Sprite mySprite; // Assign this in the Inspector

    void Start()
    {
        SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
        sr.sprite = mySprite; // Set the sprite to display
    }
}
๐Ÿ’ป

Example

This example shows how to create a GameObject with a sprite displayed using a SpriteRenderer component. Assign a sprite image in the Unity Editor to the mySprite field.

csharp
using UnityEngine;

public class DisplaySprite : MonoBehaviour
{
    public Sprite mySprite; // Drag your sprite here in Inspector

    void Start()
    {
        SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
        spriteRenderer.sprite = mySprite;
    }
}
Output
A GameObject appears in the scene showing the assigned sprite image.
โš ๏ธ

Common Pitfalls

  • Not setting the Texture Type: Imported images must have Texture Type set to Sprite (2D and UI) or they won't display as sprites.
  • Forgetting to add SpriteRenderer: Without a SpriteRenderer component, the sprite won't be visible.
  • Sprite not assigned: The sprite property must be assigned a sprite asset, or nothing will show.
  • Sorting order issues: Sprites may be hidden behind others if sorting layers or order in layer are not set properly.
csharp
using UnityEngine;

public class WrongSpriteUsage : MonoBehaviour
{
    void Start()
    {
        // Missing SpriteRenderer component - sprite won't show
        // gameObject.GetComponent<SpriteRenderer>().sprite = null; // This causes error
    }
}

// Correct way:
public class CorrectSpriteUsage : MonoBehaviour
{
    public Sprite mySprite;

    void Start()
    {
        SpriteRenderer sr = gameObject.AddComponent<SpriteRenderer>();
        sr.sprite = mySprite;
    }
}
๐Ÿ“Š

Quick Reference

Remember these key points when using sprites in Unity:

  • Import images as Sprite (2D and UI).
  • Use SpriteRenderer to display sprites on GameObjects.
  • Assign sprites in the Inspector or via script.
  • Adjust sorting layers to control draw order.
  • Use the Sprite Editor to slice spritesheets if needed.
โœ…

Key Takeaways

Import images as Sprite type to use them as sprites in Unity.
Add a SpriteRenderer component to GameObjects to display sprites.
Always assign a sprite asset to the SpriteRenderer's sprite property.
Use sorting layers and order in layer to manage sprite visibility order.
Use the Sprite Editor for slicing spritesheets into multiple sprites.