UGF BaseComponent默认勾选了Editor Resource Mode, 即在编辑器模式下是直接加载Assets目录下的资源。在非编辑器模式下框架是从AssetBundle中加载资源,所以使用框架加载的资源都需要打包成AssetBundle。
使用UGF框架加载的AssetBundle必须是用AssetBundle Tools生成的AssetBundle,因为AssetBundle Tools打包会生成一些数据供框架使用
一、AssetBundle Editor
1.配置AssetBundle Editor
初次使用AssetBundle Editor需要用户在配置文件添加相关数据
打开AssetBundle Editor窗口点击Save按钮框架会自动生成GameFramework/Configs/AssetBundleEditor.xml配置文件
Assets Path SourceAssetRootPath:搜索资源的根目录,AssetBundle Editor会从此目录开始搜索符合条件的资源并显示在资源列表中 SourceAssetSearchPaths:配置资源搜索的子目录(相对于SourceAssetRootPath的子目录),如果配置了此目录,AssetBundle Editor只会从SourceAssetRootPath目录下搜索当前所有配置过的子目录。可以配置多个子目录。如果不配置子目录,默认搜索SourceAssetRootPath下的所有目录 SourceAssetUnionTypeFilter:配置要找出哪些类型的资源 场景(t:Scene) 预制体(t:Prefab) 着色器(t:Shader) 材质(t:Material) 贴图(t:Texture) 声音(t:AudioClip) 动画(t:AnimationClip t:AnimatorController) 字体(t:Font) 文本(t:TextAsset t:ScriptableObject)SourceAssetUnionLabelFilter:要包含的标签类型,默认包含AssetBundleInclusive 标签
SourceAssetExceptTypeFilter:要排除的资源类型,默认会排除脚本(t:Script)资源。
SourceAssetExceptLabelFilter:要排除的标签类型,默认会排除带有 AssetBundleExclusive 标签的资源。
AssetSorter:AssetBundle Editor中资源列表显示排序方式。Name(按名字排序) 、Path(按路径排序)、Guid(按资源GUID排序)
举个栗子:
2.编辑要打包的资源
AssetBundle Editor界面有三个区域:
AssetBundle List:显示已经添加的AB包
AssetBundle Content:显示选中的AB包中所包含的资源
Asset List:显示按照AssetBundleEditor.xml配置筛选出的资源
举个栗子:将所有场景打包到名为Scene的AB包里
①点击Add添加一个名为Scene的AB包
②在AssetList中选中所有场景文件,点击‘<<’按钮将选中资源加入AB包中
③Save保存
二、AssetBundle Builder
使用AssetBundle Editor设置好需要打包的资源后,需要使用AssetBundle Builder进行资源打包。AssetBundle Builder是不能直接使用的,它需要设置一个Build Event Handler。
1.配置AssetBundle Builder
①创建Editor文件夹(AssetBundle Builder会搜索Editor文件夹,并找到该文件夹下的IBuildEventHandler),在Editor下新建脚本并实现UnityGameFramework.Editor.AssetBundleTools.IBuildEventHandler接口:
using UnityGameFramework.Editor.AssetBundleTools;
using UnityEditor;
namespace ShadowTank
{
public class AssetBuildHelper : IBuildEventHandler
{
public void PostProcessBuild(BuildTarget buildTarget, string workingPath, string outputPackagePath, string outputFullPath, string outputPackedPath)
{
//throw new System.NotImplementedException();
}
public void PostProcessBuildAll(string productName, string companyName, string gameIdentifier, string applicableGameVersion, int internalResourceVersion, string unityVersion, BuildAssetBundleOptions buildOptions, bool zip, string outputDirectory, string workingPath, string outputPackagePath, string outputFullPath, string outputPackedPath, string buildReportPath)
{
//throw new System.NotImplementedException();
}
public void PreProcessBuild(BuildTarget buildTarget, string workingPath, string outputPackagePath, string outputFullPath, string outputPackedPath)
{
//throw new System.NotImplementedException();
}
public void PreProcessBuildAll(string productName, string companyName, string gameIdentifier, string applicableGameVersion, int internalResourceVersion, string unityVersion, BuildAssetBundleOptions buildOptions, bool zip, string outputDirectory, string workingPath, string outputPackagePath, string outputFullPath, string outputPackedPath, string buildReportPath)
{
//throw new System.NotImplementedException();
}
}
}
②打开AssetBundle Builder设置Build Event Handler和Output Directory
③点击Start Build AssetBundles开始打包
Output Directory:文件输出目录。
Working Path:Unity 生成 AssetBundle 时的工作目录。
Output Package Path:为单机模式生成的文件的所在目录,若游戏是单机游戏,将此目录中生成的对应平台的文件复制到StreamingAssets中。
Output FullPath:为可更新模式生成的完整文件包的所在目录,若游戏是网络游戏,生成结束后应将此目录上传至资源服务器,供玩家下载用。
Output Packed Path:为可更新模式生成的文件的所在目录,若游戏是网络游戏,生成结束后将此目录中对应平台的文件拷贝至 StreamingAssets 后构建 App 即可。一个 AssetBundle 是否会生成到 Output Packed Path,取决与这个 AssetBundle 是否在 AssetBundle 编辑工具中被标记为 Packed。
作者:城府依旧
来源:CSDN
原文:https://blog.csdn.net/final5788/article/details/79237568
版权声明:本文为博主原创文章,转载请附上博文链接!