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

最近在看官方的AssetBundle(以下简称AB)的教程,也照着做了一遍,不过做出来的AssetBundleManager的API设计得有些不太习惯。目前想到了一个可行的解决方案。AB相关的内容有点多,所以为了良好的阅读体验,就把教程分为几个小文章,一次写一个点。

1.AssetBundle设置:
  首先要确定一个专门打资源包用的目录,我的框架定的目录是 QArt/QAB,并存放了一些Prefab资源,如下所示。


  然后选定TestAB目录,将Inspector窗口的设置为如下图所示:

  一级名字为testab,二级扩展名为unity3d。
  这样AB就设置好了。

2.制作编辑器工具。
    这里打包的核心API只有一个,就是
[C#]  纯文本查看  复制代码
?
 
BuildPipeline.BuildAssetBundles (outPath, 0, EditorUserBuildSettings.activeBuildTarget); 
贴上编辑器工具代码:

[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
using UnityEditor;
using System.Collections;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
 
namespace QFramework.Editor {
     
     public class QABEditor
     {
         [MenuItem( "QFramework/AB/Build" )]
         public static void BuildAssetBundle()
         {
             // AB包输出路径
             string outPath = Application.streamingAssetsPath + "/QAB" ;
 
             // 检查路径是否存在
             CheckDirAndCreate (outPath);
 
             BuildPipeline.BuildAssetBundles (outPath, 0, EditorUserBuildSettings.activeBuildTarget);
 
             // 刚创建的文件夹和目录能马上再Project视窗中出现
             AssetDatabase.Refresh ();
         }
 
         /// <summary>
         /// 判断路径是否存在,不存在则创建
         /// </summary>
         public static void CheckDirAndCreate( string dirPath)
         {
             if (!Directory.Exists (dirPath)) {
                 Directory.CreateDirectory (dirPath);
             }
        
     }
}

这个脚本要放在Editor目录下!!!
这个脚本要放在Editor目录下!!!
这个脚本要放在Editor目录下!!!
使用方法:
  点击QFramework/AB/Build


  之后,生成的AB包如下所示:

  
AB包就打好了,接下来开始测试AB包的使用。
3.测试:
代码很简单,如下所示,一些常识性的问题就不介绍了。
[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
using UnityEngine;
using System.Collections;
using System.IO;
 
namespace QFramework.Example {
     public class TestABEditor : MonoBehaviour {
 
         // Use this for initialization
         IEnumerator Start () {
 
             WWW www = new WWW ( "file:///" + Application.streamingAssetsPath + Path.DirectorySeparatorChar + "QAB" + Path.DirectorySeparatorChar + "testab.unity3d" );
 
             yield return www;
 
 
             if ( string .IsNullOrEmpty (www.error)) {
                 var go = www.assetBundle.LoadAsset<GameObject> ( "Canvas" );
 
                 Instantiate (go);
             }
             else {
                 Debug.LogError (www.error);
             }
 
         }
 
         // Update is called once per frame
         void Update () {
 
         }
     }
}

运行结果:
最初版的打包工具就做好了,接下来到了吐槽的时刻了。

4.存在的问题:
  1.不支持多平台,只能打当前PlayerSettings所设置的平台的AB包,有点麻烦。
  2.需要手动设置AB包的名字和扩展名,这个问题完全可以交给代码实现。
  3.看不到整个工程究竟有哪些设置了AB包的名字,哪些没设置,有些无关的资源误操作设置了AB包名,要排查这种资源要花些时间。
  4.测试的代码中暴露的字符串太多了,其中包括AB包名,AB包路径,要加载的资源名字,这些都可以集中管理或者生成代码。
  5.欢迎补充

这些问题在此后的文章中一步一步解决,希望大家多给些建议。
附:我的框架地址:https://github.com/liangxiegame/QFramework
转载请注明地址:凉鞋的笔记:http://liangxiegame.com/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值