Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GooglePlayGames;
- using UnityEngine;
- using UnityEngine.SocialPlatforms;
- using GooglePlayGames.BasicApi;
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.IO;
- namespace HutongGames.PlayMaker.Actions
- {
- [ActionCategory("Google Play")]
- [Tooltip("Load some data to the cloud")]
- public class LoadDataFromCloud : FsmStateAction
- {
- [UIHint(UIHint.Variable)]
- [Tooltip("Where the information that is loaded should go.")]
- [RequiredField]
- public FsmVar loadData;
- [Tooltip("The slot that you want to save the information to.")]
- [RequiredField]
- public FsmInt slot;
- [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.")]
- [RequiredField]
- public FsmBool resolveConflictsOnServer;
- [Tooltip("Event to call on Success")]
- [RequiredField]
- public FsmEvent OnSuccess;
- [Tooltip("Event to call on Error")]
- [RequiredField]
- public FsmEvent OnError;
- public override void OnEnter()
- {
- #if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID)
- try {
- SaveHelper helper = new SaveHelper (resolveConflictsOnServer.Value);
- helper.LoadState(slot.Value, OnFinish);
- }
- catch(Exception e) {
- Debug.LogError(e.ToString());
- Fsm.Event(OnError);
- Finish ();
- }
- #else
- Finish ();
- #endif
- }
- public void OnFinish(bool success, int slot, byte[] data) {
- if(success && data != null && data.Length > 0) {
- string stringData = Encoding.ASCII.GetString(data);
- switch(loadData.Type) {
- case VariableType.String:
- loadData.stringValue = stringData;
- break;
- case VariableType.Bool:
- loadData.boolValue = bool.Parse(stringData);
- break;
- case VariableType.Color:
- loadData.colorValue = ColorFromString(stringData);
- break;
- case VariableType.Float:
- loadData.floatValue = float.Parse(stringData);
- break;
- case VariableType.Int:
- loadData.intValue = int.Parse(stringData);
- break;
- case VariableType.Vector2:
- loadData.vector2Value = Vector2ToString(stringData);
- break;
- case VariableType.Vector3:
- loadData.vector3Value = Vector3ToString(stringData);
- break;
- default:
- Debug.LogError("Invalid Type Loaded");
- Fsm.Event(OnError);
- Finish ();
- return;
- break;
- }
- Fsm.Event(OnSuccess);
- }
- else {
- Fsm.Event(OnError);
- }
- Finish ();
- }
- public Color ColorFromString(string color) {
- if(color == "") return Color.white;
- color = Regex.Replace(color, "[^0-9,.]", "");
- var strings = color.Split(","[0] );
- Color output = Color.black;
- for (var i = 0; i < 4; i++) {
- output[i] = System.Single.Parse(strings[i]);
- }
- return output;
- }
- public Vector2 Vector2ToString(string vector) {
- if(vector == "") return Vector2.zero;
- vector = Regex.Replace(vector, "[^0-9,.]", "");
- var strings = vector.Split(","[0] );
- Vector2 output = Vector2.zero;
- for (var i = 0; i < 2; i++) {
- output[i] = System.Single.Parse(strings[i]);
- }
- return output;
- }
- public Vector3 Vector3ToString(string vector) {
- if(vector == "") return Vector2.zero;
- vector = Regex.Replace(vector, "[^0-9,.]", "");
- var strings = vector.Split(","[0] );
- Vector3 output = Vector3.zero;
- for (var i = 0; i < 3; i++) {
- output[i] = System.Single.Parse(strings[i]);
- }
- return output;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement