数据库缓存 服务器缓存
In Unity 3.5 we shipped the first version of the Cache Server, and with 4.0 we’re making it even better (the way it works is the same). I want to go a bit in-depth to explain what it actually does, because it’s pretty awesome and can save you lot of time.
在Unity 3.5中,我们发布了Cache Server的第一个版本,而在4.0中,我们使其变得更好(它的工作方式是相同的)。 我想深入解释一下它的实际作用,因为它很棒,可以节省很多时间。
It makes importing projects insanely fast.
它使导入项目变得异常Swift。
If you pull changes from version control and everyone in your team uses the cache server, everything will already be cached and your imports will be almost instantaneous.
如果您从版本控制中撤消更改,并且团队中的每个人都使用缓存服务器,那么所有内容都将被缓存,并且您的导入几乎是瞬时的。
If you switch between platforms, you no longer need to wait for a long time while Unity re-compresses textures. Switching platforms becomes almost instant.
如果您在平台之间切换,则在Unity重新压缩纹理时不再需要等待很长时间。 切换平台几乎是即时的。
It works with any version control system: SVN, Perforce, Asset Server, Git, Hg etc.; or if you don’t use version control. The only requirement is that you enable .meta files in the project.
它适用于任何版本控制系统:SVN,Perforce,Asset Server,Git,Hg等; 或者如果您不使用版本控制。 唯一的要求是您在项目中启用.meta文件。
The Cache Server helps you make this go away:
高速缓存服务器可帮助您消除这种情况:
Unity中的版本控制 (Version control in Unity)
In Unity all source assets are stored in the “Assets” and “ProjectSettings” folders. Since these two folders contains all source assets, they must be in version control and they are the only folders that need to be in version control. The “Library” folder should not be under version control. In order to use Version Control in Unity you also need to enable .meta files. This can be done in the Edit -> Project Settings -> Editor menu.
在Unity中,所有源资产都存储在“资产”和“ ProjectSettings”文件夹中。 由于这两个文件夹包含所有源资产,因此它们必须处于版本控制中,并且它们是唯一需要处于版本控制中的文件夹。 “库”文件夹不应受版本控制。 为了在Unity中使用版本控制,您还需要启用.meta文件。 这可以在“ 编辑”->“项目设置”->“编辑器”菜单中完成。
The Cache Server is not a replacement for version control. In fact it does not touch source assets at all! It is a way of caching the import step. Thus it works with any version control system out there, simply because it does not interfere with it in any way.
缓存服务器不能替代版本控制 。 实际上,它根本不涉及源资产! 这是一种缓存导入步骤的方法 。 因此,它可以与任何版本控制系统一起使用,这仅仅是因为它不会以任何方式对其进行干扰。
导入步骤有什么作用? (What does the import step do?)
Unity has a completely automatic asset pipeline. Whenever a source asset like a .psd or .fbx file is modified, Unity automatically reimports it. During the import step Unity creates a file with the imported data. Various things are generated in this import step, for example a .psd file will import the texture and compress it for the target platform. A Texture2D class object will be generated and stored in the “Library/metadata” folder. Unity also generates a preview for the texture which is used when picking assets. The generated data is not under version control, since it can always be regenerated. Some asset types will reimport when switching platforms, for example textures require a reimport when switching to a platform with different compression scheme. Meshes on the other hand are independent of the target platform.
Unity具有全自动资产管道。 每当修改.psd或.fbx文件之类的源资产时,Unity都会自动重新导入它。 在导入步骤中,Unity将使用导入的数据创建一个文件。 在此导入步骤中会生成各种内容,例如,.psd文件将导入纹理并将其压缩以用于目标平台。 将生成Texture2D类对象,并将其存储在“ Library / metadata”文件夹中。 Unity还会为拾取资产时使用的纹理生成预览。 生成的数据不受版本控制,因为它始终可以重新生成。 切换平台时,某些资产类型将重新导入,例如,切换到具有不同压缩方案的平台时,纹理需要重新导入。 另一方面,网格与目标平台无关。
The import step sometimes does very complex operations. For example when importing a .psd file for iOS we generate PVR compressed texture automatically. Or we could generate secondary mesh UV data for lightmapping. In some cases it can take seconds for each asset. This time can add up and thus the cache server can give you massive import speed benefits.
导入步骤有时会执行非常复杂的操作。 例如,当为iOS导入.psd文件时,我们会自动生成PVR压缩纹理。 或者我们可以生成辅助网格UV数据以进行光照贴图。 在某些情况下,每项资产可能需要花费几秒钟的时间。 这段时间加起来,因此缓存服务器可以为您带来巨大的导入速度优势。
In Unity each asset is identified by a GUID (a globally unique identifier). The GUID is generated when an asset is first imported and stored in the .meta file. We do this so we can move any asset without losing any references to it (all asset references are GUID-based, not folder or file name based).
在Unity中,每个资产都由GUID(全局唯一标识符)标识。 首次导入资产并将其存储在.meta文件中时,将生成GUID。 我们这样做是为了在不丢失任何资产引用的情况下移动任何资产(所有资产引用均基于GUID,而不是基于文件夹或文件名)。
高速缓存服务器做什么? (What does the Cache Server do?)
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!
Unity不会实际导入资产,而是会重用导入的资产,而另一个团队成员或您自己以前已经导入并上传到缓存服务器。 缓存服务器是完全自动的,并且是导入管道的一部分。 您从缓存服务器获得多少收益取决于资产的类型。 纹理和大型模型文件通常花费最长时间导入,因此在那里的速度将是最大的。 2x-100x的任何价格都可以期待!
The first thing Unity does when it is about to import an asset is to generate an MD5 hash of all source data.
Unity即将导入资产时要做的第一件事是生成所有源数据的MD5哈希。
For a texture this consists of:
对于纹理,它包括:
- The source asset: “myTexture.psd” file. 源资产:“ myTexture.psd”文件。
- The meta file: “myTexture.psd.meta” (stores all importer settings). 元文件:“ myTexture.psd.meta”(存储所有导入器设置)。
- 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.
任何会影响所得导入数据的内容都将被散列。 缓存服务器是一个非常简单的文件服务器,它通过该资产的GUID和哈希值查找资产。 在缓存服务器上,您可能有许多相同纹理的缓存文件。 高速缓存服务器会自动删除一段时间内未使用的资产。 它使用一种最近最少使用的简单方案来丢弃未使用的缓存。
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.
例如,如果您将Unity从iOS切换到独立模式,则导入的纹理将获得不同的哈希值。 切换平台时,Unity计算所有纹理的新哈希,然后尝试从缓存服务器下载它们。 如果可用,将使用它们,否则它将导入纹理并将导入的结果上传到缓存服务器。
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 performance)
The Cache Server itself is very simple. It is essentially a streaming file server. If you setup a cache server for a big team, the important considerations are:
缓存服务器本身非常简单。 它本质上是一个流文件服务器。 如果为大型团队设置缓存服务器,则重要的注意事项是:
- Lots of RAM so the cache server can keep all assets in disk cache and serve them instantly 大量RAM,因此缓存服务器可以将所有资产保留在磁盘缓存中并立即提供服务
- Fast disk so if it’s not in RAM cache it can be served quickly 快速磁盘,因此如果不在RAM缓存中,则可以快速提供服务
- Ensure the cache server is at least 3-4x larger than the Library/metadata folder in your project 确保缓存服务器至少比项目中的Library / metadata文件夹大3-4倍
- The network connection to the machines using Unity must be fast. 使用Unity到计算机的网络连接必须快速。
Since it is purely a cache that accelerates the import and all the data can always be re-generated automatically by reimporting, it usually makes sense to ensure the cache server is local. Some people setup cache servers that are used remotely, this can make sense with a very fast internet connection. It is best to profile it and see what is fastest. But for the best performance I’d recommend to have the cache server in the same location with a wired Ethernet connection. There is no problem with having multiple cache servers for one project. It is simply a cache, there are no conflicts if you have multiple servers.
由于它纯粹是可以加速导入的缓存,并且始终可以通过重新导入自动重新生成所有数据,因此确保缓存服务器在本地通常很有意义。 某些人设置了远程使用的缓存服务器,这可以通过非常快速的Internet连接来实现。 最好对其进行概要分析,并查看最快的方法。 但是为了获得最佳性能,我建议将高速缓存服务器放置在具有有线以太网连接的相同位置。 一个项目具有多个缓存服务器没有问题。 它只是一个缓存,如果有多个服务器,则不会发生冲突。
Once the data is downloaded, the actual integration of the data usually takes less than 1 ms.
数据下载后,数据的实际集成时间通常少于1毫秒。
Essentially it caches the entire import step including preview generation, meaning other people using the same cache server can reuse the already imported asset without having to go through the full import process. In Unity the asset importers often perform complex tasks for example, when importing a .psd file in iOS we generate compressed PVRTC textures automatically for you so you get the best performance on the device. Or we have an option to generate secondary UV data for your models for lightmapping purposes automatically. These tasks can easily take several seconds and when you have a ton of assets it can quickly add up.
从本质上讲,它缓存了整个导入步骤,包括预览生成,这意味着使用同一缓存服务器的其他人可以重复使用已导入的资产,而无需执行完整的导入过程。 在Unity中,资产导入程序通常执行复杂的任务,例如,在iOS中导入.psd文件时,我们会自动为您生成压缩的PVRTC纹理,以便您在设备上获得最佳性能。 或者,我们可以选择为模型自动生成辅助UV数据以进行光照贴图。 这些任务可能很容易花费几秒钟,并且当您拥有大量资产时,它很快就会累加起来。
Windows上的缓存服务器 (Cache Server on Windows)
The Cache Server is built on top of node.js. The windows file system & file locking behaviour on windows make the cache server work not very efficiently on windows. I suggest that you install your cache server on a Linux or OS X server instead.
缓存服务器构建在node.js之上。 Windows上的Windows文件系统和文件锁定行为使缓存服务器在Windows上的工作效率不是很高。 我建议您将缓存服务器安装在Linux或OS X服务器上。
在缓存服务器中不该做什么? (What not to do in the 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.
缓存服务器不处理资产依赖关系。 实际上,Unity的资产管道也不处理资产依赖项的概念。 它以一种避免依赖的方式构建。 AssetPostprocessors是一种常用的技术,用于自定义资产导入器以满足您的需求。 例如,您可能想根据其名称或标签将MeshColliders添加到FBX文件中的某些GameObjects。
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.
但是,使用AssetPostprocessors引入依赖关系也很容易。 例如,您可以使用资产旁边的文本文件中的数据将其他组件添加到导入的游戏对象中。 缓存服务器不支持此功能! 如果要使用缓存服务器,则必须删除对项目文件夹中其他资产的依赖。 由于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:
实际上,有很多方法可以进行与Cache Server一起使用的资产后处理。 您可以使用:
- The Path of the imported asset 导入资产的路径
- Any import settings of the asset (including string AssetImporter.userData) 资产的任何导入设置(包括字符串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.
修改已经存在的材料也可能导致麻烦。 使用缓存服务器时,Unity会验证是否保留了对材料的引用。 但是,由于不会调用任何后处理调用,因此通过高速缓存服务器导入模型时,无法更改材料的内容。 因此,在有或没有缓存服务器的情况下导入时,您可能会得到不同的结果。 最好不要修改资产后处理器在磁盘上已经存在的材料。
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.
修改已经存在的材料的问题在于,如果您通过缓存服务器下载fbx文件,则没有为其运行的导入过程,这意味着如果您依赖每次将生成的材料重置为某些生成的默认值如果模型导入程序运行,则在导入缓存的fbx文件时将不运行此资产后处理器。
我在哪里得到的? (Where do I get it?)
The Cache Server is part of the Team License: http://unity3d.com/unity/team/
缓存服务器是团队许可的一部分: http : //unity3d.com/unity/team/
You can download it here: http://unity3d.com/unity/team/cacheserver/
您可以在这里下载: http : //unity3d.com/unity/team/cacheserver/
How to setup the Cache Server: http://docs.unity3d.com/Documentation/Manual/AssetCacheServer.html
如何设置缓存服务器: http : //docs.unity3d.com/Documentation/Manual/AssetCacheServer.html
结论 (Conclusion)
The Cache Server gives you massive import time speed ups. If you work on a large project folder, using it is incredibly important. It makes your life better, seriously. Use it!
高速缓存服务器为您提供了巨大的导入时间加速。 如果您使用大型项目文件夹,则使用它非常重要。 认真地使您的生活变得更好。 用它!
翻译自: https://blogs.unity3d.com/2012/10/26/in-depth-cache-server/
数据库缓存 服务器缓存