By default, the CloudService will automatically authenticate as a Guest User, unless you disable Auto Login As Guest option in the CoherenceBridge inspector.
If you disable the automatic login, you can handle Player authentication manually through the CloudService.GameServices.AuthService API.
In this example, you can see how you can manually login as a Guest:
usingCoherence.Cloud;usingCoherence.Toolkit;usingSystem.Collections;usingUnityEngine;usingResult=Coherence.Runtime.Result;publicclassLoginAsGuestExample:MonoBehaviour{publicCoherenceBridge bridge;privateCloudService cloudService;voidStart() { cloudService =bridge.CloudService;StartCoroutine(CheckRefreshLogin()); }privateIEnumeratorCheckRefreshLogin() {IAuthClient authService =cloudService.GameServices.AuthService;while (!cloudService.IsConnectedToCloud&&authService.SessionTokenRefreshResult==null) {yieldreturnnull; } // If refresh failed, we have to login as a guest againif (authService.SessionTokenRefreshResult==Result.InvalidCredentials) {var task =authService.LoginAsGuest();while (!task.IsCompleted) {yieldreturnnull; } } // At this point, we are ready to fully utilize the Cloud Service }}
You can also choose to allow your users to create their own Player accounts, you can use the Authentication Service API in a similar fashion to allow for manual authentication:
usingCoherence.Cloud;usingCoherence.Toolkit;usingSystem.Collections;usingUnityEngine;usingResult=Coherence.Runtime.Result;publicclassLoginAsUserExample:MonoBehaviour{publicCoherenceBridge bridge;publicstring username;publicstring password;privateCloudService cloudService;voidStart() { cloudService =bridge.CloudService;StartCoroutine(LoginAsAUser()); }privateIEnumeratorLoginAsAUser() {IAuthClient authService =cloudService.GameServices.AuthService;while (!cloudService.IsConnectedToCloud&&authService.SessionTokenRefreshResult==null) {yieldreturnnull; } // If refresh failed, we have to login as a user with the given credentials againif (authService.SessionTokenRefreshResult==Result.InvalidCredentials) {var task =authService.LoginWithPassword(username, password,true);while (!task.IsCompleted) {yieldreturnnull; } } // At this point, we are ready to fully utilize the Cloud Service }}
Game Services
In the subsections of this page you will find example usages of the GameServices API that can be found in CloudService.GameServices.
Key-value store
The key-value store provides a simple persistence layer for the players.
CloudService.GameServices.KvStoreService
The player needs need to be logged in to use the key-value store.
This class provides the methods to set, get and unset key-value pairs. This is executed within the context of the currently logged in player.
Example
usingCoherence.Cloud;usingCoherence.Toolkit;usingSystem.Collections;usingUnityEngine;usingResult=Coherence.Runtime.Result;publicclassKeyValueStoreExample:MonoBehaviour{publicCoherenceBridge bridge;publicstring username;publicstring password;publicstring myKey;publicstring myValue;privateCloudService cloudService;voidStart() { cloudService =bridge.CloudService;StartCoroutine(LoginAsAUserAndUseKvStore()); }privateIEnumeratorLoginAsAUserAndUseKvStore() {IAuthClient authService =cloudService.GameServices.AuthService;while (!cloudService.IsConnectedToCloud&&authService.SessionTokenRefreshResult==null) {yieldreturnnull; } // If refresh failed, we have to login as a user with the given credentials againif (authService.SessionTokenRefreshResult==Result.InvalidCredentials) {var task =authService.LoginWithPassword(username, password,true);while (!task.IsCompleted) {yieldreturnnull; } } // At this point, we are ready to fully utilize the Cloud ServiceKvStoreClient keyValueStore =cloudService.GameServices.KvStoreService; // Sets a value // key: lowercase letters, numbers, underscore, dash // val: any string (null is not allowed)keyValueStore.Set(myKey, myValue); // Gets a value // key: lowercase letters, numbers, underscore, dashvar foo =keyValueStore.Get(myKey);Debug.Log(string.Format("foo={0}", foo)); // foo=1 // Unsets a value, removing it from the store // key: lowercase letters, numbers, underscore, dashkeyValueStore.Unset(myKey); }}
Limitations
Size: there are no limits to the number of stored key/values as long as the total size is less than 256 kB.
Requests: Set/Get/Unset can be called unlimited amount of times but the execution may be throttled.