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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值