Advertisement
Guest User

Untitled

a guest
Mar 9th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.53 KB | None | 0 0
  1. using GooglePlayGames;
  2. using UnityEngine;
  3. using UnityEngine.SocialPlatforms;
  4. using GooglePlayGames.BasicApi;
  5. using System;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.IO;
  9.  
  10. namespace HutongGames.PlayMaker.Actions
  11. {
  12.     [ActionCategory("Google Play")]
  13.     [Tooltip("Load some data to the cloud")]
  14.     public class LoadDataFromCloud : FsmStateAction
  15.     {
  16.         [UIHint(UIHint.Variable)]
  17.         [Tooltip("Where the information that is loaded should go.")]
  18.         [RequiredField]
  19.         public FsmVar loadData;
  20.  
  21.         [Tooltip("The slot that you want to save the information to.")]
  22.         [RequiredField]
  23.         public FsmInt slot;
  24.  
  25.         [Tooltip("A conflict happens when a device attempts to save state to the cloud but the data currently on the cloud was written by a different device.")]
  26.         [RequiredField]
  27.         public FsmBool resolveConflictsOnServer;
  28.  
  29.         [Tooltip("Event to call on Success")]
  30.         [RequiredField]
  31.         public FsmEvent OnSuccess;
  32.  
  33.         [Tooltip("Event to call on Error")]
  34.         [RequiredField]
  35.         public FsmEvent OnError;
  36.  
  37.         public override void OnEnter()
  38.         {
  39. #if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID)
  40.             try {
  41.                 SaveHelper helper = new SaveHelper (resolveConflictsOnServer.Value);
  42.                 helper.LoadState(slot.Value, OnFinish);
  43.             }
  44.             catch(Exception e) {
  45.                 Debug.LogError(e.ToString());
  46.                 Fsm.Event(OnError);
  47.                 Finish ();
  48.             }
  49. #else
  50.             Finish ();
  51. #endif
  52.         }
  53.  
  54.         public void OnFinish(bool success, int slot, byte[] data) {
  55.  
  56.             if(success && data != null && data.Length > 0) {
  57.  
  58.                 string stringData = Encoding.ASCII.GetString(data);
  59.                 switch(loadData.Type) {
  60.                     case VariableType.String:
  61.                         loadData.stringValue = stringData;
  62.                     break;
  63.  
  64.                     case VariableType.Bool:
  65.                         loadData.boolValue = bool.Parse(stringData);
  66.                     break;
  67.  
  68.                     case VariableType.Color:
  69.                         loadData.colorValue = ColorFromString(stringData);
  70.                     break;
  71.  
  72.                     case VariableType.Float:
  73.                         loadData.floatValue = float.Parse(stringData);
  74.                     break;
  75.  
  76.                     case VariableType.Int:
  77.                         loadData.intValue = int.Parse(stringData);
  78.                     break;
  79.  
  80.                     case VariableType.Vector2:
  81.                         loadData.vector2Value = Vector2ToString(stringData);
  82.                     break;
  83.  
  84.                     case VariableType.Vector3:
  85.                         loadData.vector3Value = Vector3ToString(stringData);
  86.                     break;
  87.  
  88.                     default:
  89.                         Debug.LogError("Invalid Type Loaded");
  90.                         Fsm.Event(OnError);
  91.                         Finish ();
  92.                         return;
  93.                     break;
  94.                 }
  95.  
  96.                 Fsm.Event(OnSuccess);
  97.             }
  98.             else {
  99.                 Fsm.Event(OnError);
  100.             }
  101.  
  102.             Finish ();
  103.         }
  104.  
  105.         public Color ColorFromString(string color) {
  106.            
  107.             if(color == "") return Color.white;
  108.            
  109.             color = Regex.Replace(color, "[^0-9,.]", "");
  110.             var strings = color.Split(","[0] );
  111.            
  112.             Color output = Color.black;
  113.             for (var i = 0; i < 4; i++) {
  114.                 output[i] = System.Single.Parse(strings[i]);
  115.             }
  116.            
  117.             return output;
  118.         }
  119.  
  120.         public Vector2 Vector2ToString(string vector) {
  121.            
  122.             if(vector == "") return Vector2.zero;
  123.            
  124.             vector = Regex.Replace(vector, "[^0-9,.]", "");
  125.             var strings = vector.Split(","[0] );
  126.            
  127.             Vector2 output = Vector2.zero;
  128.             for (var i = 0; i < 2; i++) {
  129.                 output[i] = System.Single.Parse(strings[i]);
  130.             }
  131.            
  132.             return output;
  133.         }
  134.  
  135.         public Vector3 Vector3ToString(string vector) {
  136.            
  137.             if(vector == "") return Vector2.zero;
  138.            
  139.             vector = Regex.Replace(vector, "[^0-9,.]", "");
  140.             var strings = vector.Split(","[0] );
  141.            
  142.             Vector3 output = Vector3.zero;
  143.             for (var i = 0; i < 3; i++) {
  144.                 output[i] = System.Single.Parse(strings[i]);
  145.             }
  146.            
  147.             return output;
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement