Last Updated: 9/2/2022, 11:14:49 AM

# Unity SDK Installation Guide

You can use AccelByte Cloud’s Unity SDK to help implement AccelByte Cloud services in your game. The SDK acts as a bridge between your game and our services, making those services easy to access. Before processing to follow this tutorial, please create your own game client credentials and game server credentials by following the Create a Client (opens new window) tutorial in the IAM Clients documentation.

NOTE

AccelByte Cloud currently supports Unity versions 2019.2 and 2019.4 upwards.


Download Unity SDK

# Install AccelByte Cloud’s Unity SDK Plugin

There are two different ways to install the Unity plugin: from the source code or from the package. Make sure you’ve already configured the game client before installing the Unity plugin.

# From the Source Code

  1. Create a file for Game Client configuration named AccelByteSDKConfig.json.

  2. Copy the AccelByteSDKConfig.json file to the Assets/Resource directory.

  3. Fill in the AccelByteSDKConfig.json with the information based on your game. Here is the contents of the JSON file:

    {
        "PublisherNamespace": "PUBLISHER_NAMESPACE",
        "Namespace": "GAME_NAMESPACE",
        "UseSessionManagement": true,
        "BaseUrl": "https://api.demo.accelbyte.io",
        "LoginServerUrl": "https://api.demo.accelbyte.io",
        "IamServerUrl": "https://api.demo.accelbyte.io/iam",
        "PlatformServerUrl": "https://api.demo.accelbyte.io/platform",
        "BasicServerUrl": "https://api.demo.accelbyte.io/basic",
        "LobbyServerUrl": "wss://api.demo.accelbyte.io/lobby/",
        "CloudStorageServerUrl": "https://api.demo.accelbyte.io/binary-store",
        "TelemetryServerUrl": "https://api.demo.accelbyte.io/telemetry",
        "GameProfileServerUrl": "https://api.demo.accelbyte.io/soc-profile",
        "StatisticServerUrl": "https://api.demo.accelbyte.io/statistic",
        "LeaderboardServerUrl": "https://api.demo.accelbyte.io/leaderboard",
        "AgreementServerUrl": "https://api.demo.accelbyte.io/agreement",
        "ClientId": "GAME_CLIENT_ID",
        "ClientSecret": "GAME_CLIENT_SECRET",
        "RedirectUri": "http://127.0.0.1"
    }
    

    NOTE

    Leave the Client Secret empty if the Game Client uses Public Client Type (opens new window).

  4. Copy the AccelByteServerSDKConfig.json from the repository and add it to your project in the Assets/Resource directory.

  5. Fill in the AccelByteServerSDKConfig.json with code below that’ll be reserved as the Game Server configuration. Here is the contents of the JSON file:

    {
      "PublisherNamespace": "PUBLISHER_NAMESPACE",
      "Namespace": "GAME_NAMESPACE",
      "BaseUrl": "https://api.demo.accelbyte.io",
      "DSMServerUrl": "https://api.demo.accelbyte.io/dsm",
      "IamServerUrl": "https://api.demo.accelbyte.io/iam",
      "ClientId": "GAME_SERVER_ID",
      "ClientSecret": "GAME_SERVER",
      "RedirectUri": "http://127.0.0.1"
    }
    

    NOTE

    For game servers, you need to define the Client Secret since Game Server uses Confidential Client Type.

For the next steps, it depends on your game.

# From the Package

  1. Download the Unity package (opens new window).

  2. Open the Unity package using the Unity Editor and select all of the components, including any API you want to use.

    NOTE

    You can exclude any API that you’re sure you won’t use, such as Cloud Storage, Telemetry, etc. If you need these APIs at a later date, you’ll be able to import them without having to reinstall the Unity plugin.

  3. Click Import and wait for the installation to finish.

# Plugin Usage

Here’s an example of how to use AccelByte Cloud’s Unity SDK to create a new user profile. Please note that you must be authorized before you can implement any backend services.

using AccelByte.Api;
using AccelByte.Models;
using AccelByte.Core;

class MainMenu
{
    public void OnLoginClick(string email, string password)
    {
        var user = AccelBytePlugin.GetUser();
        user.LoginWithUsername(email, password,
            result =>
            {
                if (result.IsError)
                {
                    Debug.Log("Login failed");
                }
                else
                {
                    Debug.Log("Login successful");
                }
            });
    }

    public void OnCreateProfileClick()
    {
        var userProfiles = AccelBytePlugin.GetUserProfiles();

        userProfiles.CreateUserProfile(
            new CreateUserProfileRequest
            {
                language = "en",
                timeZone = "Asia/Jakarta",
                firstName = "John",
                lastName = "Doe",
                dateOfBirth = "2000-01-01"
            },
            result =>
            {
                if (result.IsError)
                {
                    Debug.Log("Creating user profile failed");
                }
                else
                {
                    Debug.Log("User profile created.");
                    Debug.Log("First Name: " + result.firstName);
                    Debug.Log("Last Name: " + result.lastName);
                }
            });
    }
}

# Debug Log

AccelByte Cloud’s Unity SDK includes a debug log, which appears both in the Unity editor and on a player’s device. You can filter which types of messages are included in this log, or even disable the log altogether. Disabling the log can be useful in the build process to skip logs from the AccelByte services.

The log is enabled by default. To disable it, open the AccelByteSDKConfig.json file and change the EnableDebugLog value to false. To enable the log again, just change this value back to true.

{
...
  "UsePlayerPrefs": false,
  "EnableDebugLog": true,
  "DebugLogFilter": "log",}

You can also filter which messages are included in the log by defining the Log Type, as seen below:

Log Type Remarks
Log This is the default setting, used to display all log messages
Warning Display warning, assert, error, and exception log messages
Assert Display assert, error, and exception log messages
Error Display error and exception log messages
Exception Display exception log messages

Here’s an example of how to apply filtering messages based on the Log Type. You can make this change in AccelByteSDKConfig.json.

{
...
  "UseSessionManagement": true,
  "UsePlayerPrefs": false,
  "EnableDebugLog": true,
  "DebugLogFilter": "log",}