Load sprite dynamically from assets in Unity

Hello programmers, In this article, you will learn how to load sprite dynamically from assets in Unity with the help of C# programming.

Before we get started with the building process, we need to know a few concepts. Let’s first discuss them.

Resources

The Resources is a class that allows the script to find and access the GameObject including assets.

All assets in a folder can be accessed by the Resources.Load function.

Resources.Load

Procedure:-

  • Create a Resources folder inside the Assets folder.
  • Create Sprites inside the Resources folder.
  • Now, you can add the sprites in the Sprites folder.

         Assets > Resources > Sprites

  • Add your path to the script as sp that will load the sprites from a given path.
Sprite sp = Resources.Load("Sprites/example") as Sprite;

The below-given program is to load sprite dynamically from assets in Unity using C# programming.

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

public class LoadSprite : MonoBehaviour
{
    // Start is called before the first frame update
    private void Start()
    {
        Sprite sp = Resources.Load("Sprites/example") as Sprite;
    }

}

 

Leave a Reply

Your email address will not be published.