Unity 游戏框架搭建 2019 (四) 导出 UnityPackage

在上一篇中我们搞定了合并的功能。那么在这篇文章我们接着继续。

先列出我们目前的导出步骤:

  1. 点击菜单栏 QFramework/3.生成文件名到剪切板。
  2. 右击 QFramework 文件夹,然后选择 ExportPackage。
  3. 之后确保取消选择 Include Dependency。
  4. 粘贴名字,然后导出到指定目录下。

目前第一步操作已经没有优化空间了。
那么我们看看第二步: 右击 QFramework 文件夹,然后选择 ExportPackage。

ExportPackage 是 Unity 编辑器提供的 Assets 菜单。那么我们可以找一找,是不是有对应的 UnityEditor API。
很幸运地是,在 bing.con 搜索引擎上直接搜索关键字 “unity exportpackage” 就找到了。

API 为:

public static void ExportPackage(string assetPathName, string fileName, ExportPackageOptions flags);

第一个参数 assetPathName:通过名字可以得出,就是要打包的路径名字,就是我们的 QFramework 路径。

第二个参数 fileName 应该就是导出的文件名字,对应我们示例一生中生成的名字。
第三个参数应该是一些选项。我们直接看他的定义。

using System;

namespace UnityEditor
{
  /// <summary>
  ///   <para>Export package option. Multiple options can be combined together using the | operator.</para>
  /// </summary>
  [Flags]
  public enum ExportPackageOptions
  {
    /// <summary>
    ///   <para>Default mode. Will not include dependencies or subdirectories nor include Library assets unless specifically included in the asset list.</para>
    /// </summary>
    Default = 0,
    /// <summary>
    ///   <para>The export operation will be run asynchronously and reveal the exported package file in a file browser window after the export is finished.</para>
    /// </summary>
    Interactive = 1,
    /// <summary>
    ///   <para>Will recurse through any subdirectories listed and include all assets inside them.</para>
    /// </summary>
    Recurse = 2,
    /// <summary>
    ///   <para>In addition to the assets paths listed, all dependent assets will be included as well.</para>
    /// </summary>
    IncludeDependencies = 4,
    /// <summary>
    ///   <para>The exported package will include all library assets, ie. the project settings located in the Library folder of the project.</para>
    /// </summary>
    IncludeLibraryAssets = 8,
  }
}

分别有 5 个:

  1. Default: 默认模式,Include Dependencies 为 false,不包括子目录。所以这个不是我们要的。
  2. Interactive:交互选项,弹出一个窗口,这个和我们直接用默认导出相比没啥区别。所以这个也不是我们要的。
  3. Recurse:递归选项,意思是说包含子目录。我们要用这个。
  4. IncludeDependencies:包含依赖,不是我们需要的。
  5. IncludeLibraryAssets:包含 ProjectSetting 选项,也不是我们要的。

分析了一番,第三个是我们要的。

OK,到此参数都确定下来了。今天的示例代码如下。

using System;
#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;

namespace QFramework
{
    public class ExportUnityPackage : MonoBehaviour
    {
#if UNITY_EDITOR
        [MenuItem("QFramework/4.导出 UnityPackage")]
        private static void MenuClicked()
        {
            var assetPathName = "Assets/QFramework";
            var fileName = "QFramework_" + DateTime.Now.ToString("yyyyMMdd_hh") + ".unitypackage";
            AssetDatabase.ExportPackage(assetPathName, fileName, ExportPackageOptions.Recurse);
        }
#endif
    }
}

这里要注意,fileName 要加上 .unitypacakge 后缀名。

这个代码要创建在 QFramework/4.导出 UnityPackage/ 目录下。

执行的结果为:当点击菜单栏 QFramework/4.导出 UnityPackage 后,在项目目录下(Assets 目录的上一级)会生成我们的 UnityPackage。

这样我们目前的导出步骤为:

  1. 点击菜单栏 QFramework/4.导出 UnityPackage。

一步…

假如每一步节省 5 秒,那么节省了 15 秒,如果积累了 1000 个知识点,那么可以赚到大概 390 块钱。ok,今天又赚到了。

虽然说得有点夸张,但是从小老师就告诉了我们一个简单的道理:“时间就是金钱”。

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

更多内容

猜你喜欢

转载自www.cnblogs.com/liangxiegame/p/12544398.html