type
status
date
slug
summary
tags
category
icon
password

什么是 AssetDependencyHash

在 Unity 中打包 AssetBundle 的开发者经常会遇到 AssetDependencyHash。这是一个表示资产(Asset)及其所有直接和间接依赖项内容的哈希值。AssetDependencyHash 的主要用途是判断资产是否发生变化,以决定是否需要重新导入或编译。
为了提升构建 AssetBundle 的效率,我们通常只针对有变更的资产执行构建逻辑,而未变化的资源则跳过。但是,哪些因素会影响 AssetDependencyHash 的值呢?

如何获取 AssetDependencyHash

Unity 提供了 AssetDatabase.GetAssetDependencyHash 方法来获取资产的哈希值。官方文档描述如下:
Returns the hash of all the dependencies of an asset.
The hash aggregates the following: source asset path, source asset, meta file, target platform and importer version. The change of this hash indicates that the imported asset may have changed so the relevant asset bundles should be rebuilt.
根据文档,以下因素会影响一个资源的哈希值:
  1. 资源的路径
  1. 资源的内容
  1. 资源的元数据文件
  1. 目标导入平台
  1. 导入器版本
下面我们通过实验来验证这些因素。

实验对象

以场景 Desert_01.unity 及其依赖资源为例进行测试。
notion image
初始哈希值:
notion image

实验结果

路径变更

  1. 资源重命名(哈希值变更
    1. Desert_01.unity 重命名后,哈希值发生变化。
      notion image
  1. 资源命名还原(哈希值恢复
    1. 将资源重命名回 Desert_01.unity,哈希值恢复至修改前。
      notion image
  1. 资源移动到不同目录(哈希值不变
    1. 移动 Desert_01.unity 到上级目录,哈希值未变。
      notion image
  1. 资源所在目录重命名(哈希值不变
    1. 重命名 Desert_01.unity 所在目录,哈希值未变。
      notion image
  1. 直接引用资源重命名(哈希值不变
    1. 重命名直接引用的资源,如 Desert_Scene.FBX,哈希值未变。
      notion image
      notion image
  1. 直接引用资源移动目录(哈希值不变
    1. 移动直接引用的资源,哈希值未变。
      notion image
      notion image
  1. 直接引用资源丢失(哈希值不变
    1. 删除直接引用的资源,如 Desert_Scene.FBX,哈希值未变。
      notion image
      notion image
  1. 间接引用资源重命名(哈希值不变
    1. 重命名间接引用的资源,哈希值未变。
      notion image
      notion image
  1. 间接引用资源移动目录(哈希值不变
    1. 移动间接引用的资源,哈希值未变。
      notion image
      notion image

资源内容修改

  1. 资源本身修改(哈希值变更
    1. 修改并保存 Desert_01.unity 后,哈希值变更。
      notion image
      notion image
  1. 直接引用资源修改(哈希值不变
    1. 修改直接引用的资源,如 Desert_Scene.FBX,哈希值未变。
      notion image
      notion image
  1. 间接引用资源修改(哈希值不变
    1. 修改间接引用的资源,哈希值未变。
      notion image
      notion image

META 文件变更

  1. 资源对应 META 文件修改(哈希值变更
    1. 修改场景 META 文件中的 GUID,哈希值变更。
      notion image
      notion image
  1. 直接引用资源 META 文件变更(哈希值不变
    1. 调整直接引用资源的导入参数(保存在 META 文件中),哈希值未变。
      notion image
      notion image

结论

通过上述实验,我们可以得出以下结论:
  • 资源的文件名内容对应的 META 文件的变化导致 AssetDependencyHash 的变更。
  • 资源的所在目录直接依赖以及间接依赖的变化,不会导致 AssetDependencyHash 的变更。
 
漫谈C# Language Version、.Net Framework、Mono、.Net Standard以及.Net Core算法解惑:快速选择
Loading...