关于UGUI图集AssetBundle的加载与释放

首先,声明一个字典用来存储从Bunlde中读取出的Sprite信息

 private Dictionary> spriteDic = new Dictionary>();

1.加载AssetBundle 读取里面的Sprite信息

2.释放AssetBundle

 public Sprite LoadSprite(string atlasName, string spriteName)
    {
        if (spriteDic.ContainsKey(atlasName) == false)
        {
            AssetBundle spriteBundel = AssetBundle.CreateFromFile(PathURL + atlasName + ".assetbundle");
            Sprite[] result = spriteBundel.LoadAllAssets();
            int length = result.Length;
            Dictionary sprites = new Dictionary();
            for (int i = 0; i < length; i++)
            {
                Debug.Log(result[i].name);
                if (sprites.ContainsKey(result[i].name) == false)
                {
                    sprites.Add(result[i].name, result[i]);
                }
            }
            spriteDic.Add(atlasName, sprites);

            //释放bundle
            spriteBundel.Unload(false);
            spriteBundel = null;
            Resources.UnloadUnusedAssets();
        }
        return spriteDic[atlasName][spriteName];
    }

你可能感兴趣的:(Unity3d)