Unity Cache Server了解和常见问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingangxin666/article/details/80870052

Cache Server做什么

The cache server accelerates the import step. It does not touch source data. The cache server is independent of version control. It is even independent of your current project.

Instead of actually importing an asset Unity will re-use an imported asset another team member or yourself has previously imported and uploaded to the cache server. The Cache Server is completely automatic and happens as part of the import pipeline. How much benefit you get from the cache server depends on the type of assets. Textures and large model files usually take the most time to import thus the speed up will be greatest there. Anything from 2x-100x can be expected!

The first thing Unity does when it is about to import an asset is to generate an MD5 hash of all source data.

For a texture this consists of:

  • The source asset: “myTexture.psd” file.
  • The meta file: “myTexture.psd.meta” (stores all importer settings).
  • The internal version number of the texture importer.
  • A hash of all version numbers of all Asset Postprocessors.

Anything that can affect the resulting imported data will be hashed. The Cache Server is a very simple file server that looks up assets by the GUID & Hash of that asset. On the cache server you might have many cached files for the same texture. The cache server removes assets that have not been used for a period of time automatically. It uses a simple least recently used scheme for throwing away unused caches.

If you switch Unity from iOS to Standalone mode for example, the imported texture will get a different hash. When switching the platform Unity calculates the new hash of all textures and tries to download them from the cache server. If they are available they will be used, otherwise it will import the texture and upload the imported result to the cache server.

Once it is cached on the server it can be used next time someone switches to the platform or imports that asset. Thus switching platforms becomes pretty much instant.

重点提炼:

  • cache server 通过资源的GUID&Hash来查找资源
  • 相同纹理的可能Hash值不同, 所以在缓存服务器上,可能有许多相同纹理的缓存文件
  • 在切换平台时,Unity会计算所有资源的新Hash,并尝试从缓存服务器下载它们。如果它们可用,则会使用它们,否则它将重新导入资源并将导入的结果上载到缓存服务器。

Cache Server上不能做什么?

The Cache Server does not handle asset dependencies. In fact, Unity’s asset pipeline does not deal with the concept of asset dependencies either; it is built in a way to avoid dependencies. AssetPostprocessors are a common technique used to customize the asset importer to fit your needs; for example you might want to add MeshColliders to some GameObjects in an FBX file based on their name or tag.

However, it is also easy to use AssetPostprocessors to introduce dependencies. For example you might use data from a text file next to the asset to add additional components to the imported game objects. This is not supported in the Cache Server! If you want to use the cache server you you will have to remove dependency on other assets in the project folder. Since the Cache Server doesn’t know anything about the dependency in your postprocessor, it will not know that anything has changed thus use an old cached version of the asset.

In practice there are plenty of ways how you can do asset postprocessing that works well with the Cache Server.
You can use:

  • The Path of the imported asset
  • Any import settings of the asset (including string AssetImporter.userData)
  • The source asset itself or any data generated from it passed to you in the asset postprocessor.

Modifying materials that already exist might cause trouble as well. When using the cache server Unity validates that the references to materials are maintained. But since no postprocessing calls will be invoked, the contents of the material can not be changed when a model is imported through the cache server. Thus you might get different results when importing with or without cache server. It is best to never modify materials that already exist on disk from an asset postprocessor.

The issue with modifying materials that already exist is that, if you download an fbx file through the cache server, then there is no import process running for it, this means that if you rely on resetting the generated materials to some generated defaults every single time the model importer runs, then this asset postprocessor will not be run, when importing a cached fbx file.

重点提炼:

  • Cache Server不处理资源的依赖关系
  • 使用Cache Server来导入资源时, 将不会使用AssetPostprocessors方法来进行资源后处理
  • 使用Cache Server来下载fbx, 则不会运行import process, 那么OnPostprocessModel方法不会被触发, 也就是无法对fbx进行处理

FAQ

什么样的改变将导致Cache Server的更新?

  • 当Unity导入一个资源,所有元数据会对应生成一个MD5 hash文件
  • 如果得到的hash与存储在缓存服务器上的hash不同,资源将重新被导入并上传到缓存服务器

Are there any issues when working with materials? 一些有关材质工作的问题

Modifying materials that already exist might cause trouble. When using the Cache Server, Unity validates that the references to materials are maintained. But since no postprocessing calls will be invoked, the contents of the material can not be changed when a model is imported through the Cache Server. Thus you might get different results when importing with or without Cache Server. It is best to never modify materials that already exist on disk.

修改已存在的材质可能将导致问题。当使用缓存服务器,Unity验证那些维护材质的参照。但是因为没有后台程序被唤醒,材质内容不能被改变当模型导入通过缓存服务。因此你可能会得到不同结果当导入或没有缓存服务。最好是不要修改已经保存在硬盘上的材质。

资源后处理和Cache Server如何配合使用?

In practice there are plenty of ways how you can do asset postprocessing that works well with the Cache Server.

You can use:

  • The Path of the imported asset
  • Any import settings of the asset (including string AssetImporter.userData)
  • The source asset itself or any data generated from it passed to you in the asset postprocessor.7u

修改资源的原始数据(影响Hash计算的元素), 触发Cache Server上资源缓存更新

  • Unity官方提出建议使用以下数据(修改???), 可以触发Cache Server更新

    • 导入的资源的路径
    • 资源的导入设置(包括AssetImporter.userData)
    • 资源的源数据本身或在资源后处理方法中产生的新的资源源数据
  • 实践

    • 移动fbx的路径, 再调用AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.Default), 是可以调用到OnPostprocessModel方法的
    • 修改.meta文件中的userData值, 可以触发OnPostprocessModel方法

Cache Server里面存储的是什么数据呢?

还记得一个Unity项目下由Library文件夹吗, 通过查看本地Cache Server, 里面的数据其实跟每个项目的Library\metadata类似, 只不过存储的是所有Unity项目的资源处理元数据.

参加阅读:

猜你喜欢

转载自blog.csdn.net/jingangxin666/article/details/80870052