Skip to content

Tirtstan/Godot-Unity-Gaming-Services

Repository files navigation

Godot Unity Gaming Services

Basic SKD for connecting Unity Gaming Services (UGS) to Godot 4+ (C# Mono) (All latest stables & 4.3 beta 3).

Feel free to use this as a jumping point to create a bigger, final version or contribute directly.

Architecture

Using the wonderful RestSharp to make this process a little easier.

I have tried to keep the implementation of the SDK as similar to Unity's version for their engine as I can. I am inexperienced with using REST API's so bare with me here (or fork/contribute!).

Scripts are communicated by singletons like in Unity. I use one initial Godot Autoload to instantiated all child services.

Setup

In your Godot project, install RestSharp.

dotnet add package RestSharp

To use GodotUGS, you have to provide your game's project ID. Here's how you can:

  • In Your Browser:
    • Go to Unity Gaming Services and login or create an account.
    • At the top, choose a project or create one.
    • Go to the dashboard of the project (on the side).
    • Click on the Project settings button in the top right.
    • Copy the project ID.
  • In Godot:
    • Locate the example APIResource in "res://addons/GodotUGS/Resources/APIResource_Example.tres".
    • Fill in the project ID field.
    • Locate the GodotUGS autoload in "res://addons/GodotUGS/Autoloads/GodotUGS.tscn" and open it.
    • In the UnityServices node, provide the APIResource file through the inspector.

Done!

Unity Gaming Services

Supported

  • Authentication
    • Anonymous/Session
    • Username & Password
  • Leaderboards
  • Cloud Save

Planned

  • Economy
  • Friends
  • User Generated Content

Services

Examples

Initialization

Default

using Godot;
using Unity.Services.Core;

public override async void _Ready()
{
    UnityServices.Instance.OnInitialize += OnInitialize;

    try
    {
        await UnityServices.Instance.InitializeAsync(); // required to do anything with UGS
    }
    catch (System.Exception e)
    {
        GD.PrintErr(e);
    }
}

private void OnInitialize(bool isInitialized)
{
    if (!isInitialized)
        return;

    GD.Print("Unity Services Initialized!");
}

Custom Environment

using Godot;
using Unity.Services.Core;

public override async void _Ready()
{
    var options = new InitializationOptions();
    initializationOptions.SetEnvironmentName("experimental");

    try
    {
        await UnityServices.Instance.InitializeAsync(options);
    }
    catch (System.Exception e)
    {
        GD.PrintErr(e);
    }
}

Authentication

Anonymous & Session Sign In

using Godot;
using Unity.Services.Authentication;

// If a player has signed in previously with a session token stored on the device,
// they are signed back in regardless of if they're an anonymous player or not.
private async void SignInAnonymously()
{
    try
    {
        await AuthenticationService.Instance.SignInAnonymouslyAsync();
        GD.Print("Signed in ID: " + AuthenticationService.Instance.PlayerId);
    }
    catch (AuthenticationException e)
    {
        GD.PrintErr(e);
    }
}

Username & Password

using Godot;
using Unity.Services.Authentication;

private async void SignUp(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignUpWithUsernamePasswordAsync(username, password);
        GD.Print("Signed up ID: " + AuthenticationService.Instance.PlayerId);
    }
    catch (AuthenticationException e)
    {
        GD.PrintErr(e);
    }
}

private async void SignIn(string username, string password)
{
    try
    {
        await AuthenticationService.Instance.SignInWithUsernamePasswordAsync(username, password);
        GD.Print("Signed in ID: " + AuthenticationService.Instance.PlayerId);
    }
    catch (AuthenticationException e)
    {
        GD.PrintErr(e);
    }
}

Leaderboards

Adding a score

using Godot;
using Unity.Services.Leaderboards;

private async void AddPlayerScore(string leaderboardId, double score)
{
    try
    {
        await LeaderboardsService.Instance.AddPlayerScoreAsync(leaderboardId, score);
        GD.Print("Score added!");
    }
    catch (LeaderboardsException e)
    {
        GD.PrintErr(e);
    }
}

Getting scores

using Godot;
using Unity.Services.Leaderboards;

private async void GetScores(string leaderboardId)
{
    try
    {
        var scores = await LeaderboardsService.Instance.GetScoresAsync(leaderboardId);
        GD.Print("Total Scores: " + scores.Total);
    }
    catch (LeaderboardsException e)
    {
        GD.PrintErr(e);
    }
}

Cloud Save

Saving Items

using Godot;
using Unity.Services.CloudSave;

private async void SaveItem()
{
    try
    {
        await CloudSaveService.Instance.Data.Player.SaveAsync(
            new Dictionary<string, object>()
            {
                { "coins", 1000 },
                { "gems", 100 },
                {
                    "items",
                    new List<Item>
                    {
                        new Item(1, "Sword", "A sharp sword", 10.5f),
                        new Item(2, "Shield", "A strong shield", 15.5f)
                    }
                }
            }
        );
    }
    catch (CloudSaveException e)
    {
        GD.PrintErr(e);
    }
}

Loading Items

using Godot;
using Unity.Services.CloudSave;

private async void LoadItem()
{
    try
    {
        var data = await CloudSaveService.Instance.Data.Player.LoadAllAsync();
        foreach (var pair in data)
        {
            GD.Print(pair.Key + ": " + pair.Value);

            if (pair.Value.TryGetValueAs(out Item item))
                GD.Print(item.Description);
        }
    }
    catch (CloudSaveException e)
    {
        GD.PrintErr(e);
    }
}