Advertisement
apieceoffruit

CloudSaveTest

Oct 15th, 2023 (edited)
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using System.Threading.Tasks;
  3. using Unity.Services.Authentication;
  4. using Unity.Services.Core;
  5. using UnityEngine;
  6. using Random = UnityEngine.Random;
  7.  
  8. namespace JS
  9. {
  10.     public class QuickCloudSaveTest : MonoBehaviour
  11.     {
  12.         ISaveClient _client;
  13.  
  14.         static int ID = 0;
  15.         [SerializeField]
  16.         SomeCoolData _data;
  17.        
  18.         void Awake()
  19.         {
  20.             InitializeTheServices();
  21.          
  22.         }
  23.        
  24.         async Task SignInAnonymouslyAsync()
  25.         {
  26.             try
  27.             {
  28.                 await AuthenticationService.Instance.SignInAnonymouslyAsync();
  29.                 Debug.Log("Sign in anonymously succeeded!");
  30.        
  31.                 // Shows how to get the playerID
  32.                 Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
  33.  
  34.             }
  35.             catch (AuthenticationException ex)
  36.             {
  37.                 // Compare error code to AuthenticationErrorCodes
  38.                 // Notify the player with the proper error message
  39.                 Debug.LogException(ex);
  40.             }
  41.             catch (RequestFailedException ex)
  42.             {
  43.                 // Compare error code to CommonErrorCodes
  44.                 // Notify the player with the proper error message
  45.                 Debug.LogException(ex);
  46.             }
  47.         }
  48.        
  49.  
  50.         async void InitializeTheServices()
  51.         {
  52.            await UnityServices.InitializeAsync();
  53.            await SignInAnonymouslyAsync();
  54.            _client = new CloudSave();
  55.            Debug.Log("UnityServices.InitializeAsync() done");
  56.         }
  57.        
  58.         [ContextMenu("Load")]
  59.         async void Load()
  60.         {
  61.             _data = await _client.Load<SomeCoolData>("SomeCoolData");    
  62.         }
  63.        
  64.         [ContextMenu("Save")]
  65.         async void Save()
  66.         {
  67.             await _client.Save("SomeCoolData", _data);
  68.         }
  69.        
  70.         [ContextMenu("Delete")]
  71.         async void Delete()
  72.         {
  73.             await _client.Delete("SomeCoolData");
  74.         }
  75.        
  76.         [ContextMenu("Delete All")]
  77.         async void DeleteAll()
  78.         {
  79.             await _client.DeleteAll();
  80.         }
  81.        
  82.         [ContextMenu("Generate Some Data")]
  83.         public void GenerateSomeData() => _data = CreateRandomData();
  84.        
  85.         SomeCoolData CreateRandomData()
  86.         {
  87.             return new SomeCoolData
  88.             {
  89.                 ID = ID++,
  90.                 Name = "Player" + Random.Range(0, 100),
  91.                 Class = "Warrior",
  92.                 Level = Random.Range(1, 100),
  93.                 Health = Random.Range(1, 100),
  94.                 Mana = Random.Range(1, 100),
  95.                 Strength = Random.Range(1, 100)
  96.             };
  97.         }
  98.     }
  99.  
  100.     [Serializable]
  101.     public class SomeCoolData
  102.     {
  103.         public int ID;
  104.         public string Name;
  105.         public string Class;
  106.         public int Level;
  107.         public int Health;
  108.         public int Mana;
  109.         public int Strength;
  110.     }
  111.    
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement