首先,声明一个字典用来存储从Bunlde中读取出的Sprite信息
private Dictionary<string, Dictionary<string, Sprite>> spriteDic = new Dictionary<string, Dictionary<string, Sprite>>();
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<Sprite>();
int length = result.Length;
Dictionary<string, Sprite> sprites = new Dictionary<string, Sprite>();
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];
}