Speakeasy Logo
Skip to Content

Create Unity SDKs from OpenAPI / Swagger

Unity SDK Overview

The Speakeasy Unity C# SDK supports Unity 2021.3 LTS and above and is designed to be strongly typed, light on external dependencies, easy to debug, and easy to use.

Some of the core features of the SDK include:

  • Interfaces for core components allow for dependency injection and mocking.
  • Generated C# doc comments to enhance the SDK’s IntelliSense compatibility and developer experience.
  • Async/await support for all API calls, which can easily be wrapped in coroutines if needed.
  • Optional pagination support for supported APIs.
  • Support for complex number types:
    • System.Numbers.BigInteger
    • System.Decimal
  • Support for both string- and integer-based enums.
  • Streaming downloads for files.

The SDK includes minimal dependencies. The only external dependencies are:

  • newtonsoft.json for JSON serialization and deserialization.
  • The UnityEngine libraries.

Unity Package Structure

HTTP Client

The Unity C# SDK provides an interface for the HTTP client used to make API calls. A custom HTTP client can be provided to the SDK as long as it conforms to the interface.

By default, the SDK will instantiate its own client using UnityWebRequest.SendWebRequest() to send the request, but this can be overridden by providing a custom implementation of the ISpeakeasyHttpClient interface:

This is useful if you’re using a custom HTTP client that supports UnityWebRequests and a proxy or other custom configuration, or to provide a client preconfigured with standard headers.

Data Types and Classes

The C# SDK uses as many native types from the standard library as possible, for example:

  • string
  • System.DateTime
  • int
  • long
  • System.Numberics.BigInteger
  • float
  • double
  • decimal
  • bool

The SDK will only fall back on custom types when the native types are not suitable, for example:

  • A custom DateOnly class for date types
  • Custom enum types for string and integer based enums

Speakeasy will generate standard C# classes with public fields that use attributes to guide the serialization and deserialization processes.

The classes are also Serializable, with [SerializeField] attributes on the fields allowing them to be used in the Unity Inspector.

Parameters

If parameters are defined in the OpenAPI document, Speakeasy will generate methods with parameters.

The number of parameters defined should not exceed the maxMethodParams value configured in the gen.yaml file. If they do or the maxMethodParams value is set to 0, Speakeasy will generate a request object that allows for all parameters to be passed in a single object.

Async Support

The Unity C# SDK is generated with async/await support for all API calls, which can easily be wrapped in coroutines if needed. For example:

The example above can be used like so:

Due to the nature of the underlying UnityWebRequest, the response is an IDisposable object that should be disposed of when finished with or used within a using statement as shown above.

Errors

The Unity C# SDK will throw exceptions for network or invalid request errors.

For unsuccessful responses, the SDK will return a response object containing the status code and response body, which can be checked for the status of the method call.

User Agent Strings

The Unity SDK will include a user agent  string in all requests that can be leveraged for tracking SDK usage amongst broader API usage. The format is as follows:

Where

  • SDKVersion is the version of the SDK, defined in gen.yaml and released
  • GenVersion is the version of the Speakeasy generator
  • DocVersion is the version of the OpenAPI document
  • PackageName is the name of the package defined in gen.yaml

Last updated on