Resources 性能消耗较大
Resources文件夹大小不能超过2个G
获取AssetBundle中的资源
打包流程
选择图片后点击
创建文件夹,Editor优先编译
打包文件夹位置
using UnityEditor;
using UnityEngine;public class SimpleBuild : Editor
{
[MenuItem("AssetBundle/EasyBuild")]
public static void EasyBuild() {
BuildPipeline.BuildAssetBundles(Application.dataPath+ "/ABPackage"/*Bundle输入路径*/,
//BuildAssetBundleOptions.None-->LZMA包小,加载慢
BuildAssetBundleOptions.ChunkBasedCompression,/*包中等,加载快*/
//BuildAssetBundleOptions.UncompressedAssetBundle--->不压缩,加载快
//目标平台 ,苹果平台BuildTarget.StandaloneOSX
BuildTarget.StandaloneWindows64
);
}
}
加载
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class SimpleLoad : MonoBehaviour
{
public RawImage showImg;
private void Start()
{
//简单文件加载
AssetBundle loadedPic=AssetBundle.LoadFromFile(Application.dataPath + "/ABPackage/unitypicture");
//解包
Texture texture = loadedPic.LoadAsset<Texture>("Unity");
showImg.texture = texture;
}
}
p161
using UnityEditor;
using UnityEngine;
using System.IO;public class SimpleBuild : Editor
{
//需要打包的资源都放在这个文件夹下
private static string needBuildPath = Application.dataPath + "/Res";
//打包好后,存储的文件夹
private static string outPutPath = Application.dataPath + "/ABPackage";[MenuItem("AssetBundle/EasyBuild")]
public static void EasyBuild() {
BuildPipeline.BuildAssetBundles(Application.dataPath+ "/ABPackage" /*Bundle输入路径*/,
//BuildAssetBundleOptions.None-->LZMA包小,加载慢
BuildAssetBundleOptions.ChunkBasedCompression,/*包中等,加载快*/
//BuildAssetBundleOptions.UncompressedAssetBundle--->不压缩,加载快
//目标平台 ,苹果平台BuildTarget.StandaloneOSX
BuildTarget.StandaloneWindows64
);
}
[MenuItem("AssetBundle/AutoBuild")]
public static void AutoBuild() {
//清空所有文件的Bundle名称
ClearAllFilesBundleName();
//设置资源的Bundle名
SetAssetBundlesName(needBuildPath);
//Build Asset Bundle
EasyBuild();
//清空所有文件的Bundle名称
ClearAllFilesBundleName();
//刷新
AssetDatabase.Refresh();
}
/// <summary>
/// 清空所有文件的Bundle名称
/// </summary>
private static void ClearAllFilesBundleName() {
//获取当前工程的所有文件名称
string[] allPath=AssetDatabase.GetAllAssetBundleNames();
for (int i = 0; i < allPath.Length; i++) {
//强制移除文件的BundleName
AssetDatabase.RemoveAssetBundleName(allPath[i],true);
}
Debug.Log("清空所有Bundle名称【完成】");
}/// <summary>
/// 设置资源的Bundle名称
/// </summary>
/// <param name="rootPath"></param>
private static void SetAssetBundlesName(string rootPath) {
//获取根路径的文件夹
DirectoryInfo rootInfo = new DirectoryInfo(rootPath);
//获取根路径下的所有文件(夹)
FileSystemInfo[] fileInfos=rootInfo.GetFileSystemInfos();
for (int i = 0; i < fileInfos.Length; i++) {
//如果该文件是个文件夹
if (fileInfos[i] is DirectoryInfo) {
//递归遍历子文件夹下的所有文件
SetAssetBundlesName(fileInfos[i].FullName);
}
else if (!fileInfos[i].Name.EndsWith(".meta"))
{
//设置该文件的Bundle名
SetAssetBundleName(fileInfos[i].FullName);
}
}
Debug.Log("设置所有资源的Bundle名称【完成】");
}
private static void SetAssetBundleName(string filePath) {
//c://abd/def/Lesson10/Assets/Res/unity.png
//获取文件相对路径
string relativePath=filePath.Substring(Application.dataPath.Length + 1);
//Debug.Log(relativePath);
//拼凑导入需要的相对路径
string importerPath ="Assets/" + relativePath;
//去前缀,Res/Texture/unity.png
relativePath=relativePath.Substring(relativePath.LastIndexOf("\\")+1);
Debug.Log(relativePath);
//去后缀,unity[.png]
relativePath = relativePath.Remove(relativePath.LastIndexOf("."));
//Debug.Log(relativePath);
//通过相对路径得到importer对象
AssetImporter importer=AssetImporter.GetAtPath(importerPath);if (importer != null) {
//最终设置Bundle名
importer.assetBundleName = relativePath;
}
}
}
测试加载
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class SimpleLoad : MonoBehaviour
{
public RawImage showImg;
private void Start()
{
//简单文件加载
AssetBundle loadedPic=AssetBundle.LoadFromFile(Application.dataPath + "/ABPackage/unity");
//解包
//Texture texture = loadedPic.LoadAsset<Texture>("Unity");
//showImg.texture = texture;
AssetBundle loadedmat = AssetBundle.LoadFromFile(Application.dataPath + "/ABPackage/mat");
AssetBundle loadedCube = AssetBundle.LoadFromFile(Application.dataPath + "/ABPackage/cube");GameObject cube=loadedCube.LoadAsset<GameObject>("Cube");
Instantiate(cube);
}
}
加载流程
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;public class NormalLoad : MonoBehaviour
{
[Header("本地路径")]
public string localPath = "ABPackage";
[Header("资源的Bundle名称")]
public string loadAssetBundleName = "model";
[Header("资源的文件名称")]
public string loadAssetFileName = "Model";
//资源列表说明文字
private string manifestName;
//资源列表说明文件的全路径
private string manifestFullPath;
[Header("版本号")]
public int version = 1;[Header("服务器地址")]
public string serverAddress = "";//面板记得添加服务器地址
private void Start()
{
//LoadByFile();
//StartCoroutine(LoadByWWW());
StartCoroutine(LoadByWebRequest());
}/// <summary>
/// 通过文件加载
/// </summary>
private void LoadByFile() {
if (localPath.Contains("/"))
{
//拿到资源所在文件的名称
manifestName = localPath.Substring(localPath.LastIndexOf("/") + 1);
}
else {
//拿到资源所在文件的名称
manifestName = localPath;
}
//获取资源的全路径
//[://ABC/Project/Assets]/[Res/ABPackage]/[ABPackage]
manifestFullPath = Application.dataPath + "/" + localPath + "/" + manifestName;
//加载Manifest文件Bundle
AssetBundle manifestBundle=AssetBundle.LoadFromFile(manifestFullPath);//加载真正的Manifest文件
AssetBundleManifest manifestFile=manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//Debug.Log(manifestBundle);
//获取要加载的资源的所有依赖
string[] depandancies=manifestFile.GetAllDependencies(loadAssetBundleName);
//实例化Bundle数组
AssetBundle[] depandenciesBundle = new AssetBundle[depandancies.Length];for (int i=0;i<depandancies.Length;i++) {
//Debug.Log(depandancies[i]);
//拼凑依赖文件的全路径
string depPath = Application.dataPath + "/" + localPath + "/" + depandancies[i];
//加载依赖文件
depandenciesBundle[i]= AssetBundle.LoadFromFile(depPath);
}//拼凑真正需要加载的资源全路径
string realAssetPath = Application.dataPath + "/" + localPath + "/" + loadAssetBundleName;//加载
AssetBundle realBundle=AssetBundle.LoadFromFile(realAssetPath);
//Load预设体
Object realAsset =realBundle.LoadAsset(loadAssetFileName);if (realAsset is GameObject) {
Instantiate(realAsset, Vector3.zero, Quaternion.identity);
}
}private IEnumerator LoadByWWW() {
if (localPath.Contains("/"))
{
//拿到资源所在文件的名称
manifestName = localPath.Substring(localPath.LastIndexOf("/") + 1);
}
else
{
//拿到资源所在文件的名称
manifestName = localPath;
}
//获取资源的全路径
//[://ABC/Project/Assets]/[Res/ABPackage]/[ABPackage]
manifestFullPath = Application.dataPath + "/" + localPath + "/" + manifestName;//WWW对象
WWW www = null;#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
www=WWW.LoadFromCacheOrDownload("file:///" +manifestFullPath,version);
#endif#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
www=WWW.LoadFromCacheOrDownload("file://" +manifestFullPath,version);
#endifyield return www;
#region WWW Load
//加载Manifest文件Bundle
AssetBundle manifestBundle = www.assetBundle;//加载真正的Manifest文件
AssetBundleManifest manifestFile = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//Debug.Log(manifestBundle);
//获取要加载的资源的所有依赖
string[] depandancies = manifestFile.GetAllDependencies(loadAssetBundleName);
//实例化Bundle数组
AssetBundle[] depandenciesBundle = new AssetBundle[depandancies.Length];for (int i = 0; i < depandancies.Length; i++)
{
//Debug.Log(depandancies[i]);
//拼凑依赖文件的全路径
string depPath = Application.dataPath + "/" + localPath + "/" + depandancies[i];
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
www = WWW.LoadFromCacheOrDownload("file:///" + depPath, version);
#endif#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
www=WWW.LoadFromCacheOrDownload("file://" +depPath,version);
#endif
yield return www;
//加载依赖文件
depandenciesBundle[i] = www.assetBundle;
}//拼凑真正需要加载的资源全路径
string realAssetPath = Application.dataPath + "/" + localPath + "/" + loadAssetBundleName;
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
www = WWW.LoadFromCacheOrDownload("file:///" + realAssetPath, version);
#endif#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
www=WWW.LoadFromCacheOrDownload("file://" +realAssetPath,version);
#endif
//等待下载
yield return www;
//加载
AssetBundle realBundle = www.assetBundle;
//Load预设体
Object realAsset = realBundle.LoadAsset(loadAssetFileName);if (realAsset is GameObject)
{
Instantiate(realAsset, Vector3.zero, Quaternion.identity);
}
#endregion
}private IEnumerator LoadByWebRequest()
{
if (localPath.Contains("/"))
{
//拿到资源所在文件的名称
manifestName = localPath.Substring(localPath.LastIndexOf("/") + 1);
}
else
{
//拿到资源所在文件的名称
manifestName = localPath;
}
//获取资源的全路径
//[://ABC/Project/Assets]/[Res/ABPackage]/[ABPackage]
manifestFullPath = Application.dataPath + "/" + localPath + "/" + manifestName;
//服务器地址 manifestFullPath = serverAddress + "/" + localPath + "/" + manifestName;//通过WebRequest加载
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(manifestFullPath);
yield return request.SendWebRequest();//加载Manifest文件Bundle
AssetBundle manifestBundle = DownloadHandlerAssetBundle.GetContent(request);//加载真正的Manifest文件
AssetBundleManifest manifestFile = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//Debug.Log(manifestBundle);
//获取要加载的资源的所有依赖
string[] depandancies = manifestFile.GetAllDependencies(loadAssetBundleName);
//实例化Bundle数组
AssetBundle[] depandenciesBundle = new AssetBundle[depandancies.Length];for (int i = 0; i < depandancies.Length; i++)
{
//Debug.Log(depandancies[i]);
//拼凑依赖文件的全路径
string depPath = Application.dataPath + "/" + localPath + "/" + depandancies[i];
//服务器地址 string depPath = serverAddress + "/" + localPath + "/" + depandancies[i];
//通过WebRequest加载
request = UnityWebRequestAssetBundle.GetAssetBundle(depPath);
yield return request.SendWebRequest();
//加载依赖文件
depandenciesBundle[i] = DownloadHandlerAssetBundle.GetContent(request);
}//拼凑真正需要加载的资源全路径
string realAssetPath = Application.dataPath + "/" + localPath + "/" + loadAssetBundleName;
//服务器地址 string realAssetPath = serverAddress + "/" + localPath + "/" + loadAssetBundleName;//通过WebRequest加载
request = UnityWebRequestAssetBundle.GetAssetBundle(realAssetPath);
yield return request.SendWebRequest();
//加载
AssetBundle realBundle = DownloadHandlerAssetBundle.GetContent(request);
//Load预设体
Object realAsset = realBundle.LoadAsset(loadAssetFileName);if (realAsset is GameObject)
{
Instantiate(realAsset, Vector3.zero, Quaternion.identity);
}
}}
卸载AssetBundle资源
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;public class NormalLoad : MonoBehaviour
{
[Header("本地路径")]
public string localPath = "ABPackage";
[Header("资源的Bundle名称")]
public string loadAssetBundleName = "model";
[Header("资源的文件名称")]
public string loadAssetFileName = "Model";
//资源列表说明文字
private string manifestName;
//资源列表说明文件的全路径
private string manifestFullPath;
[Header("版本号")]
public int version = 1;[Header("服务器地址")]
public string serverAddress = "";//面板记得添加服务器地址
private void Start()
{
//LoadByFile();
//StartCoroutine(LoadByWWW());
StartCoroutine(LoadByWebRequest());
}/// <summary>
/// 通过文件加载
/// </summary>
private void LoadByFile() {
if (localPath.Contains("/"))
{
//拿到资源所在文件的名称
manifestName = localPath.Substring(localPath.LastIndexOf("/") + 1);
}
else {
//拿到资源所在文件的名称
manifestName = localPath;
}
//获取资源的全路径
//[://ABC/Project/Assets]/[Res/ABPackage]/[ABPackage]
manifestFullPath = Application.dataPath + "/" + localPath + "/" + manifestName;
//加载Manifest文件Bundle
AssetBundle manifestBundle=AssetBundle.LoadFromFile(manifestFullPath);//加载真正的Manifest文件
AssetBundleManifest manifestFile=manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//Debug.Log(manifestBundle);
//获取要加载的资源的所有依赖
string[] depandancies=manifestFile.GetAllDependencies(loadAssetBundleName);
//实例化Bundle数组
AssetBundle[] depandenciesBundle = new AssetBundle[depandancies.Length];for (int i=0;i<depandancies.Length;i++) {
//Debug.Log(depandancies[i]);
//拼凑依赖文件的全路径
string depPath = Application.dataPath + "/" + localPath + "/" + depandancies[i];
//加载依赖文件
depandenciesBundle[i]= AssetBundle.LoadFromFile(depPath);
}//拼凑真正需要加载的资源全路径
string realAssetPath = Application.dataPath + "/" + localPath + "/" + loadAssetBundleName;//加载
AssetBundle realBundle=AssetBundle.LoadFromFile(realAssetPath);
//Load预设体
Object realAsset =realBundle.LoadAsset(loadAssetFileName);if (realAsset is GameObject) {
Instantiate(realAsset, Vector3.zero, Quaternion.identity);
}
}private IEnumerator LoadByWWW() {
if (localPath.Contains("/"))
{
//拿到资源所在文件的名称
manifestName = localPath.Substring(localPath.LastIndexOf("/") + 1);
}
else
{
//拿到资源所在文件的名称
manifestName = localPath;
}
//获取资源的全路径
//[://ABC/Project/Assets]/[Res/ABPackage]/[ABPackage]
manifestFullPath = Application.dataPath + "/" + localPath + "/" + manifestName;//WWW对象
WWW www = null;#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
www=WWW.LoadFromCacheOrDownload("file:///" +manifestFullPath,version);
#endif#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
www=WWW.LoadFromCacheOrDownload("file://" +manifestFullPath,version);
#endifyield return www;
#region WWW Load
//加载Manifest文件Bundle
AssetBundle manifestBundle = www.assetBundle;//加载真正的Manifest文件
AssetBundleManifest manifestFile = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//Debug.Log(manifestBundle);
//获取要加载的资源的所有依赖
string[] depandancies = manifestFile.GetAllDependencies(loadAssetBundleName);
//实例化Bundle数组
AssetBundle[] depandenciesBundle = new AssetBundle[depandancies.Length];for (int i = 0; i < depandancies.Length; i++)
{
//Debug.Log(depandancies[i]);
//拼凑依赖文件的全路径
string depPath = Application.dataPath + "/" + localPath + "/" + depandancies[i];
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
www = WWW.LoadFromCacheOrDownload("file:///" + depPath, version);
#endif#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
www=WWW.LoadFromCacheOrDownload("file://" +depPath,version);
#endif
yield return www;
//加载依赖文件
depandenciesBundle[i] = www.assetBundle;
}//拼凑真正需要加载的资源全路径
string realAssetPath = Application.dataPath + "/" + localPath + "/" + loadAssetBundleName;
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
www = WWW.LoadFromCacheOrDownload("file:///" + realAssetPath, version);
#endif#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
www=WWW.LoadFromCacheOrDownload("file://" +realAssetPath,version);
#endif
//等待下载
yield return www;
//加载
AssetBundle realBundle = www.assetBundle;
//Load预设体
Object realAsset = realBundle.LoadAsset(loadAssetFileName);if (realAsset is GameObject)
{
Instantiate(realAsset, Vector3.zero, Quaternion.identity);
}
#endregion
}private IEnumerator LoadByWebRequest()
{
if (localPath.Contains("/"))
{
//拿到资源所在文件的名称
manifestName = localPath.Substring(localPath.LastIndexOf("/") + 1);
}
else
{
//拿到资源所在文件的名称
manifestName = localPath;
}
//获取资源的全路径
//[://ABC/Project/Assets]/[Res/ABPackage]/[ABPackage]
manifestFullPath = Application.dataPath + "/" + localPath + "/" + manifestName;
//服务器地址 manifestFullPath = serverAddress + "/" + localPath + "/" + manifestName;//通过WebRequest加载
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(manifestFullPath);
yield return request.SendWebRequest();//加载Manifest文件Bundle
AssetBundle manifestBundle = DownloadHandlerAssetBundle.GetContent(request);//加载真正的Manifest文件
AssetBundleManifest manifestFile = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//Debug.Log(manifestBundle);
//获取要加载的资源的所有依赖
string[] depandancies = manifestFile.GetAllDependencies(loadAssetBundleName);
//卸载manifestBundle
manifestBundle.Unload(true);
//实例化Bundle数组
AssetBundle[] depandenciesBundle = new AssetBundle[depandancies.Length];for (int i = 0; i < depandancies.Length; i++)
{
//Debug.Log(depandancies[i]);
//拼凑依赖文件的全路径
string depPath = Application.dataPath + "/" + localPath + "/" + depandancies[i];
//服务器地址 string depPath = serverAddress + "/" + localPath + "/" + depandancies[i];
//通过WebRequest加载
request = UnityWebRequestAssetBundle.GetAssetBundle(depPath);
yield return request.SendWebRequest();
//加载依赖文件
depandenciesBundle[i] = DownloadHandlerAssetBundle.GetContent(request);
}//拼凑真正需要加载的资源全路径
string realAssetPath = Application.dataPath + "/" + localPath + "/" + loadAssetBundleName;
//服务器地址 string realAssetPath = serverAddress + "/" + localPath + "/" + loadAssetBundleName;//通过WebRequest加载
request = UnityWebRequestAssetBundle.GetAssetBundle(realAssetPath);
yield return request.SendWebRequest();
//加载
AssetBundle realBundle = DownloadHandlerAssetBundle.GetContent(request);
//Load预设体
Object realAsset = realBundle.LoadAsset(loadAssetFileName);if (realAsset is GameObject)
{
Instantiate(realAsset, Vector3.zero, Quaternion.identity);
}realBundle.Unload(true);
//卸载依赖
for (int i = 0; i < depandenciesBundle.Length; i++) {
depandenciesBundle[i].Unload(false);
}
}}