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 = ;
/// <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[]);
}
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[]);
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[]);
}
else
{
m_ResGameObj = Resources.Load(V_AssetPath);
}
}
}
catch
{ }
}
}