筆記目錄
文章目錄
前言
本文將使用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
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案例中定義了這幾個路徑
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窗口的左側列表。
其中看一下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打包配置。界面如下圖:
<?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裏包含的資源。
這三個部分下面都有對應的工具菜單。
-
AssetBundleList
(1)以AssetBundles爲根結點
(2)這些AB的結點層級路徑表示打包後的資源相對路徑
(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)不支持多選、不支持拖拽修改相對路徑。 -
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
界面分爲以下幾個部分:
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這五個子路徑。
當這些配置都配好後,就會出現提示:Ready to build.
點擊Start Build AssetBundles按鈕開始打包,點擊Save按鈕保存配置到AssetBundleBuilder.xml配置中。
打包成功:
打包完後,再來介紹這五個目錄:
(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列表是空的?
待續。