A Solution to Sprite Atlas Are Duplicated Into Other Asset Bundles

This post was first published at Medium

0x00 Description

There is a common issue with sprite atlas and asset bundles before Unity 2018.4.6. That is, when using sprite atlas and asset bundle, the sprite atlas texture may be duplicated into other asset bundles. You can find the issue here. This article will discuss how to solve this issue.

0x01 The Issue

First of all, I will demonstrate this issue. There are 4 sprites, which are Icon1, Icon2, Icon3, and SF Window. (They come from Unity Samples: UI) Add them to a Sprite Atlas called new sprite atlas. And there is an uGUI Panel which uses the sprites from the sprite atlas to render some ui elements.

Then I use the AssetBundles-Browser to build the UI Canvas and the Sprite Atlas bundles.

Now, I will use another AssetBundle-related tool called AssetBundle Analyzer to analyze the content of the asset bundles just built. By the way, You need Python 2.7 to run this program. You will also need a tool such as DB Browser for SQLite to query the database.

python analyzer.py /Applications/Unity/Unity.app/Contents/Tools ~/projects/MyGame/AssetBundles

Ok, let’s see what we get there. The main table is called objects and it contains a row for every object in the asset bundles. It’s best to use the object_view view.

As the screenshot shows, the two AssetBundles(canvas and new sprite atlas) include the same texture, the spriteasset texture.

0x02 The Solution

How should we solve this problem now? This problem is caused by the so-called SpriteAtlas dependencies. There is an Include in Build checkbox on the SpriteAtlas inspector editor. When checked, SpriteAtlas will be there as long as you bring the sprite. This is like any asset dependency that Unity check during the conventional build. Which also applies to AssetBundle builds.

When unchecked, on the other hand, the sprite releases its reference to the SpriteAtlas. Therefore, the SpriteAtlas won’t come automatically. And You can use the Late Binding to load the sprite later or at run time.

To do this, let’s disable the Include in Build option. Then at C# script, register the SpriteAtlasManager.atlasRequested callback.

using UnityEngine;
using UnityEngine.U2D;

public class AtlasLoader : MonoBehaviour
{
    void OnEnable()
    {
        SpriteAtlasManager.atlasRequested += RequestAtlas;
    }

    void OnDisable()
    {
        SpriteAtlasManager.atlasRequested -= RequestAtlas;
    }

    void RequestAtlas(string tag, System.Action<SpriteAtlas> callback)
    {
        var ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/new sprite atlas");
        var sa = ab.LoadAsset<SpriteAtlas>(tag);
        callback(sa);
    }
}

Now let’s analyze the the content of the asset bundles via AssetBundle Analyzer. As you can see, there is only one SpriteAtlas texture now.

Ok, remember what I said at the beginning of the article? Yes, it’s an issue before Unity2018.4.6. At Unity2018.4.6, Unity fixes this issue.


Subscribe To Jiadong Chen's Blog

Avatar
Jiadong Chen
Cloud Architect/Senior Developer

Cloud Architect at Company-X | Microsoft MVP, MCT | Azure Certified Solutions Architect & Cybersecurity Architect Expert | Member of .NET Foundation | Packt Author ㅣ Opinions = my own.

comments powered by Disqus

Related