【Unity3D自学记录】AssetBundles的使用

来源:互联网 发布:在淘宝被骗了怎么办 编辑:程序博客网 时间:2024/05/17 22:32

 一共有两种方法下载AssetBundles数据资源:

第一种是无缓存:这种方法直接使用WWW类,下载完的数据不会在本地unity3d的缓存目录中进行保存。

第二种有缓存:使用WWW.LoadFromCacheOrDownload的方法,下载完的数据将在unity3d的本地缓存目录中进行保存。

Web浏览器通常允许缓存大小达到50MB,PC和MAC的本地应用,IOS和Android应用都允许缓存达到4GB大小。

官方提供缓存的服务,需要RMB的。很贵。基本不会买的。


首先建立AssetBundles

下面是创建.assetbundle文件的脚本

using UnityEngine;using System.Collections;using UnityEditor;public class AssetBundleTest : Editor{[MenuItem("Custom Editor/Create AssetBunldes Main")]static void CreateAssetBunldesMain (){        //获取在Project视图中选择的所有游戏对象Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);         //遍历所有的游戏对象foreach (Object obj in SelectedAsset) {//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径//StreamingAssets是只读路径,不能写入//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";if (BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)) {  Debug.Log(obj.name +"资源打包成功");} else { Debug.Log(obj.name +"资源打包失败");}}//刷新编辑器AssetDatabase.Refresh ();}[MenuItem("Custom Editor/Create AssetBunldes ALL")]static void CreateAssetBunldesALL (){Caching.CleanCache (); string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle"; Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets); foreach (Object obj in SelectedAsset) {Debug.Log ("Create AssetBunldes name :" + obj);}//这里注意第二个参数就行if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {AssetDatabase.Refresh ();} else {}}[MenuItem("Custom Editor/Create Scene")]static void CreateSceneALL (){//清空一下缓存Caching.CleanCache();string Path = Application.dataPath + "/MyScene.unity3d";string  []levels = {"Assets/Level.unity"};    //打包场景    BuildPipeline.BuildPlayer( levels, Path,BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);AssetDatabase.Refresh ();}}




   下面是不含缓存的代码:

using System;using UnityEngine;using System.Collections; public class Example : MonoBehaviour {  public string URL;  IEnumerator Start() {  WWW www = new WWW(URL);        yield return www;        AssetBundle bundle = www.assetBundle;        Instantiate(bundle.mainAsset);        bundle.Unload(false);    }}


   下面是含缓存的代码,系统建议利用WWW.LoadFromCacheOrDownload类进行下载:

using System;using UnityEngine;using System.Collections; public class Example : MonoBehaviour {  public string URL;  IEnumerator Start() {  WWW www = WWW.LoadFromCacheOrDownload(URL, 1);        yield return www;        AssetBundle bundle = www.assetBundle;        Instantiate(bundle.mainAsset);        bundle.Unload(false);    }}

CSDN博客真搞不懂啊。

下面都是什么东西啊。

                                             
0 0
原创粉丝点击