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.
publicstaticclassPlay{ // Initialises the Cloud APIpublicstaticvoidInit(string RuntimeKey)}
Callbacks
publicstaticclassAuth{publicenumResult { // 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.publicdelegatevoidOnSession(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.
publicstaticclassAuth{ // Authenticates the players as a guest. // callback - invoked upon completion.publicstaticvoidLoginAsGuest(OnSession callback)}
User Account
Another alternative is to login with a username and a password. You have to provide the user interface.
publicstaticclassAuth{ // 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 completionpublicstaticvoidLoginAsUser(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.
usingCoherence;usingCoherence.Runtime;usingUnityEngine;publicclassExampleLogin:MonoBehaviour{voidOnLogin() {Debug.Log(string.Format("Logged in. UserName={0}",Auth.UserName)); }asyncvoidStart() { // Initialise the Cloud APIPlay.Init(RuntimeSettings.instance); // Check for existing sessionvar res =awaitAuth.IsLoggedIn();if (res ==Result.Success) { // Session resumedOnLogin();return; }elseif (res ==Result.SessionExpired|| res ==Result.UserNotFound) { // No session found, or session expired. Proceed with normal login. res =awaitAuth.LoginAsGuest();if (res ==Result.Success) {OnLogin();return; } }UnityEngine.Debug.LogError(string.Format("Could not login. Error={0}", res)); }}