Version: 2021.3
Text assets
资源数据库

资源元数据

When Unity imports assets, it also stores and manages additional data about the asset, such as what import settings Unity should use to import the asset, and where the asset is used throughout your project. Below is a description of how this process works:

  1. Unity 为资源分配唯一 ID。
  2. Unity 创建一个伴随资源文件的 .meta 文件。
  3. Unity 对资源进行处理

The Import settings for any given asset affects how Unity processes the asset. If you modify the asset file, or any of the asset’s import settings, Unity reimports the asset. For more information, see Assets and their import settings.

唯一 ID

The Unity Editor frequently checks the contents of the Assets folder against the list of assets it already knows about. When you place an asset in the Assets folder, Unity detects that you have added a new file.

Unity 找到新文件时会向此资源分配唯一 ID。这是 Unity 在内部使用的 ID,用于引用资源,以便 Unity 可以移动或重命名此资源而不会破坏任何内容。

这些 ID 通常在编辑器中不可见,

元文件

The image below shows .meta files that Unity creates for each item in your project’s Assets folder. Unity creates meta files for asset files and folders.

These files are hidden in Unity’s Project window, and might also be hidden in your file system by default (see Wikipedia: Hidden file and hidden directory), so you might not see them in Windows Explorer or Finder unless you make your hidden files visible on your computer.

The relationship between the Assets folder in your Unity project on your computer, meta files, and the Project window in Unity
The relationship between the Assets folder in your Unity project on your computer, meta files, and the Project window in Unity

This example demonstrates that Unity creates a .meta file for each asset or folder inside the project’s Assets folder, because they appear in a system file browser. However, these .meta files are not visible in the Project window because they are hidden by default. To make them visible, open the Mode project setting and enable Visible Meta Files.

Unity 为资源创建 .meta 文件时,会将此资源的 ID 写入 .meta 文件,并将 .meta 文件与资源文件存储在同一位置。

The .meta files contain the unique ID assigned to the asset, and values for all the import settings you see in the Inspector window when you select an asset in your Project window. For example, for a Texture, this includes the Texture Type, Wrap Mode, Filter Mode and Aniso Level import settings.

If you change the import settings for an asset, Unity saves those new settings to the .meta file that accompanies the asset. Unity then re-imports the asset according to your updated settings, and updates the corresponding imported “game-ready” data in the project’s Library folder.

Meta files and asset files

Important: Meta files contain important information about how the asset is used in the Project, and they must stay with the asset file they relate to. If you move or rename an asset within Unity’s own Project window, Unity also automatically moves or renames the corresponding .meta file. However, if you move or rename an asset outside of Unity (that is, in Windows Explorer, or Finder on macOS), you must move or rename the .meta file to match.

If an asset loses its meta file (for example, if you move or rename the asset outside of Unity, but don’t move or rename the corresponding .meta file), any reference to that asset is broken in your project. In this situation, Unity notices that the asset does not have a corresponding meta file, generates a new one for the moved/renamed asset as if it is a brand new asset, and deletes the old “orphaned” .meta file.

This process can cause significant problems in your project. For example: * If a texture asset loses its .meta file, any materials that use that texture lose their reference to that texture. To fix it, you would need to manually re-assign that texture to any materials which require it. * If a script asset loses its .meta file, any GameObjects or Prefabs that have that script assigned instead have an “unassigned script” component, and lose their functionality. To fix it, you would need to manually re-assign that script to any GameObjects which require it.

空文件夹、元文件和版本控制

Unity assigns each folder in your project’s Assets folder its own .meta file. However, some version control systems (VCS) can’t store empty folders. This means that when you add or delete an empty folder from your project, your VCS stores the .meta file as added or removed, but doesn’t store the change of adding or removing the folder itself, which can be confusing or problematic.

为帮助解决此问题,Unity 针对空文件夹采用以下特定方式:

如果 Unity 检测到一个空文件夹不再含有相应的元文件,如果该文件夹以前有元文件,Unity 会假设元文件被另一个用户通过在 VCS 中删除该文件夹时删除,并在本地删除该空文件夹。

如果 Unity 检测到文件夹有一个新的元文件,但该文件夹在本地不存在,则 Unity 会假设新元文件是被另一个用户通过在 VCS 中添加文件夹而创建,并在本地创建相应的空文件夹。

Text assets
资源数据库