Setting up spine-godot with C#
October 2nd, 2023
This blog post briefly explains the steps to get started using C# with spine-godot and how it differs from using GDScript.
Installation
You can download our pre-built Godot 4.1 editor and export template binaries with C# support from the spine-godot runtime documentation. Follow the documentation there for basic installation instructions.
C# project setup
To use our Godot editor binaries with C# support, you need to take an extra step when setting up a new Godot project:
1. Create a Godot project
First, create a new Godot project using the downloaded Godot editor binary that has C# support.
Failed to load .NET runtime
If the Godot editor fails to load the .NET runtime, the following error message will appear at startup:
As described in the message, please install the .NET SDK 6.0 or later from Microsoft's official download site and restart Godot.
2. Create a godot-nuget folder
Close Godot and open your project folder. In the root directory, create a new folder called godot-nuget
.
3. Copy the C# assemblies
Copy the Godot C# assemblies into the godot-nuget
folder. If you are using Windows or Linux, you can find the assemblies in the downloaded Godot editor ZIP file:
- Windows:
godot-editor-windows-mono.zip\GodotSharp\Tools\
- Linux:
godot-editor-linux-mono.zip/GodotSharp/Tools/
If you are using macOS:
- macOS: Navigate to
Godot.app/Contents/Resources/GodotSharp/Tools/
by right clicking theGodot.app
file in Finder, selectShow Package Contents
, then navigate toContents/Resources/GodotSharp/Tools/
.
Copy the following files into your godot-nuget
folder:
GodotSharpEditor.<version>.snupkg
Godot.NET.Sdk.<version>.nupkg
Godot.SourceGenerators.<version>.nupkg
GodotSharp.<version>.nupkg
GodotSharp.<version>.snupkg
GodotSharpEditor.<version>.nupkg
The <version>
depends on which Godot version you downloaded, e.g. 4.1.1
.
4. Create a nuget.config file
Finally, create a new file called nuget.config
in the root directory of your project with the following content:
<packageSources>
<!-- package source is additive -->
<add key="godot-nuget" value="./godot-nuget" />
</packageSources>
</configuration>
This configures the godot-nuget
directory to be a package source for NuGet packages. Instead of fetching the official Godot C# assemblies from the NuGet package registry, the assemblies from the godot-nuget
directory will be used, which also include the C# bindings for the spine-godot runtime.
You can now open your project in Godot and use the Godot and spine-godot C# APIs instead of GDScript!
Animate a skeleton with C#
Here is simple example code to animate a Spine skeleton with a C# script attached to a SpineSprite node:
C#:
using System;
public partial class SpineSprite : SpineSprite {
public override void _Ready () {
GetAnimationState().SetAnimation("run", true, 0);
}
}
Compared to the same code written in GDScript, there is little difference except that the API uses PascalCase and requires a semicolon at the end according to C# code conventions.
GDScript:
func _ready():
get_animation_state().set_animation("run", true, 0)
In GDScript, you can get the skeleton outside of a function using the @onready
annotation. In C#, you cannot call the API in class definitions so you have to get it inside a function. Here is a comparison of C# and GDScript code to flip the skeleton and queue animations:
C#:
using System;
public partial class SpineSprite : SpineSprite {
private SpineSkeleton spineSkeleton;
private SpineAnimationState spineSpriteAnimState;
public override void _Ready () {
spineSkeleton = GetSkeleton();
spineSpriteAnimState = GetAnimationState();
spineSkeleton.SetScaleX(-1);
spineSpriteAnimState.SetAnimation("idle", true, 0);
spineSpriteAnimState.AddAnimation("run", 2, true, 0);
}
}
GDScript:
@onready var spineSkeleton : SpineSkeleton = get_skeleton()
@onready var spineSpriteAnimState : SpineAnimationState = get_animation_state()
func _ready():
spineSkeleton.set_scale_x(-1)
spineSpriteAnimState.set_animation("idle", true, 0)
spineSpriteAnimState.add_animation("run", 2, true, 0)
For the details of how the APIs are mapped from GDScript to C#, please refer to the Godot C# documentation.
C# examples
To inspect and experiment with the C# examples for spine-godot:
- Clone the spine-runtimes Git repository or download the latest version as a ZIP and unzip it.
- Open the
spine-runtimes/spine-godot/example-v4-csharp/
folder and click theproject.godot
file to open it.
You can find various example scenes and scripts using C# under the examples
folder in the FileSystem dock.
If you are having trouble using spine-godot with C# support, don't hesitate to post your questions on the Spine Forum!