Skip to content
Wonderful Code See
Wonderful Code See

Master the Code, Shape Your Future

  • Home
  • IT Consulting
  • Artificial Intelligence
    • AI Applications
  • CS Fundamentals
    • Data Structure and Algorithm
    • Computer Network
  • System Design
  • Programming
    • Python Stack
    • .NET Stack
    • Mobile App Development
    • Web Development
    • Unity Tutorials
    • IDE and OA
  • Technology Business
    • Website building tutorials
  • Dev News
Wonderful Code See

Master the Code, Shape Your Future

Unity in Practice 0009 – How to Use Sprite Sheets in Unity for 2D Animation and UI

WCSee, May 8, 2025May 17, 2025

In Unity, a sprite sheet is a single image file that contains multiple smaller graphics (sprites), often used for 2D animation, character states, or tilemaps. It helps improve performance and makes organizing related sprites easier.


✅ Common Use Cases:

  • Character animation (walk / run / jump frames)
  • Environment tiles (grass, bricks, water, etc.)
  • UI elements (buttons, icons)

✅ How to Get and Import a Free Sprite Sheet

In this tutorial, we use a free asset from the Unity Asset Store: Warrior Free Asset | 2D Characters | Unity Asset Store

🧩 Steps to Add the Asset:

  1. Open the asset’s page and click “Add to My Assets.”
  2. Launch the project via Unity Hub — this will open the Unity Package Manager.

In the Package Manager, select the asset, then click Download and Import:

The imported Assets will be shown in the project window:


🧰 How to Use a Sprite Sheet in Unity:

  1. Import the Sprite Sheet:
    • Drag your image into the Unity Assets folder.
  2. Set the Texture Type:
    • Select the image.
    • In the Inspector, set Texture Type to Sprite (2D and UI).
  3. Set Sprite Mode:
    • Change Sprite Mode to Multiple (instead of Single).
  4. Open Sprite Editor:
    • Click the Sprite Editor button.
    • Use the Slice tool to divide the sheet into individual sprites.
      • Auto Slice = Unity tries to detect boundaries.
      • Manual Slice = You specify cell size or grid.
  5. Apply Changes:
    • Click Apply in the top right of the Sprite Editor.
  6. Use Sprites in Your Game:
    • Drag sliced sprites into the Scene.
    • Use them in animations or as components in GameObjects.

Change the Pixels Per Unit of Sprite Sheet, from 100 to 16 in case the sprite is too small.


🎮 Add Sprite to the Scene

  1. Rename the existing Ball sprite to Player.
  2. Create an empty GameObject as a child of Player and name it Animator.
  3. Add a Sprite Renderer component to the Animator GameObject.
  4. Drag the first Warrior sprite from the asset pack into the Sprite Renderer’s Sprite field.

⚙️ Set Up Collider and Rigidbody for the Player

  1. Remove the existing Circle Collider 2D from the Player GameObject.
  2. Add a Capsule Collider 2D to the Player. Adjust its size if needed to match the Warrior sprite.
  3. In the Rigidbody 2D component of Player, update the following properties:
    • Collision Detection → set to Continuous
    • Interpolate → set to Interpolate
    • Freeze Rotation → check the box for Z Axis

🎬 Add Animation to the Animator GameObject

  1. The imported Warrior asset already includes several animations and an Animator Controller.
  2. Assign the Warrior’s Animator Controller to the Animator GameObject by dragging it into the Controller field of the Animator component.
  3. Double-click the Animator Controller to open it in the Animator window.
  4. In the Animator window, click the Parameters tab and add a new Bool parameter named IsRun.

🔁 Step 05: Add Transition Between Idle and Run

  1. Open the Animator window
    Double-click the Animator Controller to open it (Window > Animation > Animator).
  2. Verify Animation States
    Make sure both Idle and Run animations are present as states in the Animator graph.
  3. Create Transitions
    • Right-click on the Idle state and select “Make Transition”, then click on the Run state.
    • Do the same from Run back to Idle.
  4. Set Transition Conditions
    • Click the Idle → Run transition.
    • In the Inspector, under Conditions, click the + button.
    • Select the IsRun parameter and set it to true.
    • For the Run → Idle transition, add the condition IsRun equals false.
  5. (Optional) Tweak transition settings
    You can adjust transition duration and exit time if needed for smoother switching.

Final Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallScript : MonoBehaviour
{

    private Rigidbody2D rb;
    private Animator playerAnim;

    [SerializeField] private float moveSpeed;
    [SerializeField] private float jumpForce;

    private float xInput;
    [SerializeField] private bool isRun;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        playerAnim = GetComponentInChildren<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        // Get horizontal input
        xInput = Input.GetAxis("Horizontal");

        // Apply horizontal movement
        rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);


        // Jump on Space key press
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }

        isRun = rb.velocity.x != 0;

        playerAnim.SetBool("IsRun", isRun);
    }
}

Troubleshooting

Issue 1: When use Package Manager to add the asset, got following error:

[Package Manager Window] Error while getting auth code: System.InvalidOperationException: Failed to call Unity ID to get auth code.
UnityEditor.AsyncHTTPClient:Done (UnityEditor.AsyncHTTPClient/State,int)

Issue 1 Solution: Open Unity Hub and sign in again. Restart Unity Editor afterward.


Please follow and like us:
RSS
Facebook
Facebook
fb-share-icon
X (Twitter)
Visit Us
Follow Me
Tweet
Pinterest
Pinterest
fb-share-icon
Post Views: 157

Related posts:

Unity in Practice 0012 – Blend Tree in Unity Unity in Practice 0014 – Unity 2D Dash and Dash Cooldown with Time.deltaTime Unity in Practice 0011 – Player Jump with Ground Check in Unity Unity in Practice 0010 – Flipping a 2D Character Horizontally in Unity Unity Game Development – Systematic Learning Roadmap (2025 Edition) Unity in Practice 0008 – Encapsulation and Inspector Access with Unity’s [SerializeField] Unity in Practice 0003 – Unity Editor Windows and Tools Unity in Practice 0007 – Very First Unity C# Code to Move and Jump a 2D Ball
Unity Tutorials Game DevelopmentUnity DevelopmentUnity Free AssetUnity Sprite Sheet

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Free HTTPS Certificate Setup: A Complete Guide for CentOS 7 + Nginx + Let’s Encrypt
  • Understanding Architecture Evolution: Monolith, Microservices, and PBC
  • A Comprehensive Guide to AI Agents: Definition, Role, Examples, and Future Prospects
  • The History of Artificial Intelligence (AI): From Turing to ChatGPT
  • Clone a WordPress with ASP.NET and React Part 2: Create ASP.NET Projects Code Files with AI
  • Clone a WordPress with ASP.NET and React Part 1: Initialize Project Structure with AI
  • Clone a WordPress with ASP.NET Core and React: An AI-Assisted Development Journey
  • Artificial Intelligence (AI) Learning Roadmap for Beginners in 2025
  • Set Up and Customize Website Using WordPress | Building Website Tutorials Part 4
  • How to Export Wide Excel sheet to PDF Without Cutting Columns

Recent Comments

    Categories

    • Artificial Intelligence (6)
      • AI Applications (1)
    • CS Fundamentals (1)
      • Computer Network (1)
    • IT Consulting (24)
    • Programming (20)
      • .NET Stack (3)
      • IDE and OA Tool Tips (1)
      • Python Stack (1)
      • Unity Tutorials (15)
    • System Design (5)
    • Technology Business (7)
      • Website building tutorials (6)

    Archives

    • June 2025 (5)
    • May 2025 (52)
    ©2025 Wonderful Code See | WordPress Theme by SuperbThemes
    Manage Consent
    To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
    Functional Always active
    The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
    Preferences
    The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
    Statistics
    The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
    Marketing
    The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
    Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
    View preferences
    {title} {title} {title}