Last Updated: 10/17/2022, 4:09:58 PM

# Unity SDK Installation Guide

# Overview

You can use AccelByte Cloud’s Unity SDK to integrate AccelByte Cloud services into your game. The Unity SDK acts as a bridge between your game and our services, making those services easy to access.


Download Unity SDK

# Prerequisites

Before you start the steps in this guide:

  • Create your own game client credentials and game server credentials by following the Create a Client (opens new window) guide in the IAM Clients documentation.
  • You need to have a basic understanding of the Unity Editor.

NOTE

AccelByte Cloud currently supports Unity versions 2019.2 and 2019.4 upwards.

# Install AccelByte Cloud’s Unity SDK

Follow the steps below to install the Unity SDK:

  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 file using the information based on your game. Here is an example of the JSON file:
{ 
 "Default": {
    "Namespace": "Game Namespace",
    "UsePlayerPrefs": true,
    "EnableDebugLog": true,
    "DebugLogFilter": "Log",
    "BaseUrl": "https://demo.accelbyte.io",
    "RedirectUri": "http://127.0.0.1",
    "AppId": "",
    "PublisherNamespace": "Publisher Namespace"
  }
}
  1. Create a file called AccelByteServerSDKConfig.json and add it to your project in the Assets/Resource directory.
  2. Update the AccelByteServerSDKConfig.json with code below that will be used as the Game Server configuration. Here is an example of the JSON file:
{
  "Default": {
    "Namespace": "Default",
    "BaseUrl": "",
    "RedirectUri": "http://127.0.0.1"
  }
}
  1. Create two files named AccelByteSDKOAuthConfig.json and AccelByteServerSDKOAuthConfig.json. Add both of these files to your project in the Assets/Resources directory. The contents of both these JSON files should be as follows:
{
  "Default": {
    "ClientId": "<OAuth Client ID>",
    "ClientSecret": "<OAuth Client Secret>"
  }
}

NOTE

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

NOTE

Game servers use Confidential Client Type so you need to define the Client Secret.

The next steps will depend on your game.

# Plugin Usage

Here’s an example of how to use AccelByte Cloud’s Unity SDK to create a new user profile.

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<TokenData, OAuthError> 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 debug 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",}