Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)
来源:互联网 发布:瑾珑网络 编辑:程序博客网 时间:2024/06/05 07:29
上篇文章中实现了基本的打包功能,在这篇我们来解决不同平台打AB包的问题。 本篇文章的核心api还是:
BuildPipeline.BuildAssetBundles (outPath, 0, EditorUserBuildSettings.activeBuildTarget);
在第三个参数中,只要传入不同平台 BuildTarget就可以了。目前只考虑Android和iOS平台。
区分iOS、Android平台。
很简单,只要在上篇文章的QABEditor类中将原来的BuildAssetBundle方法分为BuildAssetBundleiOS和BuildAssetBundleAndroid即可。代码如下所示。
public class QABEditor { [MenuItem("QFramework/AB/Build iOS")] public static void BuildABiOS() { string outputPath = QPath.ABBuildOutPutDir (RuntimePlatform.IPhonePlayer); QIO.CreateDirIfNotExists (outputPath); QABBuilder.BuildAssetBundles (BuildTarget.iOS); AssetDatabase.Refresh (); } [MenuItem("QFramework/AB/Build Android")] public static void BuildABAndroid() { string outputPath = QPath.ABBuildOutPutDir (RuntimePlatform.Android); QIO.CreateDirIfNotExists (outputPath); QABBuilder.BuildAssetBundles (BuildTarget.Android); AssetDatabase.Refresh (); }}
大家觉得代码中有几个类有些陌生。下面我来一一介绍下。
QPath.ABBuildOutPutDir(build target)
QPath这个类在我的框架中是用来指定固定的路径用的,因为路径的代码全是字符串,不能让字符串暴露在各处都是,这样会影响代码的可读性。统一管理起来比较方便修改。ABBuildOutPutDir这个API的实现如下所示,就不多说了。
/// <summary> /// 所有的路径常量都在这里 /// </summary> public class QPath { /// <summary> /// 资源输出的路径 /// </summary> public static string ABBuildOutPutDir(RuntimePlatform platform) { string retDirPath = null; switch (platform) { case RuntimePlatform.Android: retDirPath = Application.streamingAssetsPath + "/QAB/Android"; break; case RuntimePlatform.IPhonePlayer: retDirPath = Application.streamingAssetsPath + "/QAB/iOS"; break; case RuntimePlatform.WindowsPlayer: case RuntimePlatform.WindowsEditor: retDirPath = Application.streamingAssetsPath + "/QAB/Windows"; break; case RuntimePlatform.OSXPlayer: case RuntimePlatform.OSXEditor: retDirPath = Application.streamingAssetsPath + "/QAB/OSX"; break; } return retDirPath; } /// <summary> /// 打包之前的源资源文件 /// </summary> public static string SrcABDir { get { return Application.dataPath + "/QArt/QAB"; } } }}
QIO.CreateDirIfNotExists (outputPath)
QIO这个类是用来封装C#的System.IO和一些文件操作相关的API。CreateDirIfNotExists这个命名非常的傻瓜,会点英文就应该可以理解了。下面贴出实现代码,
using UnityEngine; using System.Collections; using System.IO;/// <summary>/// 各种文件的读写复制操作,主要是对System.IO的一些封装/// </summary>namespace QFramework { public class QIO { /// <summary> /// 创建新的文件夹,如果存在则不创建 /// </summary> public static void CreateDirIfNotExists(string dirFullPath) { if (!Directory.Exists (dirFullPath)) { Directory.CreateDirectory (dirFullPath); } } }}
QABBuilder
QABBuilder只是封装了本文的核心API
BuildPipeline.BuildAssetBundles (outPath, 0, EditorUserBuildSettings.activeBuildTarget);
封装的原因是打AB包成功后,要对AB包进行一些处理,比如计算包尺寸,计算哈希或者md5值。主要是为了以后的热更新做准备的。看下QABBuilder核心实现.
public class QABBuilder { public static string overloadedDevelopmentServerURL = ""; public static void BuildAssetBundles(BuildTarget buildTarget) { string outputPath = Path.Combine(QPlatform.ABundlesOutputPath, QPlatform.GetPlatformName()); if (Directory.Exists (outputPath)) { Directory.Delete (outputPath,true); } Directory.CreateDirectory (outputPath); BuildPipeline.BuildAssetBundles(outputPath,BuildAssetBundleOptions.None,buildTarget); GenerateVersionConfig (outputPath); if(Directory.Exists(Application.streamingAssetsPath+"/QAB")){ Directory.Delete (Application.streamingAssetsPath+"/QAB",true); } Directory.CreateDirectory (Application.streamingAssetsPath+"/QAB"); FileUtil.ReplaceDirectory (QPlatform.ABundlesOutputPath,Application.streamingAssetsPath+"/QAB"); AssetDatabase.Refresh (); } }}
使用方式
按这里 结果看这里(创建了iOS文件夹)
介绍完毕,睡觉了!
阅读全文
0 0
- Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)
- Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)
- Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)
- Unity 游戏框架搭建 (十一) 简易AssetBundle打包工具(一)
- Unity 游戏框架搭建 (十一) 简易AssetBundle打包工具(一)
- Unity 游戏框架搭建 (十一) 简易AssetBundle打包工具(一)
- Unity 游戏框架搭建--简易有限状态机
- Unity 游戏框架搭建 (四) 简易有限状态机
- Unity 游戏框架搭建 (四) 简易有限状态机
- Unity 游戏框架搭建 (四) 简易有限状态机
- Unity 游戏框架搭建 (四) 简易有限状态机
- unity 5 Assetbundle 打包 scene 工具
- Unity之AssetBundle自动打包工具
- Unity 游戏框架搭建 (五) 简易消息机制
- Unity 游戏框架搭建 (五) 简易消息机制
- Unity 游戏框架搭建 (五) 简易消息机制
- Unity 资源打包Assetbundle
- Unity 资源打包Assetbundle
- Unity 游戏框架搭建 (十一) 简易AssetBundle打包工具(一)
- Android之Adapter用法总结(转)
- RTP协议学习大总结从原理到代码
- 58题 拉丁方阵
- cocos-js 关于场景点击事件的bug
- Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)
- 欢迎使用CSDN-markdown编辑器
- 智能指针
- jquery选择器
- Html之js修改span的值
- Ubuntu下mysql终端命令大全
- CSS书写
- 总结--卡特兰数
- CSDN Markdown学习