UNITY - 我想将资产包的所有子精灵加载到精灵列表中

UNITY - I would like to load all subsprites of an assetbundle into a sprite list

我想将资产包的所有子精灵加载到精灵列表中。我的 assetbundle 由 SpriteMode 等于 Multiple 的精灵组成。我的 assetbundle 由 SpriteMode 等于 Multiple 的精灵组成。我的 assetbundle 由 SpriteMode 等于 Multiple 的精灵组成。

        CachedAssetBundle cached = new CachedAssetBundle();
        cached.name = StaticStrings.emojisBundleCached;
        using (WWW web = WWW.LoadFromCacheOrDownload(url, cached))
        {
            yield return web;

            AssetBundle remoteAssetBundle = web.assetBundle;

            while (!web.isDone) ;

            Debug.LogError(url);

            String[] names = remoteAssetBundle.GetAllAssetNames();

            foreach (String sp in names)
            {
                Debug.LogError(sp);

                AssetBundleRequest asset = remoteAssetBundle.LoadAssetWithSubAssetsAsync<Sprite>(sp);

                yield return asset;

                Debug.LogError("Size " + asset.allAssets.Length);

                foreach (Sprite s in asset.allAssets)
                {
                    Debug.LogError("Name sub " + s.name);
                }

                // GameManager.Instance.bundleListEmojis.Add(remoteAssetBundle.LoadAssetWithSubAssets<Sprite>(sp.name));
            }
            if (remoteAssetBundle == null)
            {
                Debug.LogError("Failed to download AssetBundle!");
                yield break;
            }
            remoteAssetBundle.Unload(false);
        }

解决方案:使用字典

            CachedAssetBundle cached = new CachedAssetBundle();
            cached.name = StaticStrings.emojisBundleCached;
            using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(url, cached))
            {
                yield return uwr.SendWebRequest();

                if (uwr.result != UnityWebRequest.Result.Success)
                {
                    Debug.Log(uwr.error);
                }
                else
                {
                    AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);

                    String[] names = bundle.GetAllAssetNames();

                    foreach (String spriteName in names)
                    {

                        AssetBundleRequest asset = bundle.LoadAssetWithSubAssetsAsync<Sprite>(spriteName);

                        yield return asset;

                        List<Sprite> listSprite = new List<Sprite>();

                        foreach (Sprite sprite in asset.allAssets)
                        {
                            listSprite.Add(sprite);
                        }
                        GameManager.Instance.dicEmojisAnim.Add(spriteName, listSprite);
                    }
                    bundle.Unload(false);
                }
            }