AssetBundle加载

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class AssetBundleLoad : MonoBehaviour
{
    Action<AssetBundleCreateRequest> action;
    string[] paths = new string[] { "cube", "cube1", "cube2", "sphere" };
    void Start()
    {
        foreach (var item in paths)
        {
            StartCoroutine(LoadAsync(item));
        }
        //同步加载
        //string path = "D:/profession/lua/test/AssetsBundleTest2/AssetBundle/StandaloneWindows64/rollerball.assetbundle";
        //AssetBundle bundle = AssetBundle.LoadFromFile(path);
        //GameObject go = bundle.LoadAsset<GameObject>("cube");
        //Instantiate(go);

        //异步加载
        //path = "D:/profession/lua/test/AssetsBundleTest2/AssetBundle/StandaloneWindows64/";
        //StartCoroutine(LoadAssetAsync("rollerball.assetbundle", (Request) =>
        //{
        //    GameObject go = Request.assetBundle.LoadAsset("cube") as GameObject;
        //    Instantiate(go);
        //    Request.assetBundle.Unload(false);
        //}));
    }
    //private IEnumerator LoadAssetAsync(string path, Action<AssetBundleCreateRequest> callback)
    //{
    //    AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(this.path + path);
    //    action += callback;
    //    yield return request;
    //    if (request.isDone)
    //    {
    //        action(request);
    //    }
    //}
    //Instantiate(pf);
    //string url = @"http://127.0.0.1:808/AssetBundles/Cy.unity3d";
    //UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
    //yield return request.Send();
    //    AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
    //GameObject pf = ab.LoadAsset<GameObject>("Cylinder");
    //Instantiate(pf)
    IEnumerator LoadAsync(string path)
    {
        //WWW www = WWW.LoadFromCacheOrDownload("http://127.0.0.1:1599/AssetBundles/" + path+ ".assetbundle", 0);
        //yield return www;
        //if (www.error == null)
        //{
        //    AssetBundle bundle = www.assetBundle;
        //   GameObject[] gg = bundle.LoadAllAssets<GameObject>();
        //    foreach (GameObject item in gg)
        //    {
        //       GameObject game =  Instantiate(item);
        //    }
        //}
        UnityWebRequest request = UnityWebRequest.GetAssetBundle("http://127.0.0.1:1599/AssetBundles/" + path + ".assetbundle");
        yield return request.Send();
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
        GameObject[] gg = bundle.LoadAllAssets<GameObject>();
        foreach (GameObject item in gg)
        {
          Instantiate(item);
        }
    }
}
//string url = @"http://127.0.0.1:808/AssetBundles/Cy.unity3d";
//UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
//yield return request.Send();
//        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);

//GameObject pf = ab.LoadAsset<GameObject>("Cylinder");
//Instantiate(pf);

猜你喜欢

转载自blog.csdn.net/itliruochong/article/details/80302134