赞
踩
格式:左边是AssetBundle名字,默认是None,可以直接New一个新的名字,eg:scenes/grassland(这种写法可用于资源的分类),右面的是后缀,随意写就行。
AssetBundle分组策略 |
---|
把经常更新的资源放到一个单独的包里面 |
把需要同时加载的资源放到一个包里面 |
把其他包共享的资源放到一个单独的包里面 |
把一些需要同时加载的小资源打包成一个包 |
如果同一个资源有两个版本,可以考虑通过后缀来区分 |
在Assets目录下新建一个Editor特殊文件夹,在Editor文件夹下新建一个脚本,如下:
using System.IO; using UnityEditor; public class CreatAssetBundle { [MenuItem("MyTool/CreatAssetBundle")] static void CreatAB() { string dir = "AssetBundles"; //判断路径是否存在 if (Directory.Exists(dir) == false) { //创建路径 Directory.CreateDirectory(dir); } //将所有指定AssetBundle标签的资源打包到dir目录下 //BuildAssetBundleOptions.None 默认使用LZMA的算法压缩,压缩的包更小,但加载时间更长。 BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64); } }
using UnityEngine; public class LoadFileFromAssetBundel : MonoBehaviour { private string dirPlane; // Start is called before the first frame update void Start() { dirPlane = "AssetBundles/mymodel/plane.u3d"; AssetBundle loadPlane = AssetBundle.LoadFromFile(dirPlane); //AssetBundle loadPlane = AssetBundle.LoadFromMemory(File.ReadAllBytes(dirPlane)); //从内存同步加载 //应用1 GameObject plane = loadPlane.LoadAsset<GameObject>("Plane"); Instantiate(plane); //应用2 //遍历的物体标签和后缀要一样 //Object[] ob = ab.LoadAllAssets(); //foreach(Object o in ob) //{ //Instantiate(o); //} } }
LoadFromMemory:同步加载;LoadFromMemoryAsync:异步加载
IEnumerator start()
{
dirModel = "AssetBundles/cap.u3d";
//从内存异步加载 同步加载见上
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(dirModel));
yield return request;
AssetBundle loadModel = request.assetBundle;
GameObject model = loadModel.LoadAsset<GameObject>("Cap");
Instantiate(model);
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class LoadAssetBundles : MonoBehaviour { // Start is called before the first frame update IEnumerator Start() { //使用UnityWebRequest获取本地的AssetBundle(获取服务器端的AssetBudle只需把uri路径更改就行了 string uri = @"file:///D:\UnityDocument\AssetBundleProject\AssetBundles\example/greenland.u3d"; UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri); yield return request.SendWebRequest(); AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request); //使用里面的资源 GameObject go = ab.LoadAsset<GameObject>("land"); Instantiate(go); } }
以下图为例:两个prefab共用材质和贴图,将材质和贴图放到一个包,即名称:share,后缀u3d;prefab1和prefab2分别打一个包,即prefab1,u3d和prefab2,u3d。
using UnityEngine; public class LoadFileFromAssetBundel : MonoBehaviour { private string dirModel; //模型路径 private string dirShare; //共享材质、贴图路径 // Start is called before the first frame update void Start() { dirModel = "AssetBundles/cap.u3d"; dirShare = "AssetBundles/share.u3d"; //加载依赖文件 AssetBundle loadshare = AssetBundle.LoadFromFile(dirShare); //加载模型文件 AssetBundle loadPlane = AssetBundle.LoadFromFile(dirModel); //取得物体,并自动找到并添加依赖 Object[] obj = loadPlane.LoadAllAssets(); foreach (Object o in obj) { Instantiate(o); } } }
1.需要NetBox2.exe以及创建一个index.html的文本文档。
2.准备好之后双击运行。
3.将创建的AssetBundles文件夹也拷到这个目录里来。
//通过Manifest文件得到greenland的所有依赖
AssetBundle manifestAb = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = manifestAb.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] strs = manifest.GetAllDependencies("cap.u3d");
foreach(string name in strs)
{
print(name);
AssetBundle.LoadFromFile("AssetBundles/" + name);
}
CRC MD5 SHA1
都是通过对数据进行计算,生成一个校验值,用来校验数据的完整性。(文件一样则校验值一样) |
---|
CRC 计算效率很高,MD5和 SHA1比较慢 |
CRC的安全性相较于MD5和SHA1较弱,SHA1安全性最高 |
CRC一般用作通信数据的校验,MD5和SHA1用于安全领域 |
下载地址:https://github.com/Unity-Technologies/AssetBundles-Browser
将"AssetBundles-Browser-master"文件夹下的"Editor"文件夹复制到unity "Assets"目录下。
它将在Window > AssetBundle Browser中创建一个新的菜单项。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。