unity3d:YooAsset零冗余构建Assetbundle代码分析

BuildAssetInfo构建asset信息

1.每个收集器下asset会构建出BuildAssetInfo,这种asset是没有冗余,只有依赖列表
2.每个依赖asset会构建出BuildAssetInfo,会记录将要打入的bundle列表

依赖的Asset列表

这个asset依赖的其他asset列表,只对收集器资源有效

		/// <summary>
		/// 依赖的所有资源
		/// 注意:包括零依赖资源和冗余资源(资源包名无效)
		/// </summary>
		public List<BuildAssetInfo> AllDependAssetInfos { private set; get; }

列表信息从何来?
在创建收集器资源调用依赖

YooAsset.Editor.AssetBundleCollector.GetAllDependencies

private List<string> GetAllDependencies(string mainAssetPath)
{
	string[] depends = AssetDatabase.GetDependencies(mainAssetPath, true);
	List<string> result = new List<string>(depends.Length);
	foreach (string assetPath in depends)
	{
		if (IsValidateAsset(assetPath, false))
		{
			// 注意:排除主资源对象
			if (assetPath != mainAssetPath)
				result.Add(assetPath);
		}
	}
	return result;
}

预计会被打入的Bundle列表

HashSet< string> _referenceBundleNames
在处理数据时,如果一个依赖asset会被打入多个bundle,在这里记录,则视为冗余asset
这个bundle列表如何得到?
在遍历收集器资源依赖资源时,依赖资源add主资源的Bundle名
在最后处理冗余时,如果列表数量>2,则依赖asset生成独立bundle

BuildBundleInfo构建bundle信息

一个bundle会包含多个asset

主asset列表

/// <summary>
		/// 参与构建的资源列表
		/// 注意:不包含零依赖资源和冗余资源
		/// </summary>
		public readonly List<BuildAssetInfo> AllMainAssets = new List<BuildAssetInfo>();

1.普通bundle包,里面只有收集器asset,不会包含依赖asset。在构建时unity自动收集依赖进入bundle
2.共享bundle包,里面只有依赖asset

依赖asset列表

每个BuildAssetInfo又会包含List< BuildAssetInfo> AllDependAssetInfos,依赖资源列表

CollectAssetInfo收集器的asset

CollectAssetInfo是记录asset需要打入的bundle,和这个asset依赖的asset列表

asset的依赖列表

/// <summary>
		/// 依赖的资源列表
		/// </summary>
		public List<string> DependAssets = new List<string>();

如何得到
在创建CreateCollectAssetInfo时
YooAsset.Editor.AssetBundleCollector.CreateCollectAssetInfo
YooAsset.Editor.AssetBundleCollector.GetAllDependencies
调用AssetDatabase.GetDependencies,得到依赖列表

收集器Package,Group关系

package 的收集器资源
YooAsset.Editor.AssetBundleCollectorPackage.GetAllCollectAssets
收集器组
YooAsset.Editor.AssetBundleCollectorGroup.GetAllCollectAssets
一个收集器
YooAsset.Editor.AssetBundleCollector.GetAllCollectAssets
一个收集器按照规则又会包N个Asset

获取打包收集的资源文件

YooAsset.Editor.AssetBundleCollector.GetAllCollectAssets
1.如果收集器是文件夹路径,遍历下面所有资源类型,每个单独生成CollectAssetInfo加入字典
2.如果收集器是资源路径,只把该路径生成CollectAssetInfo加入字典
3.可寻址冲突,分两种情况,同个收集器与不同收集器;是对于同一个收集器而言,asset名字冲突,例如同个收集器下多个文件夹存在同名asset。例如
在这里插入图片描述

同个收集器下,两个文件夹下都存在cube1.prefab,报错

System.Exception: The address is existed : Cube1 in collector : Assets/YooAsset/Samples/Space Shooter/GameRes/Effect 
AssetPath:
     Assets/YooAsset/Samples/Space Shooter/GameRes/Effect/Engines/Cube1.prefab
     Assets/YooAsset/Samples/Space Shooter/GameRes/Effect/Explosions/Cube1.prefab

不同收集器在最后打包也会判断可寻址是否重复

System.Exception: The address is existed : Cube1 in group : battle 
AssetPath:
     Assets/YooAsset/Samples/Space Shooter/GameRes/Effect/Engines/Cube1.prefab
     Assets/YooAsset/Samples/Space Shooter/GameRes/Entity/Cube1.prefab

两个收集器里都有Cube1
在这里插入图片描述

资源构建上下文BuildMapContext

决定哪些是asset对应的bundle
YooAsset.Editor.TaskGetBuildMap.CreateBuildMap

BuildBundleInfo字典

key为bundle的名字,路径/换成_,从assets开始
Dictionary<string, BuildBundleInfo> _bundleInfoDic
在这里插入图片描述

所有asset构建BuildAssetInfo字典

Dictionary<string, BuildAssetInfo> allBuildAssetInfoDic
key为资源路径,BuildAssetInfo为打包路径

包含收集器的资源,收集器资源的依赖列表

录入所有收集器收集的资源

遍历allCollectAssetInfos

// 4. 录入所有收集器收集的资源
			foreach (var collectAssetInfo in allCollectAssetInfos)
			{					allBuildAssetInfoDic.Add(collectAssetInfo.AssetPath, buildAssetInfo);
				}

收集器下的asset 是不会存在打入多个bundle情况

录入所有收集资源的依赖资源

只对依赖资源有效
1.遍历每一个allCollectAssetInfos.DependAssets
2.如果依赖asset未进入allBuildAssetInfoDic,则创建一个新BuildAssetInfo,它的_referenceBundleNames增加一个为收集器资源信息CollectAssetInfo的bundle,即添加关联的资源包名称
3.如果依赖asset已经存在于allBuildAssetInfoDic,说明已经被一个bundleA依赖,再添加bundleB的信息,标记为冗余资源。HashSet< string> _referenceBundleNames,这是个哈希表,如果bundleA = bundleB,不会加入

// 5. 录入所有收集资源的依赖资源
foreach (var collectAssetInfo in allCollectAssetInfos)
{
	string collectAssetBundleName = collectAssetInfo.BundleName;
	foreach (var dependAssetPath in collectAssetInfo.DependAssets)
	{
		if (allBuildAssetInfoDic.ContainsKey(dependAssetPath))
		{
			allBuildAssetInfoDic[dependAssetPath].AddBundleTags(collectAssetInfo.AssetTags);
			allBuildAssetInfoDic[dependAssetPath].AddReferenceBundleName(collectAssetBundleName);
		}
		else
		{
			var buildAssetInfo = new BuildAssetInfo(dependAssetPath);
			buildAssetInfo.AddBundleTags(collectAssetInfo.AssetTags);
			buildAssetInfo.AddReferenceBundleName(collectAssetBundleName);
			allBuildAssetInfoDic.Add(dependAssetPath, buildAssetInfo);
		}
	}
}

填充所有收集资源的依赖列表

收集资源的依赖List< BuildAssetInfo>信息,只对收集资源有效,即这个收集资源的BuildAssetInfo的依赖List< BuildAssetInfo>

计算共享冗余的bundle

计算共享包名

YooAsset.Editor.BuildAssetInfo.CalculateShareBundleName
1.如果这个资源没有收集器类型CollectorType = ECollectorType.None,说明是被依赖的,非主动收集打包的
2.如果引用次数>1,说明至少被两个bundle引用,需要设置独立bundle,为了解决独立bundle过多的问题,按照资源的目录名字作为bundle名。如果同目录下还有零散资源被重复引用,都会打入这个包中。因为是按照零冗余ZeroRedundancySharedPackRule规则构建,生成的bundle名此asset的直接文件夹路径
3.如果引用 <= 1,只被一个bundle引用,不需要打独立bundle,把他bundle置为空

public void CalculateShareBundleName(ISharedPackRule sharedPackRule, bool uniqueBundleName, string packageName, string shadersBundleName)
		{
				if (_referenceBundleNames.Count > 1)
				{
					PackRuleResult packRuleResult = sharedPackRule.GetPackRuleResult(AssetPath);
					BundleName = packRuleResult.GetShareBundleName(packageName, uniqueBundleName);
				}
				else
				{
					// 注意:被引用次数小于1的资源不需要设置资源包名称
					BundleName = string.Empty;

AB依赖C,AB在同个Bundle

例如:Cube1,Cube2在同一bundleA里,它们都依赖MatCube.mat,那么MatCube.mat会被打入bundleA
在这里插入图片描述
在这里插入图片描述

AB依赖C,AB在不同Bundle

在这里插入图片描述

这种情况下C会打入共享包
在这里插入图片描述

bundle内容为空,因为被依赖asset不会通过代码加载

记录冗余资源

计算共享报名,已经去掉了冗余资源:把冗余asset的bundle变为文件夹路径名
如果asset有bundle名,一定不是冗余
对于没有bundle名的看,是否被2个引用

移除不参与构建的资源

如果一个asset的BuildAssetInfo.bundleName为空,说明它是被收集资源依赖的,且只被一个依赖,不需要生成一个独立bundle。这个asset会被打入依赖的bundle

构建资源列表

最后相当于遍历BuildAssetInfo字典,生成BuildBundleInfo字典
BuildAssetInfo字典 是N个重复的asset 对应bundle
BuildBundleInfo,是一个bundle里会有多少asset
最后按照bundle名生成BuildBundleInfo字典

构建

YooAsset.Editor.AssetBundleBuilder.Run
构建顺序,按照order来
在这里插入图片描述

两个比较重要的,TaskGetBuildMap 构建出零冗余的bundle关系:YooAsset.Editor.TaskGetBuildMap.CreateBuildMap

TaskBuilding:BuiltinBuildPipeline下出包

YooAsset.Editor.BuildMapContext.GetPipelineBuilds
根据BuildBundleInfo字典生成Ab信息
bundle里只插入主资源,依赖asset会 被收集插入bundle

		/// <summary>
		/// 获取构建管线里需要的数据
		/// </summary>
		public UnityEditor.AssetBundleBuild[] GetPipelineBuilds()
		{
			List<UnityEditor.AssetBundleBuild> builds = new List<UnityEditor.AssetBundleBuild>(_bundleInfoDic.Count);
			foreach (var bundleInfo in _bundleInfoDic.Values)
			{
				if (bundleInfo.IsRawFile == false)
					builds.Add(bundleInfo.CreatePipelineBuild());
			}
			return builds.ToArray();
		}

AssetBundleManifest buildResults = BuildPipeline.BuildAssetBundles(pipelineOutputDirectory, buildMapContext.GetPipelineBuilds(), buildOptions, buildParametersContext.Parameters.BuildTarget);

冗余MatCube.mat如何打包示例

asset:Assets/YooAsset/Samples/Space Shooter/GameRes/Material/MatCube.mat分配bundle:assets_yooasset_samples_space shooter_gameres_effect.bundle

asset:Assets/YooAsset/Samples/Space Shooter/GameRes/Material/MatCube.mat分配bundle:assets_yooasset_samples_space shooter_gameres_entity.bundle

发现bundle列表>1,重新分配bundle名
冗余:Assets/YooAsset/Samples/Space Shooter/GameRes/Material/MatCube.mat,分配bundleshare_assets_yooasset_samples_space shooter_gameres_material.bundle

生成BuildBundleInfo
bundleshare_assets_yooasset_samples_space shooter_gameres_material.bundle:塞入asset:Assets/YooAsset/Samples/Space Shooter/GameRes/Material/MatCube.mat

生成AB;bundle:share_assets_yooasset_samples_space shooter_gameres_material.bundle,assetNames;[“Assets/YooAsset/Samples/Space Shooter/GameRes/Material/MatCube.mat”]

报告bundleshare_assets_yooasset_samples_space shooter_gameres_material.bundle:里assets:[“Assets/YooAsset/Samples/Space Shooter/GameRes/Material/MatCube.mat”]

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Unity中,AssetBundle是用于打和加载资源的一种方式。你可以使用AssetBundle来控制资源的依赖关系,以确保资源在加载时能够正确地解决它们之间的依赖关系。以下是一种常见的方法来控制资源的依赖关系: 1. 创建AssetBundle:首先,你需要将相关资源打成AssetBundle。你可以使用Unity的BuildPipeline.BuildAssetBundles方法来生成AssetBundle文件,并指定资源及其依赖项的路径。 2. 定义AssetBundle的依赖关系:在打AssetBundle时,你可以通过设置AssetBundleManifest来定义资源之间的依赖关系。AssetBundleManifest是一个记录了打过程中所有AssetBundle及其依赖关系的清单文件。 3. 加载主AssetBundle:在运行时,你需要加载主AssetBundle,这是整个资源加载的起点。你可以使用AssetBundle.LoadFromFile或AssetBundle.LoadFromMemoryAsync等方法来加载主AssetBundle。 4. 加载依赖项:一旦主AssetBundle加载完成,你可以通过AssetBundleManifest.GetDirectDependencies方法获取它的所有直接依赖项的名称。然后,你可以使用AssetBundle.LoadFromFile或AssetBundle.LoadFromMemoryAsync等方法来加载这些依赖项的AssetBundle文件。 5. 加载资源:当所有依赖项的AssetBundle都加载完成后,你可以使用AssetBundle.LoadAsset或者AssetBundle.LoadAssetAsync等方法来加载资源。这些资源可以是场景、模型、纹理等。 6. 卸载AssetBundle:一旦你加载了所需的资源,你可以使用AssetBundle.Unload方法来卸载不再需要的AssetBundle。但要注意,如果你还在使用该AssetBundle中的资源,那么卸载后将无法访问这些资源。 通过以上步骤,你可以控制资源的依赖关系,确保在加载AssetBundle时能够正确地解决依赖关系。这样可以提高资源管理的灵活性和效率,并减少资源冗余

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值