SDK 0.10
Search…
⌃K
Links

Game account

Creating a player account is the first step towards using the coherence Cloud API. It is required in order to use the rest of the services.

Initialization

To initialize the Cloud API you need to provide a runtime key that can be obtained from the Settings.
public static class Play
{
// Initialises the Cloud API
public static void Init(string RuntimeKey)
}

Callbacks

public static class Auth
{
public enum Result
{
// Successful operation.
Success,
// Network error, e.g. connection failed.
NetworkError,
// Server error, e.g. exception on the server.
ServerError,
// User not found, e.g. when trying to login with non-existing user
UserNotFound,
// Invalid credentials, e.g. when trying to login with wrong password
InvalidCredentials,
// Session has expired, e.g. when trying to renew an existing session
SessionExpired,
// Feature is not enabled, e.g. K/V store
FeatureDisabled,
}
// Invoked when completing the login process.
public delegate void OnSession(Result result);
}

Guest Account

The easiest way to get started is by using a guest account. The only thing that is needed is to call LoginAsGuest. This will create a random username / password combination and will authenticate the player with the coherence Cloud.
Once logged in, the credentials are securely persisted so that if the game is restarted the player will be able to log in automatically.
If the game is uninstalled then the account credentials will be lost and a new guest account will be created next time the game is installed.
public static class Auth
{
// Authenticates the players as a guest.
// callback - invoked upon completion.
public static void LoginAsGuest(OnSession callback)
}

User Account

Another alternative is to login with a username and a password. You have to provide the user interface.
public static class Auth
{
// Authenticates the player with a username and a password.
// userName - any combination of letters, numbers, underscore and dash.
// password - any combination of ASCII characters
// autosignup - whether to create an account automatically if it
// doesn't exist and the username is not taken
// callback - invoked upon completion
public static void LoginAsUser(string userName, string password,
bool autosignup, OnSession callback)
}

Example

This example initializes the Cloud API, checks for an existing session and, if no session was found or if it expired, logs in the player as guest.
using Coherence;
using Coherence.Runtime;
using UnityEngine;
​
public class ExampleLogin : MonoBehaviour
{
void OnLogin()
{
Debug.Log(string.Format("Logged in. UserName={0}", Auth.UserName));
}
​
async void Start()
{
// Initialise the Cloud API
Play.Init(RuntimeSettings.instance);
​
// Check for existing session
var res = await Auth.IsLoggedIn();
if (res == Result.Success)
{
// Session resumed
OnLogin();
return;
}
else if (res == Result.SessionExpired || res == Result.UserNotFound)
{
// No session found, or session expired. Proceed with normal login.
res = await Auth.LoginAsGuest();
if (res == Result.Success)
{
OnLogin();
return;
}
}
​
UnityEngine.Debug.LogError(string.Format("Could not login. Error={0}", res));
}
}
Last modified 3mo ago