参考自: http://blog.youkuaiyun.com/y1196645376/article/details/52602002 表示感谢
1. Building AssetBundle 打包
给资源设置Asset BundleName 。命名规则为 文件夹/名称.unity3d
using UnityEngine;
using System.Collections;
using UnityEditor;
public class AssetBundleConfig {
/// <summary>
/// 目标平台
/// </summary>
public static readonly BuildTarget BUILD_TARGET = BuildTarget.StandaloneWindows64;
/// <summary>
/// Asset Bundle 的存放地址
/// </summary>
public static readonly string ASSETBUNDLE_PATH= Application.dataPath+ "/AssetBundle/"+ BUILD_TARGET.ToString();
/// <summary>
/// 资源地址
/// </summary>
public static readonly string APPLICATION_PATH = Application.dataPath + "/";
public static readonly string PROJECT_PATH = APPLICATION_PATH.Substring(0, APPLICATION_PATH.Length - 7);
/// <summary>
/// 打包的后缀名
/// </summary>
public static readonly string SUFFIX = ".unity3d";
/// <summary>
/// 需要命名的资源后缀名
/// </summary>
public static readonly string[] SET_ASSETBUNDLE_SUFFIX_ARRAY=new string[] { ".fbx", ".png", ".mat", "prefab", ".sharder" };
/// <summary>
/// 清理命名资源后缀名
/// </summary>
public static readonly string[] CLEAR_ASSETBUNDLE_SUFFIX_ARRAY=new string[] { ".fbx", ".png", ".mat", "prefab", ".sharder" };
}
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class SetAssetBundle : Editor
{
[MenuItem("Assets/AssetBundle/BuildAssetBundle")]
private static void BuildAssetBundle()
{
Debug.Log(AssetBundleConfig.ASSETBUNDLE_PATH);
if (!File.Exists(AssetBundleConfig.ASSETBUNDLE_PATH))
{
Directory.CreateDirectory(AssetBundleConfig.ASSETBUNDLE_PATH);
}
BuildPipeline.BuildAssetBundles(AssetBundleConfig.ASSETBUNDLE_PATH, BuildAssetBundleOptions.None, AssetBundleConfig.BUILD_TARGET);
}
/// <summary>
/// 设置Asset Bundle资源的名字
/// </summary>
[MenuItem("Assets/AssetBundle/SetAssetBundleName")]
private static void SetAssetBundleName()
{
UnityEngine.Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.Assets | SelectionMode.ExcludePrefab);
Debug.Log(SelectedAsset.Length);
if (SelectedAsset.Length != 1) { return; }
string fullPath = AssetBundleConfig.PROJECT_PATH + AssetDatabase.GetAssetPath(SelectedAsset[0]);
if (Directory.Exists(fullPath))
{
DirectoryInfo dir = new DirectoryInfo(fullPath);
FileInfo[] files = dir.GetFiles("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
//Debug.Log(files[i].ToString());
//Debug.Log(i * 1f / files.Length+";"+i+";"+files.Length);
EditorUtility.DisplayProgressBar("设置AssetBundle名称", "正在设置...", (i + 1) * 1f / files.Length);
foreach (var item in AssetBundleConfig.SET_ASSETBUNDLE_SUFFIX_ARRAY)
{
if (files[i].Name.EndsWith(item))
{
Debug.Log(files[i].Name);
string path = files[i].FullName.Substring(AssetBundleConfig.PROJECT_PATH.Length).Replace("\\","/");
Debug.Log(path);
AssetImporter assetImporter = AssetImporter.GetAtPath(path);
if (assetImporter)
{
string temp = fullPath.Substring(AssetBundleConfig.PROJECT_PATH.Length);
string name = path.Substring(temp.Length+1);
string assetbundleName = name.Substring(0, name.LastIndexOf(".")) + AssetBundleConfig.SUFFIX;
Debug.Log(assetbundleName);
assetImporter.assetBundleName = assetbundleName;
}
}
}
}
EditorUtility.ClearProgressBar();
}
}
[MenuItem("Assets/AssetBundle/GetAssetBundleName")]
private static void GetAssetBundleName()
{
string[] names = AssetDatabase.GetAllAssetBundleNames();
foreach (var item in names)
{
Debug.Log(item);
}
}
[MenuItem("Assets/AssetBundle/ClearAssetBundleName")]
private static void ClearAssetBundleName()
{
UnityEngine.Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.Assets | SelectionMode.ExcludePrefab);
Debug.Log(SelectedAsset.Length);
if (SelectedAsset.Length != 1) { return; }
string fullPath = AssetBundleConfig.PROJECT_PATH + AssetDatabase.GetAssetPath(SelectedAsset[0]);
if (Directory.Exists(fullPath))
{
DirectoryInfo dir = new DirectoryInfo(fullPath);
FileInfo[] files = dir.GetFiles("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
//Debug.Log(files[i].ToString());
//Debug.Log(i * 1f / files.Length+";"+i+";"+files.Length);
EditorUtility.DisplayProgressBar("清除AssetBundle名称", "正在清除...", (i + 1) * 1f / files.Length);
foreach (var item in AssetBundleConfig.CLEAR_ASSETBUNDLE_SUFFIX_ARRAY)
{
if (files[i].Name.EndsWith(item))
{
Debug.Log(files[i].Name);
string path = files[i].FullName.Substring(AssetBundleConfig.PROJECT_PATH.Length).Replace("\\", "/");
Debug.Log(path);
AssetImporter assetImporter = AssetImporter.GetAtPath(path);
if (assetImporter)
{
assetImporter.assetBundleName = null;
}
}
}
}
EditorUtility.ClearProgressBar();
}
}
}
2 。加载Asset Bundle.先加载AssetBundleManifest 获得总的依赖文件列表;加载依赖资源;加载物体
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LoadAssetBundle : MonoBehaviour {
private static AssetBundleManifest manifest = null;
private static Dictionary<string, AssetBundle> abDic = new Dictionary<string, AssetBundle>();
public static AssetBundle LoadAB(string abPath)
{
if (abDic.ContainsKey(abPath) == true)
return abDic[abPath];
if (manifest == null)
{
/*
*两种资源加载的方式
*/
/*第一种*/
//Debug.Log("资源地址" + AssetBundleConfig.ASSETBUNDLE_PATH + "/" + AssetBundleConfig.BUILD_TARGET.ToString());
//string mURL = "file://" + AssetBundleConfig.ASSETBUNDLE_PATH + "/" + AssetBundleConfig.BUILD_TARGET.ToString();
//WWW www = WWW.LoadFromCacheOrDownload(mURL, 0);
//if (!string.IsNullOrEmpty(www.error))
//{
// Debug.LogError(www.error);
//}
//else
//{
// AssetBundle manifestBundle1 = www.assetBundle;
// manifest = (AssetBundleManifest)manifestBundle1.LoadAsset("AssetBundleManifest");
//}
/*第二种*/
AssetBundle manifestBundle = AssetBundle.LoadFromFile(AssetBundleConfig.ASSETBUNDLE_PATH + "/" + AssetBundleConfig.BUILD_TARGET.ToString());
manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
}
if (manifest != null)
{
// 2.获取依赖文件列表
string[] cubedepends = manifest.GetAllDependencies(abPath);
for (int index = 0; index < cubedepends.Length; index++)
{
Debug.Log("加载依赖资源" + cubedepends[index]);
// 3.加载所有的依赖资源
LoadAB(cubedepends[index]);
}
/*第一种*/
4.加载资源
//string mURL1 = "file://" +AssetBundleConfig.ASSETBUNDLE_PATH+ "/" + abPath;
//WWW www1 = WWW.LoadFromCacheOrDownload(mURL1, 0);
//if (!string.IsNullOrEmpty(www1.error))
//{
// Debug.LogError(www1.error);
//}
//else
//{
// AssetBundle manifestBundle1 = www1.assetBundle;
// abDic[abPath] = manifestBundle1;
//}
/*第二中*/
// 4.加载资源
abDic[abPath] = AssetBundle.LoadFromFile(AssetBundleConfig.ASSETBUNDLE_PATH + "/" + abPath);
return abDic[abPath];
}
return null;
}
public static Object LoadGameObject(string abName)
{
string abPath = abName + AssetBundleConfig.SUFFIX;
int index = abName.LastIndexOf('/');
string realName = string.Empty;
if (index == -1)
{
index = abName.Length;
realName = abName;
}
else
{
realName = abName.Substring(index + 1, abName.Length - index - 1);
}
LoadAB(abPath);
if (abDic.ContainsKey(abPath) && abDic[abPath] != null)
{
return abDic[abPath].LoadAsset(realName);
}
return null;
}
}
下载地址链接:链接:http://pan.baidu.com/s/1boJZ96n 密码:0vhg