unity 《专题系列》资源打包 Asset Bundles 第三节 Asset Bundles 加载

unity 《专题系列》资源打包 Asset Bundles 


第三节 Asset Bundles 加载


一、AssetBundle的加载常用的的几种方式

1、AssetBundle.LoadFromMemoryAsync;

2、AssetBundle.LoadFromFile;

3、WWW.LoadFromCacheOrDownload (注意可能会逐渐弃用掉);

4、UnityWebRequest;


二、AssetBundle的加载

1、新建一个场景LoadAssetBundleScene,并且在MainCamera上新建个脚本,如下图

2.1、AssetBundle.LoadFromMemoryAsync 方法加载,在脚本中添加如下代码,运行结果,如下图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;


public class LoadAssetBundle : MonoBehaviour {


	// Use this for initialization
	IEnumerator Start () {


        //1.1 LoadFromMemoryAsync 方法加载
        //AssetBundle的存放路径
        string path = "Assets/AssetBundles/sphereab.unityab";
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        //等待异步加载请求完成
        yield return request;
        AssetBundle ab = request.assetBundle;
        //取得所需要的资源
        GameObject go = ab.LoadAsset<GameObject>("Sphere");
        //加载生成到场景中
        Instantiate(go);
	}
}



2.2、AssetBundle.LoadFromMemory 方法加载,在脚本中添加如下代码,运行结果,如下图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class LoadAssetBundle : MonoBehaviour {

	// Use this for initialization
	void Start () {

        //1.2 LoadFromMemory 方法加载
        //AssetBundle的存放路径
        string path = "Assets/AssetBundles/sphereab.unityab";
        AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
        //取得所需要的资源
        GameObject go = ab.LoadAsset<GameObject>("Sphere");
        //加载生成到场景中
        Instantiate(go);
    }
}


3.1、AssetBundle.LoadFromFile 方法加载,在脚本中添加如下代码,运行结果,如下图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class LoadAssetBundle : MonoBehaviour {

	// Use this for initialization
	IEnumerator Start () {

        //2.1 LoadFromFileAsync 方法加载
        //AssetBundle的存放路径
        string path = "Assets/AssetBundles/sphereab.unityab";
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        //等待异步加载请求完成
        yield return request;
        AssetBundle ab = request.assetBundle;
        //取得所需要的资源
        GameObject go = ab.LoadAsset<GameObject>("Sphere");
        //加载生成到场景中
        Instantiate(go);
    }
}



3.2、AssetBundle.LoadFromFile 方法加载,在脚本中添加如下代码,运行结果,如下图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class LoadAssetBundle : MonoBehaviour {

	// Use this for initialization
	void Start () {

        //2.2 LoadFromFile 方法加载
        //AssetBundle的存放路径
        string path = "Assets/AssetBundles/sphereab.unityab";
        AssetBundle ab = AssetBundle.LoadFromFile(path);
        //取得所需要的资源
        GameObject go = ab.LoadAsset<GameObject>("Sphere");
        //加载生成到场景中
        Instantiate(go);
    }




三、搭建简单的server服务器

NetBox2是一款可以代替IIS的快速ASP服务器搭建工具,我们将用到他来搭建简单服务器环境,可以在网上自行搜索下载,也可以单击下载

1、把“NetBox2”在一个没有中文的路径文件夹下,并新建个文本文件,随便编辑文字,如下图


2、把文本文件改名为“index.html”,双击NetBox2.exe运行,如下图


3、随后把构建好的AssetBundle包放在该文件夹下,即可模拟服务器http://127.0.0.1/*或http://localhost/*访问该资源,如下图




四、WWW 和 UnityWebRequest 加载


注意:1)链接Http时,对应的网页要打开;2)路径正反斜杠注意;

1、WWW 方法加载,在脚本中添加如下代码,运行结果,如下图 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class LoadAssetBundle : MonoBehaviour {

	// Use this for initialization
	IEnumerator Start () {
        //3 WWW 方法加载
        //判断Cache是否准备好
        while (Caching.ready == false) {
            yield return null;
        }
        //AssetBundle本地的存放路径
        //string url = @"file:///D:\Unity2017\Unity2017Project\AssetBundle1112\Assets\AssetBundles\sphereab.unityab";
        //AssetBundle服务器的存放路径
        //string url = @"http:\\localhost\AssetBundles\sphereab.unityab";
        string url = @"http:\\127.0.0.1\AssetBundles\sphereab.unityab";
        WWW www = WWW.LoadFromCacheOrDownload(url, 1);
        yield return www;
        // 判断WWW是否有错误信息,有则退出
        if (string.IsNullOrEmpty(www.error) == false) {
            Debug.Log(www.error);
            yield break;
        }
        AssetBundle ab = www.assetBundle;
        //取得所需要的资源
        GameObject go = ab.LoadAsset<GameObject>("Sphere");
        //加载生成到场景中
        Instantiate(go);

    }
}


2、UnityWebRequest 方法加载,在脚本中添加如下代码,运行结果,如下图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;

public class LoadAssetBundle : MonoBehaviour {

	// Use this for initialization
	IEnumerator Start () {
        //4 UnityWebRequest 方法加载
        //AssetBundle本地的存放路径
        //string url = @"file:///D:\Unity2017\Unity2017Project\AssetBundle1112\Assets\AssetBundles\sphereab.unityab";
        //AssetBundle服务器的存放路径
        //string url = @"http://localhost/AssetBundles/sphereab.unityab";
        string url = @"http://127.0.0.1/AssetBundles/sphereab.unityab";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
        yield return request.SendWebRequest();
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        //AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        //取得所需要的资源
        GameObject go = ab.LoadAsset<GameObject>("Sphere");
        //加载生成到场景中
        Instantiate(go);

    }
}




参考资料:

声明:如无意中涉及侵权到您的内容,请及时沟通修改,谢谢


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仙魁XAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值