Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)

时间:2021-07-12 15:48:12
上篇文章中实现了基本的打包功能,在这篇我们来解决不同平台打AB包的问题。 本篇文章的核心api还是:


[C#]  纯文本查看  复制代码
?
 
BuildPipeline.BuildAssetBundles
(outPath, 0, EditorUserBuildSettings.activeBuildTarget);    

在第三个参数中,只要传入不同平台 BuildTarget就可以了。目前只考虑和iOS平台。

区分iOS、Android平台:
  很简单,只要在上篇文章的QABEditor类中将原来的BuildAssetBundle方法分为BuildAssetBundleiOS和BuildAssetBundleAndroid即可。代码如下所示。

[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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的实现如下所示,就不多说了。

[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
         /// <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这个命名非常的傻瓜,会点英文就应该可以理解了。下面贴出实现代码,
[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
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
[C#]  纯文本查看  复制代码
?
 
BuildPipeline.BuildAssetBundles
(outPath, 0, EditorUserBuildSettings.activeBuildTarget); 
  封装的原因是打AB包成功后,要对AB包进行一些处理,比如计算包尺寸,计算哈希或者md5值。主要是为了以后的热更新做准备的。看下QABBuilder核心实现.
[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
         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 ();
                 }
     }
}
 
         {
                 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 ();
                 }
     }


使用方式:
按这里
Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)  
结果看这里(创建了iOS文件夹)
Unity 游戏框架搭建 (十二) 简易AssetBundle打包工具(二)
介绍完毕,睡觉了!

附:我的框架地址:https://github.com/liangxiegame/QFramework
转载请注明地址:凉鞋的笔记:http://liangxiegame.com/