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的使用的更多相关文章
- 实力封装:Unity打包AssetBundle(大结局)
		→→前情提要:让用户选择要打包的文件←← 大结局:更多选择 Unity打包AssetBundle从入门到放弃系列终于要迎来大结局了[小哥哥表示实在写不动了o(╥﹏╥)o]... 经过上一次的教程,其实 ... 
- UNITY 打包安卓APK
		1,安装JDK.这个直接下就行了. 2,安装android sdk相关.这个比较蛋疼,官网是被墙的.有些网站的包还是需要访问墙外下载的.关键是找对那个能用的包(对我来说就是不FQ). http://p ... 
- unity 打包资源及网络请求资源包
		第一步 导包 在Assets新建一个Editor目录 新建一个Test类 using UnityEngine; using System.Collections; using UnityEditor; ... 
- unity 打包apk安装失败
		Unity 打包Apk安装失败,错误提示:安卓未完成. 解决方案:检查BundleID是否一致 
- SDK,JDk,Unity打包安卓apk
		SDK:软件开发工具包(缩写:SDK.外语全称:Software Development Kit)一般都是一些软件工程师为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的开发工具的集合. ... 
- unity打包iOS上线归纳
		1.去https://developer.apple.com登陆账号去生成打包用的签名文件,生成一个测试用的和一个上线发布用的签名文件 注意:签名文件绑定id必须和应用绑定包命一致,iOSapp唯一性 ... 
- 实力封装:Unity打包AssetBundle(一)
		说明:这是一系列循序渐进的教程,今天先介绍最简单的AssetBundle打包方式. 这是一个由在Unity中需要加载模型而引发出来的一系列坑,为了填坑花了不少时间,如果有需要在Unity中自定义菜单, ... 
- 实力封装:Unity打包AssetBundle(二)
		→前情提要:Unity最基本的AssetBundle打包方式. 第二种打包方式 Unity提供的BuildAssetBundles API还有一个重载形式,看下面↓↓ public static As ... 
- Unity打包/读取AssetBundle资源全教程
		Unity 资源AssetBundle打包 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar ... 
- Unity打包同一文件Hash不一样
		问题起因 游戏开发基本都会涉及到资源版本管理及更新,本文记录我在打包过程中遇到的一小问题: 开过中常用于标记资源版本的方法有计算文件Hash.VCS的版本等. 在Unity中对同一个资源文件进行多次打 ... 
随机推荐
- python(45)内置函数:os.system() 和 os.popen()
			os.system() 和 os.popen() 概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.pope ... 
- 【C/C++】C语言嵌入式编程修炼·背景篇·软件架构篇·内存操作篇
			C 语言嵌入式系统编程修炼之一:背景篇 不同于一般形式的软件编程,嵌入式系统编程建立在特定的硬件平台上,势必要求其编程语言具备较强的硬件直接操作能力.无疑,汇编语言具备这样的特质.但是,归因于汇编语言 ... 
- 事件驱动模型的简单Java实现
			事件驱动模型的原理不再赘述,Swing是不错的实现.别人也有不错的博文来说明原理. 本文的目的是提供一种简单的,可供参考的简短代码,用来帮助理解该模型. Project Navigator Event ... 
- html table 点击跳转
			在tr上加 onclick事件 ,然后再js代码中写 页面的跳转,将参数以url的形式拼接在跳转url上然后再另一个页面以 request.getAttribute接收当然你如果使用了框架 可能在一些 ... 
- 正则表达式-python-无捕获分组与分支选择
			无捕获分组 当你要将一部分规则作为一个整体对它进行某些操作,比如指定其重复次数时,你需要将这部分规则用 (?:) 把它包围起来. 分支条件 在正则表达式中,分支条件是一个很常用的条件. 满足条件A 或 ... 
- Mybatis之工作原理
			1.Mybatis的架构 1.1 Mybatis的框架分层 1.2 MyBatis的实现原理 mybatis底层还是采用原生jdbc来对数据库进行操作的,它支持定制化 SQL.存储过程以及高级映射的优 ... 
- 让cpu跑到100%的bat文件
			请你新建一个叫“virus.bat”的批处理文件,里面填写上如下三段命令: Echo "welcom to the aqjava's world"Tree /fCall virus ... 
- 朴素贝叶斯分类器的应用 Naive Bayes classifier
			一.病人分类的例子 让我从一个例子开始讲起,你会看到贝叶斯分类器很好懂,一点都不难. 某个医院早上收了六个门诊病人,如下表. 症状 职业 疾病 打喷嚏 护士 感冒 打喷嚏 农夫 过敏 头痛 建筑工 ... 
- 前端常用功能记录(二)—datatables表格(转)
			前端常用功能记录(二)—datatables表格 并不是所有的后台开发都有美工和前端工程师来配合做页面,为了显示数据并有一定的美感,jQuery的DataTables插件对于像我这样的前端菜鸟来说真是 ... 
- 【Unity Shader】UnityCG.cginc中一些常用的函数
			// 摄像机方向(视角方向) float3 WorldSpaceViewDir(float4 v) // 根据模型空间中的顶点坐标 得到 (世界空间)从这个点到摄像机的观察方向 float3 Unit ... 
