Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

C# 特征

本页概述了C#和Godot的常用特征以及它们如何一起使用.

类型转换和强制转换

C#是一种静态类型语言. 因此, 您无法执行以下操作:

var mySprite = GetNode("MySprite");
mySprite.SetFrame(0);

The method GetNode() returns a Node instance. You must explicitly convert it to the desired derived type, Sprite2D in this case.

为此, 在C#中有多种选择.

强制转换和类型检查

Throws InvalidCastException if the returned node cannot be cast to Sprite2D. You would use it instead of the as operator if you are pretty sure it won't fail.

Sprite2D mySprite = (Sprite2D)GetNode("MySprite");
mySprite.SetFrame(0);

使用AS运算符

The as operator returns null if the node cannot be cast to Sprite2D, and for that reason, it cannot be used with value types.

Sprite2D mySprite = GetNode("MySprite") as Sprite2D;
// Only call SetFrame() if mySprite is not null
mySprite?.SetFrame(0);

使用泛型方法

还提供了泛型方法以使该类型转换透明.

GetNode <T>() 在返回之前强制转换节点. 如果节点无法强制转换为所需类型, 它将抛出一个 InvalidCastException.

Sprite2D mySprite = GetNode<Sprite2D>("MySprite");
mySprite.SetFrame(0);

GetNodeOrNull <T>() 使用 as 运算符, 如果节点无法强制转换为所需类型, 则返回 null.

Sprite2D mySprite = GetNodeOrNull<Sprite2D>("MySprite");
// Only call SetFrame() if mySprite is not null
mySprite?.SetFrame(0);

使用IS运算符进行类型检查

To check if the node can be cast to Sprite2D, you can use the is operator. The is operator returns false if the node cannot be cast to Sprite2D, otherwise it returns true. Note that when the is operator is used against null the result is always going to be false.

if (GetNode("MySprite") is Sprite2D)
{
    // Yup, it's a Sprite2D!
}

if (null is Sprite2D)
{
    // This block can never happen.
}

You can also declare a new variable to conditionally store the result of the cast if the is operator returns true.

if (GetNode("MySprite") is Sprite2D mySprite)
{
    // The mySprite variable only exists inside this block, and it's never null.
    mySprite.SetFrame(0);
}

对于更高级的类型检查, 你可以查看 模式匹配.

预处理器符号定义

为了能够根据目标编译环境改变 C# 代码,Godot 提供了一组符号定义。

备注

如果是Godot 3.2之前创建的项目, 你需要修改或重新生成你的 csproj 文件来使用这个功能(可与一个3.2+新项目中的 <DefineConstants> 相对比).

示例

例如, 你可以根据平台更改代码:

    public override void _Ready()
    {
#if GODOT_SERVER
        // Don't try to load meshes or anything, this is a server!
        LaunchServer();
#elif GODOT_32 || GODOT_MOBILE || GODOT_WEB
        // Use simple objects when running on less powerful systems.
        SpawnSimpleObjects();
#else
        SpawnComplexObjects();
#endif
    }

或者你可以检测代码所在的引擎, 这对于制作跨引擎库很有用:

    public void MyPlatformPrinter()
    {
#if GODOT
        GD.Print("This is Godot.");
#elif UNITY_5_3_OR_NEWER
        print("This is Unity.");
#else
        throw new NotSupportedException("Only Godot and Unity are supported.");
#endif
    }

Or you can write scripts that target multiple Godot versions and can take advantage that are only available on some of those versions:

    public void UseCoolFeature()
    {
#if GODOT4_3_OR_GREATER || GODOT4_2_2_OR_GREATER
        // Use CoolFeature, that was added to Godot in 4.3 and cherry-picked into 4.2.2, here.
#else
        // Use a workaround for the absence of CoolFeature here.
#endif
    }

完整的定义列表

  • 所有 Godot 项目都会定义 GODOT

  • TOOLS is defined when building with the Debug configuration (editor and editor player).

  • GODOT_REAL_T_IS_DOUBLE is defined when the GodotFloat64 property is set to true.

  • 根据架构是 64 位还是 32 位,会定义 GODOT_64GODOT_32

  • One of GODOT_LINUXBSD, GODOT_WINDOWS, GODOT_OSX, GODOT_ANDROID, GODOT_IOS, GODOT_HTML5, or GODOT_SERVER depending on the OS. These names may change in the future. These are created from the get_name() method of the OS singleton, but not every possible OS the method returns is an OS that Godot with .NET runs on.

  • GODOTX, GODOTX_Y, GODOTX_Y_Z, GODOTx_OR_GREATER, GODOTX_y_OR_GREATER, and GODOTX_Y_z_OR_GREATER, where X, Y, and Z are replaced by the current major, minor and patch version of Godot. x, y, and z are replaced by 0 to to the current version for that component.

    备注

    These defines were first added in Godot 4.0.4 and 4.1. Version defines for prior versions do not exist, regardless of the current Godot version.

    For example: Godot 4.0.5 defines GODOT4, GODOT4_OR_GREATER, GODOT4_0, GODOT4_0_OR_GREATER, GODOT4_0_5, GODOT4_0_4_OR_GREATER, and GODOT4_0_5_OR_GREATER. Godot 4.3.2 defines GODOT4, GODOT4_OR_GREATER, GODOT4_3, GODOT4_0_OR_GREATER, GODOT4_1_OR_GREATER, GODOT4_2_OR_GREATER, GODOT4_3_OR_GREATER, GODOT4_3_2, GODOT4_3_0_OR_GREATER, GODOT4_3_1_OR_GREATER, and GODOT4_3_2_OR_GREATER.

导出 时, 根据导出功能, 还可能定义以下内容:

  • GODOT_PC , GODOT_MOBILEGODOT_WEB 中的一种, 取决于平台类型.

  • One of GODOT_WINDOWS, GODOT_LINUXBSD, GODOT_MACOS, GODOT_UWP, GODOT_HAIKU, GODOT_ANDROID, GODOT_IOS, or GODOT_WEB depending on the platform.

如果想要参考一个示例项目, 可以参考该OS测试demo:https://github.com/godotengine/godot-demo-projects/tree/master/misc/os_test