UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

assets

Determining Sprite Source Atlas vs Asset

Solution

renderingoptimizationeditor scriptingsprites

Unity 2021.3.x - Unity 6.3.x

Published Thu, Mar 19

Issue

 Identifying whether a Sprite is sourced from a SpriteAtlas or a direct asset is essential for verifying memory usage and ensuring efficient batching during the rendering process.

Access the sprite.texture.name property or utilize the Frame Debugger to identify the active SpriteAtlas and verify memory source.

Explanation

The origin of a loaded Sprite is verified through editor tools or programmatic inspection. Using the Frame Debugger provides visibility into the textures being processed during the render loop.

  1. Open the Frame Debugger via Window > Analysis.
  2. Analyze the draw call to see if the texture belongs to a SpriteAtlas.
  3. Use your script to access the sprite.texture.name property at runtime.
  4. Observe the returned string; it matches the SpriteAtlas name when packed.

Additional Tips

  • Use the Sprite.packed property to verify packing status without string comparison.
  • Check your sprite settings to ensure the Packing Mode is set correctly in the Inspector.
  • Sprites packed into a SpriteAtlas share the same Texture2D reference, which reduces draw calls.

Copy


using UnityEngine;

public class SpriteSourceIdentifier : MonoBehaviour
{
    [SerializeField] private SpriteRenderer spriteRenderer;

    private void Start()
    {
        if (spriteRenderer == null) spriteRenderer = GetComponent<SpriteRenderer>();

        if (spriteRenderer != null && spriteRenderer.sprite != null)
        {
            // The texture name identifies the source
            string sourceName = spriteRenderer.sprite.texture.name;
            bool isPacked = spriteRenderer.sprite.packed;

            Debug.Log($"Sprite: {spriteRenderer.sprite.name} | Source: {sourceName} | Packed: {isPacked}");
        }
    }
}

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.