GameFramework框架源码解读(一):Editor篇

博客迁移

个人博客站点,欢迎访问,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

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

配置格式介绍


<UnityGameFramework><AssetBundleEditor><Settings><SourceAssetRootPath>Assets/GameMainSourceAssetRootPath><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:ScriptableObjectSourceAssetUnionTypeFilter><SourceAssetUnionLabelFilter>l:AssetBundleInclusiveSourceAssetUnionLabelFilter><SourceAssetExceptTypeFilter>t:ScriptSourceAssetExceptTypeFilter><SourceAssetExceptLabelFilter>l:AssetBundleExclusiveSourceAssetExceptLabelFilter><AssetSorter>PathAssetSorter>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的加载方式,对应下面的枚举

    /// /// 资源加载方式类型。/// public enum AssetBundleLoadType{/// /// 从文件加载。/// LoadFromFile = 0,/// /// 从内存加载。/// LoadFromMemory,/// /// 从内存快速解密加载。/// LoadFromMemoryAndQuickDecrypt,/// /// 从内存解密加载。/// LoadFromMemoryAndDecrypt,}

(3)Variant:变体
(4)ResourceGroups:资源组

Asset:
(1)Guid:资源的guid
(2)AssetBundleName:配置中上面所记录的AssetBundleName
(3)AssetBundleVariant:变体

打开AssetBundleEditor窗口后,解析该配置,形成窗口左侧的树状图。

AssetBundleBuilder.xml

该配置用来存储AB打包配置。界面如下图:


<UnityGameFramework><AssetBundleBuilder><Settings><InternalResourceVersion>0InternalResourceVersion><Platforms>1Platforms><ZipSelected>TrueZipSelected><UncompressedAssetBundleSelected>FalseUncompressedAssetBundleSelected><DisableWriteTypeTreeSelected>FalseDisableWriteTypeTreeSelected><DeterministicAssetBundleSelected>TrueDeterministicAssetBundleSelected><ForceRebuildAssetBundleSelected>FalseForceRebuildAssetBundleSelected><IgnoreTypeTreeChangesSelected>FalseIgnoreTypeTreeChangesSelected><AppendHashToAssetBundleNameSelected>FalseAppendHashToAssetBundleNameSelected><ChunkBasedCompressionSelected>TrueChunkBasedCompressionSelected><OutputPackageSelected>TrueOutputPackageSelected><OutputFullSelected>FalseOutputFullSelected><OutputPackedSelected>FalseOutputPackedSelected><BuildEventHandlerTypeName>StarForce.Editor.StarForceBuildEventHandlerBuildEventHandlerTypeName><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的结点层级路径表示打包后的资源相对路径

    (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

界面分为以下几个部分:

Environment Information:工程的基本信息
(1)ProductName:PlayerSettings.productName
(2)Company Name:PlayerSettings.companyName
(3)Game Identifier:

public string GameIdentifier
{get{
#if UNITY_5_6_OR_NEWERreturn PlayerSettings.applicationIdentifier;
#elsereturn 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列表是空的?

待续。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部