AssetBundle资源包
资源管理
文件 在硬盘存着 在服务器存着
常用的资源可以用Resources.Load加载
不太常用,或者比较大的资源要用AssetBundle打包,放到服务器上
资源有依赖关系
插件需要放在Editor里面或者子文件里面
AssetBundleSimpleBuild
(1)第一步取AssetBundle名字
找到资源 点一下 Inspector监视器面板最下面 命名
using UnityEngine;
using UnityEditor;//引用命名空间
using System.IO;
public class AssetBundleSimpleBuild : Editor {//继承编译器这个类
//需要打包的资源路径
public static string assetPath = Application.dataPath + "/NeedBuild";
//打包后的资源路径
public static string outputPath = Application.dataPath + "/OutputAssetBundle";
//使用特性设置为菜单项
//[MenuItem("AssetBundle/SimpleBuildOSX")]
//创建静态方法【public 无参数】
public static void SimpleBuildOSX(){
//输出的路径
//压缩模式 枚举
//LZMA默认方式 压缩的贼很,加载慢,解压需要整体解压
//LZ4推荐用这个 压缩中等 可以跟不压缩的加载速度媲美
//Uncompressed不压缩 包大 加载快
//指定一个平台
BulidPipeline.BuildAssetBundles(outputPath,
BuildAssetBundleOptions.ChunkBasedCompression,
BuildTarget.StandaloneOSX);
}
//[MenuItem("AssetBundle/SimpleBuildWindows64")]
public static void SimpleBuildWindows64(){
BulidPipeline.BuildAssetBundles(outputPath,
BuildAssetBundleOptions.ChunkBasedCompression,
BuildTarget.StandaloneWindows64);
}
[MenuItem("AssetBundle/AutoBuildOSX")]
public static void AutoBuildOSX(){
//清除名称
ClearAssetBundleNames();
//设置名称
SetFilesAssetBundleName(assetPath);
//打包
SimpleBuildOSX();
//清除名称 <----看情况写不写
//ClearAssetBundleNames();
//刷新资源
AssetDatabase.Refresh();
}
[MenuItem("AssetBundle/AutoBuildWindows64")]
public static void AutoBuildWindows64(){
//清除名称
ClearAssetBundleNames();
//设置名称
SetFilesAssetBundleName(assetPath);
//打包
SimpleBuildWindows64();
//清除名称 <----看情况写不写
//ClearAssetBundleNames();
//刷新资源
AssetDatabase.Refresh();
}
public static void SetFilesAssetBundleName(string path){
//获取该路径下的所有文件名称
string[] filesName = Directory.GetFiles(path);
//获取该路径下的所有子文件夹的名称
string[] directorysName = Directory.GetDirectories(path);
for(int i = 0; i < directorysName.Length; i++){
//递归设置
SetFilesAssetBundleName(directorysName[i]);
}
for(int i = 0; i < filesName.Length; i++){
//过滤meta文件
if(filesName[i].EndsWith(".meta"))
continue;
//设置某个文件的Bundle名称
SetAssetBundleName(filesName[i]);
}
}
//设置某个文件的Bundle名称
private static void SetAssetBundleName(string path){
//得到不完整的相对路径
string relativePath = path.Substring(Application.dataPath.Length);
//然后再加上Assets
relativePath = "Assets" + relativePath;
//声明bundle名称,去掉了前面的路径,带后缀名
string bundleName = relativePath.Substring(relativePath.LastIndexOf('/') + 1);
//去掉后缀
bundleName = bundleName.Remove(bundleName.LastIndexOf('.'));
//通过相对路径获取资源导入对象
AssetImporter asset = AssetImporter.GetAtPath(relativePath);
//设置Bundle名称
asset.assetBundleName = bundleName;
}
//清除Bundle名称 防止有的命名 有的没有
private static void ClearAssetBundleNames(){
//获取所有的Bundle名称
string[] names = AssetDatabase.GetAllAssetBundleNames();
//遍历
for(int i = 0; i < names.Length; i++){
//强制移除bundle名称
AssetDatabase.RemoveAssetBundleName(names[i],true);
}
}
}
打包后的数据
ManifestFileVersion: 0 //版本号
CRC: 3106621443 //校验码
AssetBundleManifest:
AssetBundleInfos: //信息
Info_0:
Name: cube //AssetBundle名字
Dependencies: //依赖
Dependency_0: mat
Info_1:
Name: mat
Dependencies:
Dependency_0: tex
Info_2:
Name: tex
Dependencies: {}
加载资源
using UnityEngine;
using System.Collections;
using System.IO;
public class LoadAssetsDemo : MonoBehaviour {
[Header("版本号")]
public int version = 1;
[Header("加载本地资源")]
public bool loadLocal = true;
[Header("资源的bundle名称")]
public string assetBundleName;
[Header("资源的真正文件名称")]
public string assetRealName;
//bundle所在的路径
private string assetBundlePath;
//bundle所在的文件夹名称
private string assetBundleRootName;
private void Awake(){
assetBundlePath = Application.dataPath + "/OutputAssetBundle";
assetBundleRootName = assetBundlePath.Substring(assetBundlePath.LastIndexOf("/") + 1);
}
private void Start(){
//StartCoroutine(LoadNoDepandenceAsset());
//StartCoroutine(LoadAssetsByWWW());
//StartCoroutine(LoadAssetsByFile());
StartCoroutine(LoadAssetsByMemory());
}
/*
WWW是有缓存的 即使删了 也能加载出来 可以换版本号
*/
private IEnumerator LoadAssetsByWWW(){
string path = "";
if(loadLocal){
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
path += "File:///";
#endif
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
path += "File://";
#endif
}
//获取要加载的资源路径【bundle总说明文件】
string path += assetBundlePath + "/" + assetBundleRootName;
//加载
WWW www = WWW.LoadFromCacheOrDownload(path,version);
//等待下载
yield return www;
//获取其中的bundle
AssetBundle manifestBundle = www.assetBundle;
//获取到说明文件
AssetBundleManifest manifest = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//获取资源的所有依赖
string[] dependencies = manifest.GetAllDependencies(assetBundleName);
//卸载Bundle和解压出的Manifest对象
manifestBundle.Unload(true);
//获取到相对路径
path = path.Remove(path.LastIndexOf("/") + 1);
//获取资源的所有依赖
string[] dependencies = manifest.GetAllDependencies(assetBundleName);
//获取到相对路径
path = path.Remove(path.LastIndexOf("/"));
//声明依赖的Bundle数组
AssetBundle[] depAssetBundles = new AssetBundle[dependencies.Length];
//遍历加载所有的依赖
for(int i = 0; i < dependencies.Length;i++){
//获取到依赖Bundle的路径
string depPath = path + "/" + dependencies[i];
//获取新的路径进行加载
www.WWW.LoadFromCacheOrDownload(depPath,version);
//等待下载
yield return www;
//将依赖临时保存
depAssetBundles[i] = www.assetBundle;
}
//获取路径
path += "/" + assetBundleName;
//加载终极资源
www = WWW.LoadFromCacheOrDownload(path,version);
//等待下载
yield return www;
//获取到资源的Bundle
AssetBundle realAssetBundle = www.assetBundle;
//加载真正资源
GameObject prefab = realAssetBundle.LoadAsset<GameObject>(assetRealName);
Instantiate(prefab);
//卸载依赖
for(int i = 0;i < depAssetBundles.Length;i++){
depAssetBundles[i].Unload(false);
}
//卸载主资源Bundle
realAssetBundle.Unload(true);
}
/*
加载比WWW要快点 不过仅限本地
*/
private IEnumerator LoadAssetsByFile(){
string path = "";
//获取要加载的资源路径【bundle总说明文件】
string path += assetBundlePath + "/" + assetBundleRootName;
//获取其中的bundle
AssetBundle manifestBundle = AssetBundle.loadFromFile(path);
//获取到说明文件
AssetBundleManifest manifest = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//获取资源的所有依赖
string[] dependencies = manifest.GetAllDependencies(assetBundleName);
//获取到相对路径
path = path.Remove(path.LastIndexOf("/") + 1);
//获取资源的所有依赖
string[] dependencies = manifest.GetAllDependencies(assetBundleName);
//获取到相对路径
path = path.Remove(path.LastIndexOf("/"));
//声明依赖的Bundle数组
AssetBundle[] depAssetBundles = new AssetBundle[dependencies.Length];
//遍历加载所有的依赖
for(int i = 0; i < dependencies.Length;i++){
//获取到依赖Bundle的路径
string depPath = path + "/" + dependencies[i];
//将依赖临时保存
depAssetBundles[i] = AssetBundle.LoadFromFile(depPath);
}
//获取路径
path += "/" + assetBundleName;
//获取到资源的Bundle
AssetBundle realAssetBundle = AssetBundle.LoadFromFile(path);
//加载真正资源
GameObject prefab = realAssetBundle.LoadAsset<GameObject>(assetRealName);
Instantiate(prefab);
yield break;
}
private IEnumerator LoadAssetsByMemory(){
string path = "";
//获取要加载的资源路径【bundle总说明文件】
string path += assetBundlePath + "/" + assetBundleRootName;
//获取其中的bundle
AssetBundle manifestBundle = AssetBundle.loadFromMemory(File.ReadAllBytes(path));
//获取到说明文件
AssetBundleManifest manifest = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
//获取资源的所有依赖
string[] dependencies = manifest.GetAllDependencies(assetBundleName);
//获取到相对路径
path = path.Remove(path.LastIndexOf("/") + 1);
//获取资源的所有依赖
string[] dependencies = manifest.GetAllDependencies(assetBundleName);
//获取到相对路径
path = path.Remove(path.LastIndexOf("/"));
//声明依赖的Bundle数组
AssetBundle[] depAssetBundles = new AssetBundle[dependencies.Length];
//遍历加载所有的依赖
for(int i = 0; i < dependencies.Length;i++){
//获取到依赖Bundle的路径
string depPath = path + "/" + dependencies[i];
//将依赖临时保存
depAssetBundles[i] = AssetBundle.loadFromMemory(File.ReadAllBytes(depPath));
}
//获取路径
path += "/" + assetBundleName;
//获取到资源的Bundle
AssetBundle realAssetBundle = AssetBundle.loadFromMemory(File.ReadAllBytes(path));
//加载真正资源
GameObject prefab = realAssetBundle.LoadAsset<GameObject>(assetRealName);
Instantiate(prefab);
yield break;
}
//加载无依赖的资源
private IEnumerator LoadNoDepandenceAsset(){
string path = "";
if(loadLocal){
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
path += "File:///";
#endif
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
path += "File://";
#endif
path += assetBundlePath + "/" + assetBundleName;
//www对象
WWW www = new WWW(path);
//等待下载【到内存】
yield return www;
//获取到AssetBundle
AssetBundle bundle = www.assetBundle;
//加载资源
bundle.LoadAsset<GameObject>(assetRealName);
Instantiate(prefab);
}
}
}