Unity 3D AssetBunle加載之 UnityWebRequest

從網絡中 或者 StreamingAssets 中 加載資源

C# 代碼部分

IEnumerator LoadFromWebResquet()
    {
        string path = Application.streamingAssetsPath + "/Window/Window";
        
        //根據不用平臺加 前綴
        System.Uri uri = new System.Uri(path);


        UnityWebRequest myRequest = UnityWebRequestAssetBundle.GetAssetBundle(uri.ToString());

        yield return myRequest.SendWebRequest();

        if (string.IsNullOrEmpty(myRequest.error))
        {
            // 加載到鏡像  
            AssetBundle tmpDependenceBundle =  DownloadHandlerAssetBundle.GetContent(myRequest);

            //中間 根據 配置文件  去加載 他的 依賴項

            // 加載某個Perfable
            StartCoroutine(LoadAssetBundle(""));
        }

    }
    IEnumerator LoadAssetBundle(string url)
    {
        string path = Application.streamingAssetsPath + "/Window/js_wk.ld";
        Debug.Log(path);
        System.Uri uri = new System.Uri(path);
        Debug.Log("url=   " + uri.ToString());
        UnityWebRequest myRequest = UnityWebRequestAssetBundle.GetAssetBundle(uri.ToString());

        yield return myRequest.SendWebRequest();

        if (string.IsNullOrEmpty(myRequest.error))
        {
            //第一塊內存 加載 到 鏡像
            AssetBundle tmpBundle = DownloadHandlerAssetBundle.GetContent(myRequest);

            //第二塊內存 解壓 可以加載 多個子資源
            UnityEngine. Object[] allAssets =  tmpBundle.LoadAssetWithSubAssets("JS_wukong.prefab");

            // 釋放鏡像
            tmpBundle.Unload(false);

            // 第三塊內存 實例化到場景

            GameObject.Instantiate(allAssets[0]);

        }
        
    }

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