AssetBundle 使用Unload時 報錯問題

因爲instantiate沒有加載完 for循環裏面開始執行下一個instantiate 就會報錯

以下是解決辦法在for 循環裏面

 for (int i = 0; i < xxx.Count; i++)
                    {
                        GameObject aaa= Instantiate<GameObject>(bbb);
                        aaa.transform.SetParent(grid);
                        ccccitem = aaa.AddComponent<ccc>();
                        yield return StartCoroutine(citem.Initialized(xxx[i]));  ///使用yield return 則會執行完一次for 返回完成後纔會下一步
                    }

 

在攜程裏面同樣使用yield return

public IEnumerator Initialized()
        {
            yield return StartCoroutine(LoadIcon());  //加載assetbundle的攜程
        }

 

//新版AssetBundle加載方法

 IEnumerator LoadIcon()
        {
            UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle("file:///" + Application.streamingAssetsPath + "/res/" + currentConfig.iconPath + ".unity3d");

            yield return request.SendWebRequest();

            AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
            //Debug.Log("~~~" + ab);
            Texture2D t = ab.LoadAsset<Texture2D>("huangbo");
            icon.texture = t;
            ab.Unload(false);
            yield return new WaitForFixedUpdate();
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章