Skip to content

hathora/cloud-sdk-unity

Repository files navigation

Serverless cloud hosting for multiplayer games

Unity SDK

SDK Installation

The SDK can either be compiled using dotnet build and the resultant .dll file can be copied into your Unity project's Assets folder, or you can copy the source code directly into your project.

The SDK relies on Newtonsoft's JSON.NET Package which can be installed via the Unity Package Manager.

To do so open the Package Manager via Window > Package Manager and click the + button then Add package from git URL... and enter com.unity.nuget.newtonsoft-json and click Add.

SDK Example Usage

Example

using HathoraCloud;
using HathoraCloud.Models.Shared;

var sdk = new HathoraCloudSDK(
    security: new Security() {
        HathoraDevToken = "<YOUR_BEARER_TOKEN_HERE>",
    },
    appId: "app-af469a92-5b45-4565-b3c4-b79878de67d2");

AppConfig req = new AppConfig() {
    AppName = "minecraft",
    AuthConfiguration = new AuthConfiguration() {},
};


using(var res = await sdk.AppV1.CreateAppAsync(req))
{
    // handle response
}

Available Resources and Operations

  • LoginAnonymous - Returns a unique player token for an anonymous user.
  • LoginGoogle - Returns a unique player token using a Google-signed OIDC idToken.
  • LoginNickname - Returns a unique player token with a specified nickname for a user.
  • GetPingServiceEndpointsDeprecated - Returns an array of V1 regions with a host and port that a client can directly ping. Open a websocket connection to wss://<host>:<port>/ws and send a packet. To calculate ping, measure the time it takes to get an echo packet back. ⚠️ Deprecated
  • GetPingServiceEndpoints - Returns an array of all regions with a host and port that a client can directly ping. Open a websocket connection to wss://<host>:<port>/ws and send a packet. To calculate ping, measure the time it takes to get an echo packet back.
  • CreateLobbyDeprecated - Create a new lobby for an application. A lobby object is a wrapper around a room object. With a lobby, you get additional functionality like configuring the visibility of the room, managing the state of a match, and retrieving a list of public lobbies to display to players. ⚠️ Deprecated
  • CreateLocalLobby - ⚠️ Deprecated
  • CreatePrivateLobby - ⚠️ Deprecated
  • CreatePublicLobby - ⚠️ Deprecated
  • GetLobbyInfo - Get details for a lobby. ⚠️ Deprecated
  • ListActivePublicLobbiesDeprecatedV2 - Get all active lobbies for a an application. Filter by optionally passing in a region. Use this endpoint to display all public lobbies that a player can join in the game client. ⚠️ Deprecated
  • SetLobbyState - Set the state of a lobby. State is intended to be set by the server and must be smaller than 1MB. Use this endpoint to store match data like live player count to enforce max number of clients or persist end-game data (i.e. winner or final scores). ⚠️ Deprecated
  • CreateLobby - Create a new lobby for an application. A lobby object is a wrapper around a room object. With a lobby, you get additional functionality like configuring the visibility of the room, managing the state of a match, and retrieving a list of public lobbies to display to players.
  • GetLobbyInfoByRoomId - Get details for a lobby.
  • GetLobbyInfoByShortCode - Get details for a lobby. If 2 or more lobbies have the same shortCode, then the most recently created lobby will be returned.
  • ListActivePublicLobbies - Get all active lobbies for a given application. Filter the array by optionally passing in a region. Use this endpoint to display all public lobbies that a player can join in the game client.

Global Parameters

Global Parameters

A parameter is configured globally. This parameter may be set on the SDK client instance itself during initialization. When configured as an option during SDK initialization, This global value will be used as the default on the operations that use it. When such operations are called, there is a place in each to override the global value, if needed.

For example, you can set appId to "app-af469a92-5b45-4565-b3c4-b79878de67d2" at SDK initialization and then you do not have to pass the same value on calls to operations like DeleteApp. But if you want to do so you may, which will locally override the global setting. See the example code below for a demonstration.

Available Globals

The following global parameter is available.

Name Type Required Description
appId string The AppId parameter.

Example

using HathoraCloud;
using HathoraCloud.Models.Shared;
using HathoraCloud.Models.Operations;

var sdk = new HathoraCloudSDK(
    security: new Security() {
        HathoraDevToken = "<YOUR_BEARER_TOKEN_HERE>",
    },
    appId: "app-af469a92-5b45-4565-b3c4-b79878de67d2");

DeleteAppRequest req = new DeleteAppRequest() {};


using(var res = await sdk.AppV1.DeleteAppAsync(req))
{
    // handle response
}

Server Selection

Server Selection

Select Server by Index

You can override the default server globally by passing a server index to the serverIndex: number optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Variables
0 https://api.hathora.dev None
1 https:/// None

Override Server URL Per-Client

The default server can also be overridden globally by passing a URL to the serverUrl: str optional parameter when initializing the SDK client instance. For example:

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or thow an exception. If Error objects are specified in your OpenAPI Spec, the SDK will raise the appropriate type.

Error Object Status Code Content Type
HathoraCloud.Models.Errors.ApiError 401,422,429,500 application/json
HathoraCloud.Models.Errors.SDKException 4xx-5xx /

Example

using HathoraCloud;
using HathoraCloud.Models.Shared;
using System;
using HathoraCloud.Models.Errors;

var sdk = new HathoraCloudSDK(
    security: new Security() {
        HathoraDevToken = "<YOUR_BEARER_TOKEN_HERE>",
    },
    appId: "app-af469a92-5b45-4565-b3c4-b79878de67d2");

AppConfig req = new AppConfig() {
    AppName = "minecraft",
    AuthConfiguration = new AuthConfiguration() {},
};

try
{
    using(var res = await sdk.AppV1.CreateAppAsync(req))
    {
            // handle response
    }
}
catch (Exception ex)
{
    if (ex is ApiError)
    {
        // handle exception
    }
    else if (ex is HathoraCloud.Models.Errors.SDKException)
    {
        // handle exception
    }
}

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
HathoraDevToken http HTTP Bearer

You can set the security parameters through the security optional parameter when initializing the SDK client instance. For example:

using HathoraCloud;
using HathoraCloud.Models.Shared;

var sdk = new HathoraCloudSDK(
    security: new Security() {
        HathoraDevToken = "<YOUR_BEARER_TOKEN_HERE>",
    },
    appId: "app-af469a92-5b45-4565-b3c4-b79878de67d2");

AppConfig req = new AppConfig() {
    AppName = "minecraft",
    AuthConfiguration = new AuthConfiguration() {},
};


using(var res = await sdk.AppV1.CreateAppAsync(req))
{
    // handle response
}

Per-Operation Security Schemes

Some operations in this SDK require the security scheme to be specified at the request level. For example:

using HathoraCloud;
using HathoraCloud.Models.Operations;
using HathoraCloud.Models.Shared;

var sdk = new HathoraCloudSDK(appId: "app-af469a92-5b45-4565-b3c4-b79878de67d2");

CreatePrivateLobbyDeprecatedRequest req = new CreatePrivateLobbyDeprecatedRequest() {};


using(var res = await sdk.LobbyV1.CreatePrivateLobbyDeprecatedAsync(
    new CreatePrivateLobbyDeprecatedSecurity() {
    PlayerAuth = "<YOUR_BEARER_TOKEN_HERE>",
},
    req))
{
    // handle response
}

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release!

SDK Created by Speakeasy