Created
February 9, 2021 09:22
-
-
Save TobiasBerg/5fb30a8b952c44219de3431a37f48313 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 GetPlayerStorage() | |
{ | |
Debug.Log("Getting player storage"); | |
using (UnityWebRequest webRequest = UnityWebRequest.Get(LootLocker.baseUrl + "v1/player/storage")) | |
{ | |
// Set the header to authenticate the request | |
webRequest.SetRequestHeader("x-session-token", this.sessionToken); | |
// 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); | |
} | |
} | |
} | |
void HandlePlayerStorageResponse(string response) | |
{ | |
// Parse the response | |
PlayerStorageResponse psr = PlayerStorageResponse.ParseSuccessfulPlayerStorageResponse(response); | |
foreach (PlayerStorageItem item in psr.payload) | |
{ | |
// When we get a key named characterColor, use the value to change the color of the character | |
if (item.key == "characterColor") | |
{ | |
string[] rgb = item.value.Split(','); | |
// Convert from 0-255 to 0.0-1.0 range | |
byte red = Convert.ToByte(rgb[0]); | |
byte green = Convert.ToByte(rgb[1]); | |
byte blue = Convert.ToByte(rgb[2]); | |
Color characterColor = new Color32(red, green, blue, 255); | |
playerController.spriteRenderer.color = characterColor; | |
} | |
} | |
} | |
[System.Serializable] | |
public class PlayerStorageResponse | |
{ | |
public bool success; | |
public List<PlayerStorageItem> payload; | |
public static PlayerStorageResponse ParseSuccessfulPlayerStorageResponse(string jsonString) | |
{ | |
return JsonUtility.FromJson<PlayerStorageResponse>(jsonString); | |
} | |
} | |
[System.Serializable] | |
public class PlayerStorageItem | |
{ | |
public string key; | |
public string value; | |
public int order; | |
public PlayerStorageItem(string key, string value, int order) | |
{ | |
this.key = key; | |
this.value = value; | |
this.order = order; | |
} | |
public string ToJSON() | |
{ | |
return JsonUtility.ToJson(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment