AssetBundles(2)的使用

1,AssetBundle.LoadFromMemoryAsync
2,AssetBundle.LoadFromFile
3,WWW.LoadFromCacheOrDownload
4,UnityWebRequest

加载方式
一般
T objectFromBundle = bundleObject.LoadAsset(assetName);

GameObject
GameObject gameObject =
loadedAssetBundle.LoadAsset(assetName);

所有资源
Unity.Object[] objectArray =
loadedAssetBundle.LoadAllAssets();

打包
添加标签
物体:
在这里插入图片描述
材质:
在这里插入图片描述
界面
在这里插入图片描述

然后直接build
结果:
在这里插入图片描述
加载方式
1

 private void Start()
    {
        //本地加载
        AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/StandaloneWindows/box.assetbundles");
        GameObject GO = ab.LoadAsset<GameObject>("Cube");
        Instantiate (GO);
        //加载依赖
        AssetBundle abc = AssetBundle.LoadFromFile("AssetBundles/StandaloneWindows/StandaloneWindows");
        AssetBundleManifest manifest = abc.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        string[] dependstr = manifest.GetAllDependencies("box.assetbundles");
        foreach (string name in dependstr)
        {
            AssetBundle.LoadFromFile("Assetbundles/StandaloneWindows/" + name);
        }
    }

所谓的依赖是指物体上需要其它其它包的内容。例如上面的方块中的材质就必须从另一个包获取。
你可以打开记事本观看里面的依赖
在这里插入图片描述
2.

IEnumerator Start()
     {

         //异地内存加载
         AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundles/StandaloneWindows/box.assetbundles"));
         yield return request;
         AssetBundle ab = request.assetBundle;
         GameObject GO = ab.LoadAsset<GameObject>("Cube");
         Instantiate(GO);
         AssetBundle abc = AssetBundle.LoadFromFile("AssetBundles/StandaloneWindows/StandaloneWindows");
         AssetBundleManifest manifest = abc.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
         string[] dependstr = manifest.GetAllDependencies("box.assetbundles");
         foreach (string name in dependstr)
         {
             AssetBundle.LoadFromFile("Assetbundles/StandaloneWindows/" + name);
         }
     }
IEnumerator Start(){
    //异地网络加载
    while(Caching.ready ==false ){
        yield return null;
    }
    //本地
    WWW ww=WWW.LoadFromCacheOrDownload(@"file:///D:\Game\AssertBuilder\AssetBundles\StandaloneWindows\box.assetbundles", 1);
    yield return ww;
    if (!string.IsNullOrEmpty (ww.error)) {
        yield break;
    }
    AssetBundle ab = ww.assetBundle;
    GameObject GO = ab.LoadAsset<GameObject>("Cube");
    Instantiate (GO);


}

部署一下服务器
使用的是NetBox2
将打包的文件放在同级目录下
使用网络加载

 IEnumerator Start(){
    //异地网络加载
    while(Caching.ready ==false ){
        yield return null;
    }
    //服务器
    WWW ww=WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/StandaloneWindows/box.assetbundles", 1);
    yield return ww;
    if (!string.IsNullOrEmpty (ww.error)) {
        yield break;
    }
    AssetBundle ab = ww.assetBundle;
    GameObject GO = ab.LoadAsset<GameObject>("Cube");
    Instantiate (GO);


}

    IEnumerator Start()

	{
		
		while(Caching.ready ==false ){
			yield return null;
		}
		string uri = @"file:///D:\Game\AssertBuilder\AssetBundles\StandaloneWindows\box.assetbundles";
        
		UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
        yield return request.SendWebRequest();
		//AssetBundle ab = DownloadHandlerAssetBundle.GetContent (request );
		AssetBundle ab=(request .downloadHandler as DownloadHandlerAssetBundle).assetBundle ;
		GameObject GO = ab.LoadAsset<GameObject>("Cube");
		Instantiate (GO);
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值