Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading.Tasks;
- using Unity.Services.Authentication;
- using Unity.Services.Core;
- using UnityEngine;
- using Random = UnityEngine.Random;
- namespace JS
- {
- public class QuickCloudSaveTest : MonoBehaviour
- {
- ISaveClient _client;
- static int ID = 0;
- [SerializeField]
- SomeCoolData _data;
- void Awake()
- {
- InitializeTheServices();
- }
- async Task SignInAnonymouslyAsync()
- {
- try
- {
- await AuthenticationService.Instance.SignInAnonymouslyAsync();
- Debug.Log("Sign in anonymously succeeded!");
- // Shows how to get the playerID
- Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
- }
- catch (AuthenticationException ex)
- {
- // Compare error code to AuthenticationErrorCodes
- // Notify the player with the proper error message
- Debug.LogException(ex);
- }
- catch (RequestFailedException ex)
- {
- // Compare error code to CommonErrorCodes
- // Notify the player with the proper error message
- Debug.LogException(ex);
- }
- }
- async void InitializeTheServices()
- {
- await UnityServices.InitializeAsync();
- await SignInAnonymouslyAsync();
- _client = new CloudSave();
- Debug.Log("UnityServices.InitializeAsync() done");
- }
- [ContextMenu("Load")]
- async void Load()
- {
- _data = await _client.Load<SomeCoolData>("SomeCoolData");
- }
- [ContextMenu("Save")]
- async void Save()
- {
- await _client.Save("SomeCoolData", _data);
- }
- [ContextMenu("Delete")]
- async void Delete()
- {
- await _client.Delete("SomeCoolData");
- }
- [ContextMenu("Delete All")]
- async void DeleteAll()
- {
- await _client.DeleteAll();
- }
- [ContextMenu("Generate Some Data")]
- public void GenerateSomeData() => _data = CreateRandomData();
- SomeCoolData CreateRandomData()
- {
- return new SomeCoolData
- {
- ID = ID++,
- Name = "Player" + Random.Range(0, 100),
- Class = "Warrior",
- Level = Random.Range(1, 100),
- Health = Random.Range(1, 100),
- Mana = Random.Range(1, 100),
- Strength = Random.Range(1, 100)
- };
- }
- }
- [Serializable]
- public class SomeCoolData
- {
- public int ID;
- public string Name;
- public string Class;
- public int Level;
- public int Health;
- public int Mana;
- public int Strength;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement