Newer
Older
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
Marco Zimmer
committed
public class GlobalBehaviour : MonoBehaviour, ISerializationCallbackReceiver
Marco Zimmer
committed
{
public static GlobalBehaviour Instance
{
get => _Instance;
set
{
_Instance = value;
else
Destroy(value);
}
}
private static GlobalBehaviour _Instance;
Marco Zimmer
committed
//Make sure when using RGBA-Colors, the A-value of animationStartColor
//and animationEndColor is the same OR try with value = 255
public static Color hintAnimationStartColor;
public static Color hintAnimationEndColor;
public static float hintAnimationDuration;
public static Color StageAccomplished;
public static Color StageNotYetAccomplished;
public static Color StageError;
public static float GadgetLaserDistance;
public static float GadgetPhysicalDistance;
#region Unity Serialization
[SerializeField] private Color _hintAnimationStartColor;
[SerializeField] private Color _hintAnimationEndColor;
[SerializeField] private float _hintAnimationDuration;
[SerializeField] private Color _StageAccomplished;
[SerializeField] private Color _StageNotYetAccomplished;
[SerializeField] private Color _StageError;
[SerializeField] private float _GadgetLaserDistance = 30f;
[SerializeField] private float _GadgetPhysicalDistance = 2.5f;
void ISerializationCallbackReceiver.OnAfterDeserialize()
Marco Zimmer
committed
{
hintAnimationStartColor = _hintAnimationStartColor;
hintAnimationEndColor = _hintAnimationEndColor;
hintAnimationDuration = _hintAnimationDuration;
StageAccomplished = _StageAccomplished;
StageNotYetAccomplished = _StageNotYetAccomplished;
StageError = _StageError;
GadgetLaserDistance = _GadgetLaserDistance;
GadgetPhysicalDistance = _GadgetPhysicalDistance;
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
_hintAnimationStartColor = hintAnimationStartColor;
_hintAnimationEndColor = hintAnimationEndColor;
_hintAnimationDuration = hintAnimationDuration;
_StageAccomplished = StageAccomplished;
_StageNotYetAccomplished = StageNotYetAccomplished;
_StageError = StageError;
_GadgetLaserDistance = GadgetLaserDistance;
_GadgetPhysicalDistance = GadgetPhysicalDistance;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
DontDestroyOnLoad(this);
}
private void Start()
{
PostServerConnection();
}
//TODO: Move where appropiate
public int tryScrollListTimes = 2;
static public List<Scroll> AvailableScrolls;
private void PostServerConnection()
{
StartCoroutine(getScrollsfromServer());
IEnumerator getScrollsfromServer()
{
//Try /scroll/listAll endpoint when scroll/list is not working
UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
//Postman-Echo-Mock
//UnityWebRequest request = UnityWebRequest.Get("https://019a8ea5-843a-498b-8d0c-778669aef987.mock.pstmn.io/get");
for (int i = 0; i < this.tryScrollListTimes; i++)
{
request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
request.method = UnityWebRequest.kHttpVerbGET;
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ConnectionError
|| request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogWarning(request.error);
Debug.Log("GET Scroll/list failed. Attempt: " + (i + 1).ToString());
}
else
break;
}
string jsonString = null;
if (request.result == UnityWebRequest.Result.ConnectionError
|| request.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogWarning(request.error);
}
else
{
CommunicationEvents.ServerRunning = true;
jsonString = request.downloadHandler.text;
}
if (string.IsNullOrEmpty(jsonString)
|| jsonString.Equals("[]"))
{
jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json");
Debug.Log("Using Fallback Scrolls: \n" + jsonString);
}
System.DateTime startTime = System.DateTime.UtcNow;
AvailableScrolls = JsonConvert.DeserializeObject<List<Scroll>>(jsonString);
Debug.Log("Scroll Parsing in: " + (System.DateTime.UtcNow - startTime).TotalMilliseconds + "ms");
}