Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public static class GSFU_Demo_Utils
  6. {
  7.     [System.Serializable]
  8.     public struct PlayerInfo
  9.     {
  10.         public string name;
  11.         public int level;
  12.         public float health;
  13.         public string role;
  14.     }
  15.    
  16.     private static PlayerInfo player;
  17.     private static string tableName;
  18.    
  19.     static GSFU_Demo_Utils()
  20.     {
  21.         // Create an example object.
  22.         player = new PlayerInfo();
  23.         player.name = "Mithrandir";
  24.         player.level = 99;
  25.         player.health = 98.6f;
  26.         player.role = "Wizzard";
  27.        
  28.         tableName = "PlayerInfo";
  29.     }
  30.  
  31.     // Create a table in the cloud to store objects of type PlayerInfo.
  32.     public static void CreatePlayerTable(bool runtime)
  33.     {
  34.         Debug.Log("<color=yellow>Creating a table in the cloud for players data.</color>");
  35.        
  36.         // Creting an string array for field (table headers) names.
  37.         string[] fieldNames = new string[4];
  38.         fieldNames[0] = "name";
  39.         fieldNames[1] = "level";
  40.         fieldNames[2] = "health";
  41.         fieldNames[3] = "role";
  42.        
  43.         // Request for the table to be created on the cloud.
  44.         CloudConnectorCore.CreateTable(fieldNames, tableName, runtime);
  45.     }
  46.    
  47.     // Persist the example object in the cloud.
  48.     public static void SaveGandalf(bool runtime)
  49.     {
  50.         // Get the json string of the object.
  51.         string jsonPlayer = JsonUtility.ToJson(player);
  52.        
  53.         Debug.Log("<color=yellow>Sending following player to the cloud: \n</color>" + jsonPlayer);
  54.        
  55.         // Save the object on the cloud, in a table called like the object type.
  56.         CloudConnectorCore.CreateObject(jsonPlayer, tableName, runtime);
  57.     }
  58.    
  59.     // Update persisted objects fields on the cloud.
  60.     public static void UpdateGandalf(bool runtime)
  61.     {
  62.         Debug.Log("<color=yellow>Updating cloud data: player called Mithrandir will be level 100.</color>");
  63.        
  64.         // Look in the 'PlayerInfo' table, for an object of name 'Mithrandir', and set its level to 100.
  65.         CloudConnectorCore.UpdateObjects(tableName, "name", "Mithrandir", "level", "100", runtime);
  66.     }
  67.    
  68.     // Get a player object from the cloud.
  69.     public static void RetrieveGandalf(bool runtime)
  70.     {
  71.         Debug.Log("<color=yellow>Retrieving player of name Mithrandir from the Cloud.</color>");
  72.        
  73.         // Get any objects from table 'PlayerInfo' with value 'Mithrandir' in the field called 'name'.
  74.         CloudConnectorCore.GetObjectsByField(tableName, "name", "Mithrandir", runtime);
  75.     }
  76.    
  77.     // Get all player objects from the cloud.
  78.     public static void GetAllPlayers(bool runtime)
  79.     {
  80.         Debug.Log("<color=yellow>Retrieving all players from the Cloud.</color>");
  81.        
  82.         // Get all objects from table 'PlayerInfo'.
  83.         CloudConnectorCore.GetTable(tableName, runtime);
  84.     }
  85.    
  86.     // Get all data tables from the cloud.
  87.     public static void GetAllTables(bool runtime)
  88.     {
  89.         Debug.Log("<color=yellow>Retrieving all data tables from the Cloud.</color>");
  90.        
  91.         // Get all objects from table 'PlayerInfo'.
  92.         CloudConnectorCore.GetAllTables(runtime);
  93.     }
  94.    
  95.     // Parse data received from the cloud.
  96.     public static void ParseData(CloudConnectorCore.QueryType query, List<string> objTypeNames, List<string> jsonData)
  97.     {
  98.         for (int i = 0; i < objTypeNames.Count; i++)
  99.         {
  100.             Debug.Log("Data type/table: " + objTypeNames[i]);
  101.         }
  102.        
  103.         // First check the type of answer.
  104.         if (query == CloudConnectorCore.QueryType.getObjects)
  105.         {
  106.             // In the example we will use only the first, thus '[0]',
  107.             // but may return several objects depending the query parameters.
  108.            
  109.             // Check if the type is correct.
  110.             if (string.Compare(objTypeNames[0], tableName) == 0)
  111.             {
  112.                 // Parse from json to the desired object type.
  113.                 PlayerInfo[] players = GSFUJsonHelper.JsonArray<PlayerInfo>(jsonData[0]);
  114.                 player = players[0];
  115.                
  116.                 Debug.Log("<color=yellow>Object retrieved from the cloud and parsed: \n</color>" +
  117.                     "Name: " + player.name + "\n" +
  118.                     "Level: " + player.level + "\n" +
  119.                     "Health: " + player.health + "\n" +
  120.                     "Role: " + player.role + "\n");
  121.             }
  122.         }
  123.        
  124.         // First check the type of answer.
  125.         if (query == CloudConnectorCore.QueryType.getTable)
  126.         {
  127.             // Check if the type is correct.
  128.             if (string.Compare(objTypeNames[0], tableName) == 0)
  129.             {
  130.                 // Parse from json to the desired object type.
  131.                 PlayerInfo[] players = GSFUJsonHelper.JsonArray<PlayerInfo>(jsonData[0]);
  132.                
  133.                 string logMsg = "<color=yellow>" + players.Length.ToString() + " objects retrieved from the cloud and parsed:</color>";
  134.                 for (int i = 0; i < players.Length; i++)
  135.                 {
  136.                     logMsg += "\n" +
  137.                         "<color=blue>Name: " + players[i].name + "</color>\n" +
  138.                         "Level: " + players[i].level + "\n" +
  139.                         "Health: " + players[i].health + "\n" +
  140.                         "Role: " + players[i].role + "\n";             
  141.                 }
  142.                 Debug.Log(logMsg);
  143.             }
  144.         }
  145.        
  146.         // First check the type of answer.
  147.         if (query == CloudConnectorCore.QueryType.getAllTables)
  148.         {
  149.             // Just dump all content to the console, sorted by table name.
  150.             string logMsg = "<color=yellow>All data tables retrieved from the cloud.\n</color>";
  151.             for (int i = 0; i < objTypeNames.Count; i++)
  152.             {
  153.                 logMsg += "<color=blue>Table Name: " + objTypeNames[i] + "</color>\n"
  154.                     + jsonData[i] + "\n";
  155.             }
  156.             Debug.Log(logMsg);
  157.         }
  158.     }
  159. }
  160.  
  161. // Helper class: because UnityEngine.JsonUtility does not support deserializing an array...
  162. // http://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/
  163. public class GSFUJsonHelper
  164. {
  165.     public static T[] JsonArray<T>(string json)
  166.     {
  167.         string newJson = "{ \"array\": " + json + "}";
  168.         Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
  169.         return wrapper.array;
  170.     }
  171.  
  172.     [System.Serializable]
  173.     private class Wrapper<T>
  174.     {
  175.         public T[] array = new T[] { };
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement