Godot 项目中的文件路径

本页面会讲解 Godot 项目中文件路径的工作原理。你会学到如何在项目中使用 res://user:// 访问路径,以及 Godot 会在硬盘的哪些位置存储用户文件。

路径分隔符

To as many platforms as possible, Godot only accepts UNIX-style path separators (/). These work on all platforms, including Windows.

Instead of writing paths like C:\Projects, in Godot, you should write C:/Projects.

访问项目文件夹中的文件

Godot considers that a project exists in any folder that contains a project.godot text file, even if the file is empty. The folder that contains this file is your project's root folder.

You can access any file relative to it by writing paths starting with res://, which stands for resources. For example, you can access an image file character.png located in the project's root folder in code with the following path: res://some_texture.png.

访问持久化用户数据

To store persistent data files, like the player's save or settings, you want to use user:// instead of res:// as your path's prefix. This is because when the game is running, the project's file system will likely be read-only.

The user:// prefix points to a different directory on the user's device. On mobile and consoles, this path is unique to the project. On desktop, the engine stores user files in ~/.local/share/godot/app_userdata/Name on macOS and Linux, and %APPDATA%/Name on Windows. Name is based on the application name defined in the Project Settings, but you can override it on a per-platform basis using feature tags.

On HTML5 exports, user:// will refer to a virtual filesystem stored on the device via IndexedDB. (Interaction with the main filesystem can still be performed through the JavaScript singleton.)

编辑器数据路径

The editor uses different paths for user data, user settings, and cache, depending on the platform. By default, these paths are:

类型

位置

用户数据

  • Windows:%APPDATA%\Godot\

  • macOS:~/Library/Application Support/Godot/

  • Linux:~/.local/share/godot/

用户设置

  • Windows:%APPDATA%\Godot\

  • macOS:~/Library/Application Support/Godot/

  • Linux:~/.config/godot/

缓存

  • Windows:%TEMP%\Godot\

  • macOS:~/Library/Caches/Godot/

  • Linux:~/.cache/godot/

  • 用户数据包含导出模板和具体项目数据。

  • 用户设置包含编辑器设置、文本编辑器主题、脚本模板等。

  • 缓存包含临时数据。当 Godot 关闭时,它可以被安全地移除。

Godot complies with the XDG Base Directory Specification on all platforms. You can override environment variables following the specification to change the editor and project data paths.

注解

如果您使用 Godot 打包为 flatpak ,编辑器数据路径将位于 ~/.var/app/org.godotengine.godot/ 下的子文件夹中。

自包含模式

If you create a file called ._sc_ or _sc_ in the same directory as the editor binary, Godot will enable self-contained mode. This mode makes Godot write all user data to a directory named editor_data/ in the same directory as the editor binary. You can use it to create a portable installation of the editor.

Steam 版本的 Godot 默认使用自包含模式。