Home

Awesome

Atlas Image

A graphic component use SpriteAtlas for uGUI.
In addition, add useful sprite picker and border editor to the inspector.

image

PRs Welcome

<< Description | Demo | Installation | Usage | Development Note | Change log >>

<br><br><br><br>

Description

Are you still fatigued with SpriteAtlas and Image?

AtlasImage provides useful feature to use SpriteAtlas for UI!

Sprite for renderring can be changed with a SpriteAtlas or a sprite name.

atlasImage.spriteAtlas = Resources.Load("A SpriteAtlas name") as SpriteAtlas;
atlasImage.spriteName = "A sprite name in the SpriteAtlas";

In the inspector, sprite picker displays only sprites in the SpriteAtlas.

image

You can edit the border in the preview window.

image

Convert Image to AtlasImage by context menu.

<br><br><br><br>

Demo

<br><br><br><br>

Installation

Requirement

(For Unity 2018.3 or later) Using OpenUPM

This package is available on OpenUPM.
You can install it via openupm-cli.

openupm add com.coffee.atlas-image

(For Unity 2018.3 or later) Using Git

Find the manifest.json file in the Packages folder of your project and add a line to dependencies field.

To update the package, change suffix #{version} to the target version.

Or, use UpmGitExtension to install and update the package.

For Unity 2018.2 or earlier

  1. Download a source code zip file from Releases page
  2. Extract it
  3. Import it into the following directory in your Unity project
    • Packages (It works as an embedded package. For Unity 2018.1 or later)
    • Assets (Legacy way. For Unity 2017.1 or later)

<br><br><br><br>

Usage

  1. Enable SpriteAtlas. Go to Edit > Project Settings > Editor, and change the sprite packing mode from Disabled to either:
    • Enabled for Builds, when you want to use packing for builds only and not when in Play mode.
    • Always Enabled when you want the packed Sprite to resolve its texture from the Sprite Atlas during Play mode, but resolve its texture from the original Texture during Edit mode.
  2. Add AtlasImage component instead of Image component from Add Component in inspector.
  3. Select the SpriteAtlas by dropdown manu, and select the sprite with object piker.
  4. Enjoy!

<br><br><br><br>

Development Note

How to work?

  1. Pack atlas on open select sprite window.
static void PackAtlas(SpriteAtlas atlas)
{
    System.Type
        .GetType("UnityEditor.U2D.SpriteAtlasUtility, UnityEditor")
        .GetMethod("PackAtlases", BindingFlags.NonPublic | BindingFlags.Static)
        .Invoke(null, new object[]{ new []{ atlas }, EditorUserBuildSettings.activeBuildTarget });
}
  1. Add label <atlas-guid> to sprites in atlas.
static string SetAtlasLabelToSprites(SpriteAtlas atlas, bool add)
{
    // GUID for the atlas. 
    string[] atlasLabel = { AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(atlas)) };

    // Packed sprites in atlas.
    SerializedProperty spPackedSprites = new SerializedObject(atlas).FindProperty("m_PackedSprites");
    Sprite[] sprites = Enumerable.Range(0, spPackedSprites.arraySize)
        .Select(index => spPackedSprites.GetArrayElementAtIndex(index).objectReferenceValue)
        .OfType<Sprite>()
        .ToArray();

    // Add/remove label to sprites.
    foreach (var s in sprites)
    {
        string[] newLabels = add
            ? AssetDatabase.GetLabels(s).Union(atlasLabel).ToArray()
            : AssetDatabase.GetLabels(s).Except(atlasLabel).ToArray();
        AssetDatabase.SetLabels(s, newLabels);
    }
    
    return atlasLabel[0];
}
  1. Open the object picker with label. It filter the sprites to display.
EditorGUIUtility.ShowObjectPicker<Sprite>(atlas.GetSprite(spriteName), false, "l:" + atlasLabel, controlID);
  1. On closed the object picker, remove label from sprites in atlas.

<br><br><br><br>

Contributing

Issues

Issues are very valuable to this project.

Pull Requests

Pull requests are, a great way to get your ideas into this repository.
See sandbox/README.md.

Support

This is an open source project that I am developing in my spare time.
If you like it, please support me.
With your support, I can spend more time on development. :)


<br><br><br><br>

License

Author

See Also