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 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.
usingPlay=Coherence.Play;voidOnLogin(){Debug.Log(string.Format("Logged in. PlayerId={0}",Play.Auth.UserId));}voidStart(){ // Initialise the Cloud APIPlay.Play.Init(apiToken); // Check for existing sessionPlay.Auth.IsLoggedIn((res) => {if (res ==Play.Result.Success) { // Session resumedOnLogin(); }elseif (res ==Play.Result.SessionExpired|| res ==Play.Result.UserNotFound) { // No session found, or session expired. Proceed with normal login.Play.Auth.LoginAsGuest((res2) => {if (res2 ==Play.Result.Success) {OnLogin();return; }UnityEngine.Debug.LogError(string.Format("Could not login. Error={0}", res2)); }); }else {UnityEngine.Debug.LogError(string.Format("Could not login. Error={0}", res)); } });}