Skip to content

AtlasImage is a graphic component use SpriteAtlas for uGUI. In addition, add useful sprite selector and border editor to the inspector.

License

Notifications You must be signed in to change notification settings

li5414/AtlasImage

 
 

Repository files navigation

AtlasImage

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

image

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





Description

Are you still fatigued with SpriteAtlas and Image?

  • No interface for SpriteAtlas
    • Support only Sprite.
    • We pack sprites for drawing call optimization, but there is no interface.
  • Confusing sprite picker
    • You can select sprites using object picker.
    • Opject picker displays all sprites in the project...
    • Do you know which sprite is included in atlas?
  • Troublesome border setting
    • You can edit sprite border using sprite editor.
    • It is troublesome to select a sprite, open a sprite editor, and edit the border.

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.





Demo





Usage

  1. Download AtlasImage.unitypackage from Releases.
  2. Import the package into your Unity project. Go to Assets > Import Package > Custom Package and select AtlasImage.unitypackage.
  3. 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.
  4. Add AtlasImage component instead of Image component from Add Component in inspector.
  5. Select the SpriteAtlas by dropdown manu, and select the sprite with object piker.
  6. Enjoy!
Requirement
  • Unity 2017.1+
  • No other SDK are required





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.





License

Author

mob-sakai

See Also

About

AtlasImage is a graphic component use SpriteAtlas for uGUI. In addition, add useful sprite selector and border editor to the inspector.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 92.6%
  • Shell 7.4%