上篇文章中实现了基本的打包功能,在这篇我们来解决不同平台打 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文件夹)

介绍完毕,睡觉了!

欢迎讨论!

转载请注明地址:凉鞋的笔记:liangxiegame.com

更多内容

Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)的更多相关文章

  1. Unity 游戏框架搭建 (十九) 简易对象池

    在Unity中我们经常会用到对象池,使用对象池无非就是解决两个问题: 一是减少new时候寻址造成的消耗,该消耗的原因是内存碎片. 二是减少Object.Instantiate时内部进行序列化和反序列化 ...

  2. Unity 游戏框架搭建 (十) QFramework v0.0.2小结

    从框架搭建系列的第一篇文章开始到现在有四个多月时间了,这段时间对自己来说有很多的收获,好多小伙伴和前辈不管是在评论区还是私下里给出的建议非常有参考性,在此先谢过各位. 说到是一篇小节,先列出框架的概要 ...

  3. Unity 游戏框架搭建 (十六) v0.0.1 架构调整

    背景: 前段时间用Xamarin.OSX开发一些工具,遇到了两个问题. QFramework的大部分的类耦合了Unity的API,这样导致不能在其他CLR平台使用QFramework. QFramew ...

  4. Unity 游戏框架搭建 (十八) 静态扩展 + 泛型实现transform的链式编程

    本篇文章介绍如何实现如下代码的链式编程: C# this.Position(Vector3.one) .LocalScale(1.0f) .Rotation(Quaternion.identity); ...

  5. Unity 游戏框架搭建 (十五) 优雅的QChain (零)

    加班加了三个月终于喘了口气,博客很久没有更新了,这段期间框架加了很多Feature,大部分不太稳定,这些Feature中实现起来比较简单而且用的比较稳定的就是链式编程支持了. 什么是链式编程? 我想大 ...

  6. Unity 游戏框架搭建 (十四) 优雅的QSignleton(零) QuickStart

      好久不见 !之前想着让各位直接用QFramework,但是后来想想,如果正在进行的项目直接使用QFramework,这样风险太高了,要改的代码太多,所以打算陆续独立出来一些工具和模块,允许各位一个 ...

  7. Unity 游戏框架搭建 2018 (一) 架构、框架与 QFramework 简介

    约定 还记得上版本的第二十四篇的约定嘛?现在出来履行啦~ 为什么要重制? 之前写的专栏都是按照心情写的,在最初的时候笔者什么都不懂,而且文章的发布是按照很随性的一个顺序.结果就是说,大家都看完了,都还 ...

  8. Unity 游戏框架搭建 (十三) 无需继承的单例的模板

    之前的文章中介绍的Unity 游戏框架搭建 (二) 单例的模板和Unity 游戏框架搭建 (三) MonoBehaviour单例的模板有一些问题. 存在的问题: 只要继承了单例的模板就无法再继承其他的 ...

  9. Unity 游戏框架搭建 (十七) 静态扩展GameObject实现链式编程

    本篇本来是作为原来 优雅的QChain的第一篇的内容,但是QChain流产了,所以收录到了游戏框架搭建系列.本篇介绍如何实现GameObject的链式编程. 链式编程的实现技术之一是C#的静态扩展.静 ...

随机推荐

  1. 【VMware&Vritualbox】虚拟机安装windows server2016

    一.下载镜像 参考链接:https://blog.csdn.net/yenange/article/details/52981769 http://blog.sina.com.cn/s/blog_10 ...

  2. [转]ASP.NET Core 中文文档 第四章 MVC(4.3)过滤器

    本文转自:http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_4_3-filters.html 原文:Filters 作者:Steve Smith 翻 ...

  3. SQL Server Profiler(转载)

    SQL Server Profiler工具 一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收 ...

  4. 跨页面传值之QueryString

    跨页面传值常用方法 1.QueryString 2.Form-post控件传递 3.Cookies传递 4.Application传递 5.Session传递(灵活强大) 1.query传值 http ...

  5. Oracle 查询当前系统时间十分钟之前的记录,时间比较SQL

    select * from t_register r ));

  6. HashMap put、get方法源码分析

    HashMap.java的实现是面试必问的问题. JDK版本 java version "1.8.0_91" Java(TM) SE Runtime Environment (bu ...

  7. 2015年创新工场校园招聘软件研发岗位笔试题目——矩阵旋转

    题目要求:给出一个NxN的矩阵,写出程序将该矩阵进行顺时针旋转90度 // matrixrotation.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h ...

  8. wxpython wx.windows的API

    wx.Window is the base class for all windows and represents any visible object on screen. All control ...

  9. 我是一只IT小小鸟读书笔记3

    Part6: 一.    无论在什么时候,师兄师姐都是我们最好的资源,遇到不会的问题一定要及时向他们请教,善于利用身边的人脉关系也是一个基本的技能. 二.    爱好很多,但特长一定要有.仔细思考一下 ...

  10. SQL Server ->> 建立linked server到Azure SQL Server

    EXEC master.dbo.sp_addlinkedserver @server = N'<nick_name_to_use>', @srvproduct=N'', @provider ...