Created
February 9, 2021 09:23
-
-
Save TobiasBerg/ff3309b22ac3d0d8a2753a52e399baf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IEnumerator UpdatePlayerStorage() | |
{ | |
Debug.Log("Updating player color with player storage"); | |
using (UnityWebRequest webRequest = UnityWebRequest.Post(LootLocker.baseUrl + "v1/player/storage", "")) | |
{ | |
// Set the header to authenticate the request, using our session token | |
webRequest.SetRequestHeader("x-session-token", this.sessionToken); | |
// Set header to let the server know we are sending JSON | |
webRequest.SetRequestHeader("Content-Type", "application/json"); | |
Color32 randomColor = new Color32( | |
Convert.ToByte(UnityEngine.Random.Range(0, 255)), | |
Convert.ToByte(UnityEngine.Random.Range(0, 255)), | |
Convert.ToByte(UnityEngine.Random.Range(0, 255)), | |
255 | |
); | |
string stringColor = randomColor.r + "," + randomColor.g + "," + randomColor.b; | |
// Create the payload (Ugly workaround because JsonUtility doesn't support top level JSON arrays) | |
string payload = "["; | |
payload = payload + new PlayerStorageItem("somethingElse", "a value", 4).ToJSON(); | |
payload = payload + ","; | |
payload = payload + new PlayerStorageItem("characterColor", stringColor, 11).ToJSON(); | |
payload = payload + "]"; | |
// Create an upload handler and turn the string into a byte array | |
UploadHandler uploader = new UploadHandlerRaw(Encoding.ASCII.GetBytes(payload)); | |
webRequest.uploadHandler = uploader; | |
// Send request and wait for the response | |
yield return webRequest.SendWebRequest(); | |
// Check if request was successful or not | |
if (webRequest.isNetworkError) | |
{ | |
Debug.Log("Error: " + webRequest.error); | |
} | |
else | |
{ | |
// Debug log the response from the server | |
Debug.Log(webRequest.downloadHandler.text); | |
HandlePlayerStorageResponse(webRequest.downloadHandler.text); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment