项目组织

简介

由于Godot对项目结构或文件系统的使用没有限制,因此学习引擎时组织文件似乎很有挑战性。本教程提出了一个工作流程,应该是一个很好的起点。我们还将介绍在Godot中使用版本控制。

组织

Godot本质上是基于场景的,并按原样使用文件系统,而没有元数据或素材数据库。

与其他引擎不同,场景本身包含许多资源,因此文件系统中的文件数量要少得多。

考虑到这一点,最常见的方法是将素材编组得尽可能接近场景;当项目变大时,它使其更易于维护。

例如,通常可以将其基本素材(例如,精灵图像、3D模型网格、材质、和音乐等)放置在一个文件夹中。然后,它们可以使用一个单独的文件夹来存储使用它们的构建关卡。

/project.godot
/docs/.gdignore  # See "Ignoring specific folders" below
/docs/learning.html
/models/town/house/house.dae
/models/town/house/window.png
/models/town/house/door.png
/characters/player/cubio.dae
/characters/player/cubio.png
/characters/enemies/goblin/goblin.dae
/characters/enemies/goblin/goblin.png
/characters/npcs/suzanne/suzanne.dae
/characters/npcs/suzanne/suzanne.png
/levels/riverdale/riverdale.scn

Style guide

For consistency across projects, we recommend following these guidelines:

  • Use snake_case for folder and file names (with the exception of C# scripts). This sidesteps case sensitivity issues that can crop up after exporting a project on Windows. C# scripts are an exception to this rule, as the convention is to name them after the class name which should be in PascalCase.
  • Use PascalCase for node names, as this matches built-in node casing.
  • In general, keep third-party resources in a top-level addons/ folder, even if they aren't editor plugins. This makes it easier to track which files are third-party. There are some exceptions to this rule; for instance, if you use third-party game assets for a character, it makes more sense to include them within the same folder as the character scenes and scripts.

导入

3.0版之前的 Godot 版本从项目外的文件执行导入过程。尽管这在大型项目中很有用,但它给大多数开发人员带来了组织麻烦。

因此,现在可以从项目文件夹中透明地导入素材。

忽略具体文件夹

为防止 Godot 导入特定文件夹中的文件,请在文件夹中创建一个名为 .gdignore 的空文件(以 ``.``号开头)。这对于加快初始项目导入非常有用。

注解

要在 Windows 上创建一个以 . 号开头的文件,你可以使用文本编辑器比如 Notepad++ 或者在命令提示符(Win+R 键-->输入cmd打开)中输入:type nul > .gdignore

一旦文件夹被忽略,该文件夹中的资源就不能再使用 load()preload() 方法加载。

Ignoring a folder will also automatically hide it from the FileSystem dock, which can be useful to reduce clutter.

区分大小写

Windows and recent macOS versions use case-insensitive filesystems by default, whereas Linux distributions use a case-sensitive filesystem by default. This can cause issues after exporting a project, since Godot's PCK virtual filesystem is case-sensitive. To avoid this, it's recommended to stick to snake_case naming for all files in the project (and lowercase characters in general).

注解

You can break this rule when style guides say otherwise (such as the C# style guide). Still, be consistent to avoid mistakes.

On Windows 10, to further avoid mistakes related to case sensitivity, you can also make the project folder case-sensitive. After enabling the Windows Subsystem for Linux feature, run the following command in a PowerShell window:

# To enable case-sensitivity:
fsutil file setcasesensitiveinfo <path to project folder> enable

# To disable case-sensitivity:
fsutil file setcasesensitiveinfo <path to project folder> disable

If you haven't enabled the Windows Subsystem for Linux, you can enter the following line in a PowerShell window running as Administrator then reboot when asked:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux