unity-------------------打包BuildAssetBundles的使用
unity5打包機制下,一種資源打ab和資源管理的方案。
1.打ab:
1.設置平臺
2.清楚所有資源的assetbundlename:
string[] abNameArr = AssetDatabase.GetAllAssetBundleNames();
AssetDatabase.RemoveAssetBundleName(abNameArr[i],true);
3.創建ab目錄
4.根據配置把需要打包的資源找出來,對每一個setAbName
5.調用BuildPipeline.BuildAssetBundles(ab文件夾路徑,BuildAssetBundleOptions.DeterministicAssetBundle,EditorUserBuildSettins.activeBuildTarget);
6.完成,但這裏要對第4點做一些說明,因為依賴資源:
我們可以選擇一種簡單粗暴的方式處理依賴資源:搞幾個公共依賴assetbundle,把一些公共資源打到這幾個公共ab中,具體就是:
在第4步,我們設置一個資源的abName後,對其依賴進行:
string[] deps = AssetDatabase.GetDependencies(sourcePath);
if (deps[i] 需要打入公共依賴1)
AssetImporter aIt = AssetImporter.GetAtPath(deps[i]);
aIt.assetBundleName = 公共依賴1.assetBundle.
if (deps[i] 需要打入公共依賴2)
...
其他的依賴就不必繼續分離了,即打到各個資源的ab裏,雖然會增加ab的大小,但依賴資源管理會方便很多。
2.資源管理:
先加載各個公共依賴到內存,並且一直引用這不unload,其他正式資源加載時自己加載對應的ab即可,無需加載依賴了。
具體代碼如下:(啟動遊戲先加載公共依賴,一樣的加載方式)
//原始資源引用
UnityEngine.Object m_ResGameObj = null;
//3個異步加載句柄:
//資源包加載句柄,引用www加載ab時結果
WWW m_LoadHandle = null;
//資源包加載請求,引用loadfromfileasync和loadfrommemoryasync的結果
AssetBundleCreateRequest m_Request = null;
//資源加載請求,引用loadassetasync結果
AssetBundleRequest m_AssetRequest = null;
//資源包,引用各種加載出來的資源包
AssetBundle m_AssetBundle = null;
//資源相對路徑且沒有後綴,作為基礎路徑,方便後續的各種拼接
public string V_AssetPath = null;//是否采用異步
public bool V_UseAsync = true;
//加載優先級
public int V_Priority = 0;
/// <summary>
/// 加載圖片只能用www方式
/// </summary>
/// <param name="path">絕對路徑</param>
/// <returns></returns>
IEnumerator LoadBundleImpImage(string path)
{
m_LoadHandle = new WWW(PathHelper.GetInstance().F_AddFilePro(path));
yield return m_LoadHandle;
if (m_LoadHandle != null && string.IsNullOrEmpty(m_LoadHandle.error) && m_LoadHandle.assetBundle != null)
{
m_AssetBundle = m_LoadHandle.assetBundle;
string[] assets = m_AssetBundle.GetAllAssetNames();
m_ResGameObj = m_AssetBundle.LoadAsset(assets[0]);
}
else if (m_LoadHandle != null && string.IsNullOrEmpty(m_LoadHandle.error) && m_LoadHandle.texture != null)
{
m_ResGameObj = m_LoadHandle.texture;
}
if (m_LoadHandle != null) m_LoadHandle.Dispose();
m_LoadHandle = null;
}
/// <summary>
/// 加載資源imp
/// </summary>
/// <returns></returns>
IEnumerator LoadResImp()
{
ResourceRequest request = Resources.LoadAsync(V_AssetPath);
yield return request;
if (request != null && request.asset != null)
{
m_ResGameObj = request.asset;
}
m_AssetBundle = null;
}
/// <summary>
/// 加載bundle
/// </summary>
/// <param name="path">絕對路徑</param>
/// <returns></returns>
IEnumerator LoadBundleImp(string path)
{
m_Request = AssetBundle.LoadFromFileAsync(path);
yield return m_Request;
if (m_Request != null && m_Request.assetBundle != null)
{
m_AssetBundle = m_Request.assetBundle;
string[] assets = m_AssetBundle.GetAllAssetNames();
m_AssetRequest = m_AssetBundle.LoadAssetAsync(assets[0]);
yield return m_AssetRequest;
m_ResGameObj = m_AssetRequest.asset;
F_SendFinishEvent();
m_Request = null;
m_AssetRequest = null;
}
}
//加載入口
public void StartLoadAsset()
{
string absolutePath = GetPersistentAssetPath();
if (V_UseAsync)
{
string abPath = string.Empty;
if (PathHelper.GetInstance().F_CheckFileExists(absolutePath))
{
abPath = absolutePath;
}
if (abPath.EndsWith(PathHelper.ABFile))
{
AssetManager.GetInstance().StartCoroutine(LoadBundleImp(abPath));
}
else if (abPath.EndsWith(".jpg") || abPath.EndsWith(".png"))
{
AssetManager.GetInstance().StartCoroutine(LoadBundleImpImage(abPath));
}
else//加載安裝包裏的文件,或者編輯器下加載資源使用
{
AssetManager.GetInstance().StartCoroutine(LoadResImp());
}
}
else
{
//同步加載
try
{
if (m_ResGameObj == null)
{
m_AssetBundle = null;
if (AssetManager.GetInstance().F_IsLoadByAb() &&
absolutePath.EndsWith(PathHelper.ABFile))
{
m_AssetBundle = AssetBundle.LoadFromFile(absolutePath);
}
if (m_AssetBundle != null)
{
string[] assets = m_AssetBundle.GetAllAssetNames();
m_ResGameObj = m_AssetBundle.LoadAsset(assets[0]);
}
else
{
m_ResGameObj = Resources.Load(V_AssetPath);
}
}
}
catch
{
}
}
}
//原始資源引用 UnityEngine.Object m_ResGameObj = null; //3個異步加載句柄: //資源包加載句柄,引用www加載ab時結果 WWW m_LoadHandle = null; //資源包加載請求,引用loadfromfileasync和loadfrommemoryasync的結果 AssetBundleCreateRequest m_Request = null; //資源加載請求,引用loadassetasync結果 AssetBundleRequest m_AssetRequest = null; //資源包,引用各種加載出來的資源包 AssetBundle m_AssetBundle = null; //資源相對路徑且沒有後綴,作為基礎路徑,方便後續的各種拼接 public string V_AssetPath = null;//是否采用異步 public bool V_UseAsync = true; //加載優先級 public int V_Priority = 0; /// <summary> /// 加載圖片只能用www方式 /// </summary> /// <param name="path">絕對路徑</param> /// <returns></returns> IEnumerator LoadBundleImpImage(string path) { m_LoadHandle = new WWW(PathHelper.GetInstance().F_AddFilePro(path)); yield return m_LoadHandle; if (m_LoadHandle != null && string.IsNullOrEmpty(m_LoadHandle.error) && m_LoadHandle.assetBundle != null) { m_AssetBundle = m_LoadHandle.assetBundle; string[] assets = m_AssetBundle.GetAllAssetNames(); m_ResGameObj = m_AssetBundle.LoadAsset(assets[0]); } else if (m_LoadHandle != null && string.IsNullOrEmpty(m_LoadHandle.error) && m_LoadHandle.texture != null) { m_ResGameObj = m_LoadHandle.texture; } if (m_LoadHandle != null) m_LoadHandle.Dispose(); m_LoadHandle = null; }
/// <summary> /// 加載資源imp /// </summary> /// <returns></returns> IEnumerator LoadResImp() { ResourceRequest request = Resources.LoadAsync(V_AssetPath); yield return request; if (request != null && request.asset != null) { m_ResGameObj = request.asset; } m_AssetBundle = null; }
/// <summary> /// 加載bundle /// </summary> /// <param name="path">絕對路徑</param> /// <returns></returns> IEnumerator LoadBundleImp(string path) { m_Request = AssetBundle.LoadFromFileAsync(path); yield return m_Request; if (m_Request != null && m_Request.assetBundle != null) { m_AssetBundle = m_Request.assetBundle; string[] assets = m_AssetBundle.GetAllAssetNames(); m_AssetRequest = m_AssetBundle.LoadAssetAsync(assets[0]); yield return m_AssetRequest; m_ResGameObj = m_AssetRequest.asset; F_SendFinishEvent(); m_Request = null; m_AssetRequest = null; } }
//加載入口 public void StartLoadAsset() { string absolutePath = GetPersistentAssetPath(); if (V_UseAsync) { string abPath = string.Empty; if (PathHelper.GetInstance().F_CheckFileExists(absolutePath)) { abPath = absolutePath; } if (abPath.EndsWith(PathHelper.ABFile)) { AssetManager.GetInstance().StartCoroutine(LoadBundleImp(abPath)); } else if (abPath.EndsWith(".jpg") || abPath.EndsWith(".png")) { AssetManager.GetInstance().StartCoroutine(LoadBundleImpImage(abPath)); } else//加載安裝包裏的文件,或者編輯器下加載資源使用 { AssetManager.GetInstance().StartCoroutine(LoadResImp()); } } else { //同步加載 try { if (m_ResGameObj == null) { m_AssetBundle = null; if (AssetManager.GetInstance().F_IsLoadByAb() && absolutePath.EndsWith(PathHelper.ABFile)) { m_AssetBundle = AssetBundle.LoadFromFile(absolutePath); } if (m_AssetBundle != null) { string[] assets = m_AssetBundle.GetAllAssetNames(); m_ResGameObj = m_AssetBundle.LoadAsset(assets[0]); } else { m_ResGameObj = Resources.Load(V_AssetPath); } } } catch {
} } }
unity-------------------打包BuildAssetBundles的使用