天天看點

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

部落格遷移

個人部落格站點,歡迎通路,www.jiingfengji.tech

文章目錄

  • 部落格遷移
  • 前言
  • 菜單欄Game Framework
    • Open Folder
    • Scenes in Build Settings
    • Log Scripting Define Symbols
    • AssetBundle Tools
      • AssetBundle工具相關配置
        • AssetBundleEditor.xml
        • AssetBundleCollection.xml
        • AssetBundleBuilder.xml
      • AssetBundle Editor
      • AssetBundle Analyzer
      • AssetBundle Builder
      • Build AssetBundle
    • Documentation
    • API Reference
  • 使用UnityGameFramework編輯器出現的常見問題記錄
    • 1. AssetBundleEditor界面中的Asset清單是空的?

前言

本文将使用StarForce案例,結合源碼和Editor界面介紹一下GF中所有的Editor界面(包括Inspector),持續更新。

StarForce位址:https://github.com/EllanJiang/StarForce

GameFramework位址:https://github.com/EllanJiang/GameFramework

UnityGameFramework位址:https://github.com/EllanJiang/UnityGameFramework

GameFramework官方網站:http://gameframework.cn/

本文所講解的内容如與你使用的架構版本有所差異,請閱讀源碼,源碼即文檔。。

菜單欄Game Framework

Open Folder

待續

Scenes in Build Settings

待續

Log Scripting Define Symbols

待續

AssetBundle Tools

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

AssetBundle工具相關配置

AB的XML配置一共有三個,分别是AssetBundleEditor.xml、AssetBundleCollection.xml、AssetBundleBuilder.xml

在StarForce案例中,它們在Assets/GameMain/Configs檔案中,然而配置的預設路徑并不在這裡,這個路徑是可以自定義的。

//AssetBundleEditorController.cs檔案中
public AssetBundleEditorController()
{
            m_ConfigurationPath = Type.GetConfigurationPath<AssetBundleEditorConfigPathAttribute>() ?? Utility.Path.GetCombinePath(Application.dataPath, "GameFramework/Configs/AssetBundleEditor.xml");
            //...省略
}
           
//AssetBundleCollection.cs檔案中
public AssetBundleCollection()
{
    m_ConfigurationPath = Type.GetConfigurationPath<AssetBundleCollectionConfigPathAttribute>() ?? Utility.Path.GetCombinePath(Application.dataPath, "GameFramework/Configs/AssetBundleCollection.xml");
    m_AssetBundles = new SortedDictionary<string, AssetBundle>();
    m_Assets = new SortedDictionary<string, Asset>();
}
           
//AssetBundleBuilderController.cs檔案中
public AssetBundleBuilderController()
{
    m_ConfigurationPath = Type.GetConfigurationPath<AssetBundleBuilderConfigPathAttribute>() ?? Utility.Path.GetCombinePath(Application.dataPath, "GameFramework/Configs/AssetBundleBuilder.xml");
    //...省略
}
           

從上面摘抄的源碼中可以看出,會先通過Type.GetConfigurationPath接口(AssetBundleEditorConfigPathAttribute、AssetBundleCollectionConfigPathAttribute、AssetBundleBuilderConfigPathAttribute)找到本地定義的路徑,如果沒有找到,則使用後面的預設路徑。【注:Type.GetConfigurationPath接口自行看源碼】

而在StarForce案例中,GameFrameworkConfigs.cs案例中定義了這幾個路徑

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄
GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

AssetBundleEditor.xml

配置格式介紹

<?xml version="1.0" encoding="UTF-8"?>
<UnityGameFramework>
  <AssetBundleEditor>
    <Settings>
    	<!--配置資源搜尋的根目錄,可以從Assets根部全部查找,可以配置成子目錄-->
      <SourceAssetRootPath>Assets/GameMain</SourceAssetRootPath>
      <!--配置資源搜尋的子目錄,相對于根目錄的路徑,支援配置多個子目錄,如果為空,則搜尋所有子目錄-->
      <SourceAssetSearchPaths>
        <SourceAssetSearchPath RelativePath="" />
      </SourceAssetSearchPaths>
      <!--篩選并包含的資源類型-->
      <SourceAssetUnionTypeFilter>t:Scene t:Prefab t:Shader t:Model t:Material t:Texture t:AudioClip t:AnimationClip t:AnimatorController t:Font t:TextAsset t:ScriptableObject</SourceAssetUnionTypeFilter>
      <!--篩選并包含的标簽類型-->
      <SourceAssetUnionLabelFilter>l:AssetBundleInclusive</SourceAssetUnionLabelFilter>
      <!--篩選并排除的資源類型-->
      <SourceAssetExceptTypeFilter>t:Script</SourceAssetExceptTypeFilter>
      <!--篩選并排除的标簽類型-->
      <SourceAssetExceptLabelFilter>l:AssetBundleExclusive</SourceAssetExceptLabelFilter>
      <!--編輯器中資源清單的排序,可以是Name(資源檔案名),Path(資源全路徑),Guid(資源Guid)-->
      <AssetSorter>Path</AssetSorter>
    </Settings>
  </AssetBundleEditor>
</UnityGameFramework>
           

AssetBundleEditorController.cs中的ScanSourceAssets就是通過以上配置搜尋篩選的資源。

public void ScanSourceAssets()
        {
            m_SourceAssets.Clear();
            m_SourceAssetRoot.Clear();

            string[] sourceAssetSearchPaths = m_SourceAssetSearchPaths.ToArray();
            HashSet<string> tempGuids = new HashSet<string>();
            //AssetDatabase.FindAssets接口傳回的是搜尋到的資源清單的guid數組,在Project的搜尋欄中輸入t:prefab也是進行這個接口的操作
            //篩選并包含指定類型的資源
            tempGuids.UnionWith(AssetDatabase.FindAssets(SourceAssetUnionTypeFilter, sourceAssetSearchPaths));
            //篩選并包含指定标簽的資源
            tempGuids.UnionWith(AssetDatabase.FindAssets(SourceAssetUnionLabelFilter, sourceAssetSearchPaths));
            //篩選并排除指定類型的資源
            tempGuids.ExceptWith(AssetDatabase.FindAssets(SourceAssetExceptTypeFilter, sourceAssetSearchPaths));
            //篩選并排除指定标簽的資源
            tempGuids.ExceptWith(AssetDatabase.FindAssets(SourceAssetExceptLabelFilter, sourceAssetSearchPaths));

            string[] assetGuids = new List<string>(tempGuids).ToArray();
            foreach (string assetGuid in assetGuids)
            {
                string fullPath = AssetDatabase.GUIDToAssetPath(assetGuid);
                if (AssetDatabase.IsValidFolder(fullPath))
                {
                    // Skip folder.
                    continue;
                }

                string assetPath = fullPath.Substring(SourceAssetRootPath.Length + 1);
                string[] splitPath = assetPath.Split('/');
                SourceFolder folder = m_SourceAssetRoot;
                for (int i = 0; i < splitPath.Length - 1; i++)
                {
                    SourceFolder subFolder = folder.GetFolder(splitPath[i]);
                    folder = subFolder == null ? folder.AddFolder(splitPath[i]) : subFolder;
                }

                SourceAsset asset = folder.AddAsset(assetGuid, fullPath, splitPath[splitPath.Length - 1]);
                m_SourceAssets.Add(asset.Guid, asset);
            }
        }
           

目前官方沒有提供這個配置的編輯工具,需手動編輯xml檔案。

打開AssetBundleEditor視窗後,根據以上配置篩選資源形成界面的右側樹狀圖。

AssetBundleCollection.xml

這個檔案是通過AssetBundleEditor工具編輯好AB後,生成的檔案。裡面用來記錄包含了哪些AB,AB中分别又包含了哪些資源,也就是對應了AssetBundleEditor視窗的左側清單。

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄
GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

其中看一下AssetBundle和Asset中包含的内容:

AssetBundle:

(1)Name:AB的名稱,主持路徑

(2)LoadType:AB的加載方式,對應下面的枚舉

/// <summary>
    /// 資源加載方式類型。
    /// </summary>
    public enum AssetBundleLoadType
    {
        /// <summary>
        /// 從檔案加載。
        /// </summary>
        LoadFromFile = 0,

        /// <summary>
        /// 從記憶體加載。
        /// </summary>
        LoadFromMemory,

        /// <summary>
        /// 從記憶體快速解密加載。
        /// </summary>
        LoadFromMemoryAndQuickDecrypt,

        /// <summary>
        /// 從記憶體解密加載。
        /// </summary>
        LoadFromMemoryAndDecrypt,
    }
           

(3)Variant:變體

(4)ResourceGroups:資源組

Asset:

(1)Guid:資源的guid

(2)AssetBundleName:配置中上面所記錄的AssetBundleName

(3)AssetBundleVariant:變體

打開AssetBundleEditor視窗後,解析該配置,形成視窗左側的樹狀圖。

AssetBundleBuilder.xml

該配置用來存儲AB打包配置。界面如下圖:

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄
<?xml version="1.0" encoding="UTF-8"?>
<UnityGameFramework>
  <AssetBundleBuilder>
    <Settings>
      <!--内部資源版本号(Internal Resource Version)建議每次自增 1 即可,Game Framework 判定資源包是否需要更新,是使用此編号作為判定依據的。-->
      <InternalResourceVersion>0</InternalResourceVersion>
      <!--打包平台-->
      <Platforms>1</Platforms>
      <!--Zip All AssetBundles是否勾選,true為勾選,false為不勾選-->
      <ZipSelected>True</ZipSelected>
      <!--以下幾個配置分别對應AssetBundleOptions的幾個Option是否勾選,true為勾選,false為不勾選-->
      <UncompressedAssetBundleSelected>False</UncompressedAssetBundleSelected>
      <DisableWriteTypeTreeSelected>False</DisableWriteTypeTreeSelected>
      <DeterministicAssetBundleSelected>True</DeterministicAssetBundleSelected>
      <ForceRebuildAssetBundleSelected>False</ForceRebuildAssetBundleSelected>
      <IgnoreTypeTreeChangesSelected>False</IgnoreTypeTreeChangesSelected>
      <AppendHashToAssetBundleNameSelected>False</AppendHashToAssetBundleNameSelected>
      <ChunkBasedCompressionSelected>True</ChunkBasedCompressionSelected>
      <!--Package輸出路徑是否勾選,True表示勾選即輸出,False表示不勾選即不輸出-->
      <OutputPackageSelected>True</OutputPackageSelected>
      <!--Full輸出路徑是否勾選,True表示勾選即輸出,False表示不勾選即不輸出-->
      <OutputFullSelected>False</OutputFullSelected>
      <!--Packed輸出路徑是否勾選,True表示勾選即輸出,False表示不勾選即不輸出-->
      <OutputPackedSelected>False</OutputPackedSelected>
      <!--Build Event Handler類型名稱-->
      <BuildEventHandlerTypeName>StarForce.Editor.StarForceBuildEventHandler</BuildEventHandlerTypeName>
      <!--AB輸出路徑-->
      <OutputDirectory></OutputDirectory>
    </Settings>
  </AssetBundleBuilder>
</UnityGameFramework>
           

AssetBundle Editor

菜單路徑:Game Framework/AssetBundle Tools/AssetBundle Editor

界面分為三個部分:

AssetBundleList、AssetBundleContent、Asset List

AssetBundleList是通過讀取解析AssetBundleCollection.xml,Asset List是通過AssetBundleEditor.xml裡的配置篩選資源而來,而AssetBundleContent部分在是選中AssetBundleList裡的一個AB後顯示被選中的AB裡包含的資源。

這三個部分下面都有對應的工具菜單。

官方文檔:使用 AssetBundle 編輯工具

  1. AssetBundleList

    (1)以AssetBundles為根結點

    (2)這些AB的結點層級路徑表示打包後的資源相對路徑

    GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

    (3)AB的層級路徑參考AssetBundle的Name屬性,也就是AssetBundleCollection.xml配置中的AssetBundle的Name

    (4)AB結點的名稱字首[Packed]表示其Packed,參考AssetBundleEditor腳本中的DrawAssetBundleItem函數

    (5)AB結點的名稱後面 “.{變體名}”,例如StarForce例子中的Dictionaries.en-us這個AB中的en-us表示其變體名

    (6)通過選中AB後,點選下方的Rename按鈕,在第一個輸入框中輸入層級名稱,即可更改其相對路徑,第二個輸入框是變體名。點選後面ok按鈕或者按回車均可改名。

    (7)不支援多選、不支援拖拽修改相對路徑。

  2. AssetBundleContent

    (1)顯示左側選中的AB裡所包含的資源清單

    (2)All:全部選中

    (3)None:全部不選中

    (4)AssetSorterType枚舉下拉框

public enum AssetSorterType
    {
        Path,
        Name,
        Guid,
    }
           

除了修改排序外,還會修改這個界面顯示的内容,Path對應資源路徑,Name對應資源名,Guid自然是資源的Guid。

(5)右邊的 0 >> 按鈕,數字表示勾選的資源數量,>> 表示将選中的資源移除這個AB,還給右側的Asset List。(又沒有借,哪來的還。。不過>>很形象嘛。。)

3. Asset List

資源樹狀視圖,支援多選

(1)資源如果包含在某個AB裡,其後面會有AssetBundle Name

(2)<<0 按鈕:同理,數字表示選中的資源數量。功能在于将選中的資源 給左側選中的A版中,如果此前資源已經在别的AB中,則會先移出。未選中資源 和 未選中AB 這兩種情況均無法點選該按鈕。

(3)<<< 0 按鈕:同理,數字表示選中的資源數量。那麼與上面按鈕有什麼差別呢?(不就多了一個 < 嗎,難道一個 << 表示移到中間的Asset List,多了一個 < 還能移到 AssetBundleList 中嗎。。)這個按鈕用于批量添加AB,将選中的一個或多個資源作為 AssetBundle 名來建立 AssetBundle,并将自身加入到對應的 AssetBundle 裡。

(4)Hie Assigne:隐藏已經存在于 AssetBundle 中的資源,隻列出尚未指定 AssetBundle 的資源。

(5)Clean:從所有的 AssetBundle 中清理無效的資源,并移除所有空的 AssetBundle。建議 Save 前總是點一下 Clean 按鈕,因為建構 AssetBundle 的時候,Unity 不允許存在無效的資源或者空的 AssetBundle。空的AB在左側中會顯示成灰色,會導緻打包失敗。

(6)Save:儲存目前所有 AssetBundle 和資源的狀态。注意儲存

AssetBundle Analyzer

待續。

AssetBundle Builder

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

界面分為以下幾個部分:

Environment Information:工程的基本資訊

(1)ProductName:PlayerSettings.productName

(2)Company Name:PlayerSettings.companyName

(3)Game Identifier:

public string GameIdentifier
{
    get
    {
#if UNITY_5_6_OR_NEWER
        return PlayerSettings.applicationIdentifier;
#else
        return PlayerSettings.bundleIdentifier;
#endif
    }
}
           

(4)Applicable Game Version:Application.version

以上這些資料在File->Build Settings->Player Settings中可以看到

Platforms:打包平台勾選,支援多選

如果一個平台都不勾選,界面下方會出現紅色警告:Platform undefined.

AssetBundle Options:參考UnityEditor.BuildAssetBundleOptions枚舉

官方文檔介紹:https://docs.unity3d.com/ScriptReference/BuildAssetBundleOptions.html,此處不再贅述

AssetBundleBuilderController.cs腳本中的GetBuildAssetBundleOptions函數會将這裡勾選的選項轉換為BuildAssetBundleOptions

private BuildAssetBundleOptions GetBuildAssetBundleOptions()
{
    BuildAssetBundleOptions buildOptions = BuildAssetBundleOptions.None;
    if (UncompressedAssetBundleSelected)
    {
        buildOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;
    }
    if (DisableWriteTypeTreeSelected)
    {
        buildOptions |= BuildAssetBundleOptions.DisableWriteTypeTree;
    }
    if (DeterministicAssetBundleSelected)
    {
        buildOptions |= BuildAssetBundleOptions.DeterministicAssetBundle;
    }
    if (ForceRebuildAssetBundleSelected)
    {
        buildOptions |= BuildAssetBundleOptions.ForceRebuildAssetBundle;
    }
    if (IgnoreTypeTreeChangesSelected)
    {
        buildOptions |= BuildAssetBundleOptions.IgnoreTypeTreeChanges;
    }
    if (AppendHashToAssetBundleNameSelected)
    {
        buildOptions |= BuildAssetBundleOptions.AppendHashToAssetBundleName;
    }
    if (ChunkBasedCompressionSelected)
    {
        buildOptions |= BuildAssetBundleOptions.ChunkBasedCompression;
    }
    return buildOptions;
}
           

這個BuildAssetBundleOptions最終是傳給了BuildPipeline.BuildAssetBundle函數。由于建構過程需要對生成的 AssetBundle 名稱進行處理,故這裡不允許使用 Append Hash To AssetBundle Name 選項。

Zip All AssetBundles : 壓縮所有 AssetBundles(Zip All AssetBundles)用于指定建構 AssetBundle 後,是否進一步使用 Zip 壓縮 AssetBundle 包。

Build部分:

Build Event Handler:

這裡會顯示一個實作了IBuildEventHandler接口的所有TypeName的下拉框,例如StarForce工程中StarForceBuildEventHandler類

接口包含以下部分:

(1)ContinueOnFailure:擷取當某個平台生成失敗時,是否繼續生成下一個平台。

(2)PreprocessAllPlatforms:所有平台生成開始前的預處理事件

(3)PostprocessAllPlatforms:所有平台生成結束後的後處理事件

(4)PreprocessPlatform:某個平台生成開始前的預處理事件

(5)PostprocessPlatform:某個平台生成結束後的後處理事件

大家可以根據自己的需求定義不同的Handler。例如:StarForce案例中:

PreprocessAllPlatforms函數中清空了StreamingAssets檔案夾。

PostprocessPlatform函數中把outputPackagePath裡的檔案給拷貝到工程的StreamingAssets目錄下。

Internal Resource Version:

内部資源版本号(Internal Resource Version)建議每次自增 1 即可,Game Framework 判定資源包是否需要更新,是使用此編号作為判定依據的。

Resource Version:

資源版本号(Resource Version)根據目前 App 版本号和内部資源版本号自動生成,并不會存儲在AssetBundleBuilder.xml中。

Output Directory:

輸出目錄(Output Directory)用于指定建構過程的結果輸出目錄,絕對路徑,支援直接輸入(記得回車),右邊Browse按鈕可以選擇路徑。

如果路徑無效,下方會提示Output directory is invalid.

Output Directory選中後,下方會出現Working Path、Output Package Path、Output Full Path、Output Packed Path、Build Report Path這五個子路徑。

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

當這些配置都配好後,就會出現提示:Ready to build.

點選Start Build AssetBundles按鈕開始打包,點選Save按鈕儲存配置到AssetBundleBuilder.xml配置中。

打包成功:

GameFramework架構源碼解讀(一):Editor篇部落格遷移前言菜單欄Game Framework使用UnityGameFramework編輯器出現的常見問題記錄

打包完後,再來介紹這五個目錄:

(1)BuildReport:

該目錄包含了兩個檔案,BuildLog.txt和BuildReport.xml。BuildLog.txt用來記錄打包日志,打包失敗的時候可以通過檢視該日志來判斷是哪一步出現了問題。AssetBundleBuilderController腳本中通過m_BuildReport.LogInfo接口進行日志記錄。而BuildReport.xml檔案則用來記錄本次打包設定、本次打包的AB資訊、AB中包含的Asset資訊等等。

(2)Full:為可更新模式生成的完整檔案包

(3)Package:為單機模式生成的檔案,若遊戲是單機遊戲,生成結束後将此目錄中對應平台的檔案拷貝至 StreamingAssets 後打包 App 即可。

(4)Packed:為可更新模式生成的檔案

(5)Working:Unity生成AssetBundle時的工作目錄

請注意:

不知道從哪個版本開始,GameResourceVersion_x_x_x.xml檔案不再生成。

相關資料可以在BuildReport目錄下的BuildLog.txt中檢視到,如下:

[15:51:32.383][INFO] Process version list for 'MacOS' complete, version list path is '/Users/teitomonari/Documents/WorkSpace/UnityProjects/Study/StarForce/StarForce/AssetBundle/Full/0_1_0_1/MacOS/version.7fe1ca32.dat', length is '11641', hash code is '2145503794[0x7FE1CA32]', zip length is '4100', zip hash code is '111937434[0x06AC079A]'.
           

Build AssetBundle

待續。

Documentation

待續。

API Reference

待續。

使用UnityGameFramework編輯器出現的常見問題記錄

1. AssetBundleEditor界面中的Asset清單是空的?

待續。