赞
踩
使用编辑器扩展方法,将打包按钮Build AssetBundles放于Asset菜单下
using UnityEditor; using System.IO; public class CreateAssetBundles { [MenuItem("Assets/Build AssetBundles")] static void BuildAllAssetBundles() { string dir = "AssetBundles"; if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } //BuildTarget 选择build出来的AB包要使用的平台 BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64); } }
压缩方式
依赖打包
将需要同时加载的资源放在同一个包里,各个包之间会保存相互依赖的信息
在开发阶段将AB包放在本地,开发结束后再上传到服务器
public class LoadFromLocal : MonoBehaviour
{
private void Start()
{
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/scene/wall.jy");
GameObject obj = ab.LoadAsset<GameObject>("wall");
Instantiate(obj);
}
}
private void Start()
{
AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes("AssetBundles/scene/cube.jy"));
GameObject obj = ab.LoadAsset<GameObject>("Cube(Clone)");
Instantiate(obj);
}
IEnumerator Start()
{
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes("AssetBundles/scene/cube.jy"));
yield return request;
AssetBundle ab = request.assetBundle;
GameObject obj = ab.LoadAsset<GameObject>("Cube(Clone)");
Instantiate(obj);
}
IEnumerator Start() { while (Caching.ready == false) { yield return null; } WWW www = WWW.LoadFromCacheOrDownload(@"file://E:\U3D-Projects\Test2017.2.0\AssetBundles\scene\cube.jy", 1); yield return www; if (!string.IsNullOrEmpty(www.error)) { Debug.Log(www.error); yield break; } AssetBundle ab = www.assetBundle; GameObject obj = ab.LoadAsset<GameObject>("Cube(Clone)"); Instantiate(obj); }
IEnumerator Start()
{
string url = @"file:///E:\U3D-Projects\Test2017.2.0\AssetBundles\scene\cube.jy";
UnityWebRequest request = UnityWebRequest.GetAssetBundle(url);
yield return request.SendWebRequest();
//方式一
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//方式二
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
GameObject obj = ab.LoadAsset<GameObject>("Cube(Clone)");
Instantiate(obj);
}
AB加载方式:
从AB中加载资源:
crc为校验码,通过其检查是否完整
Assets 表示包里包含多少资源
Dependencies 表示包有哪些依赖
注意:在加载这些包之前,也需要加载依赖的包,不然会丢失这部分内容,显示效果不正确
可以注意到scene/cube.jy依赖于material.jy,而material.jy依赖于texture.jy
AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//GetAllDependencies获取到所有的依赖对象
string[] strs = manifest.GetAllDependencies("scene/cube.jy");
//将所有依赖对象依次加载出来
foreach (var item in strs)
{
Debug.Log(item);
AssetBundle.LoadFromFile("AssetBundles/" + item);
}
如此,对象的材质也加载显示出来:
CRC、MD5、SHA1都是通过对数据进行计算,来生成一个校验值,该校验值用来校验数据的完整性。
CRC一般用于通信数据的校验,MD5和SHA1用于安全领域,例如文件校验,密码加密等
GitHub下载地址
工具面板如下:
Build Target 用于设置AB包的目标平台
OutPut Path 设置AB的输出路径
Build 一键打包
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。