2018-04-17 14:16:10 -05:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
using System.Collections;
|
2018-04-18 18:15:11 -05:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-17 15:13:19 -05:00
|
|
|
|
using UnityEngine.UI;
|
2018-04-17 14:16:10 -05:00
|
|
|
|
|
2018-04-18 18:15:11 -05:00
|
|
|
|
/*
|
|
|
|
|
* USAGE
|
|
|
|
|
*
|
|
|
|
|
* Just put this script into a button that needs
|
|
|
|
|
* to execute the HTTP request, and in the text field
|
|
|
|
|
* write the exact URL needed for the request.
|
|
|
|
|
*
|
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
|
2018-04-17 15:13:19 -05:00
|
|
|
|
public class GetData : MonoBehaviour
|
2018-04-17 14:16:10 -05:00
|
|
|
|
{
|
2018-04-17 15:13:19 -05:00
|
|
|
|
Text objectText;
|
|
|
|
|
|
2018-04-17 14:16:10 -05:00
|
|
|
|
void Start()
|
|
|
|
|
{
|
2018-04-17 15:13:19 -05:00
|
|
|
|
objectText = GetComponentInParent<Text> ();
|
2018-04-17 14:16:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 18:15:11 -05:00
|
|
|
|
public void DisplayStuff(string request)
|
2018-04-17 15:13:19 -05:00
|
|
|
|
{
|
2018-04-18 18:15:11 -05:00
|
|
|
|
StartCoroutine (MakeRequest (request));
|
2018-04-17 15:13:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-18 18:15:11 -05:00
|
|
|
|
IEnumerator MakeRequest(string request)
|
2018-04-17 14:16:10 -05:00
|
|
|
|
{
|
2018-04-18 18:15:11 -05:00
|
|
|
|
using (UnityWebRequest www = UnityWebRequest.Get(request))
|
2018-04-17 14:16:10 -05:00
|
|
|
|
{
|
2018-04-17 15:13:19 -05:00
|
|
|
|
yield return www.SendWebRequest();
|
2018-04-17 14:16:10 -05:00
|
|
|
|
|
|
|
|
|
if (www.isNetworkError || www.isHttpError)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log(www.error);
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-04-18 18:15:11 -05:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//this should be saved somewhere else...
|
|
|
|
|
List<PlayerInfo> players;
|
|
|
|
|
|
2018-04-17 14:16:10 -05:00
|
|
|
|
// Show results as text
|
2018-04-18 18:15:11 -05:00
|
|
|
|
players = ParseJsonRaw(www.downloadHandler.text);
|
|
|
|
|
objectText.text = DisplayInfo (players);
|
2018-04-17 14:16:10 -05:00
|
|
|
|
|
|
|
|
|
// Or retrieve results as binary data
|
|
|
|
|
byte[] results = www.downloadHandler.data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-18 18:15:11 -05:00
|
|
|
|
|
|
|
|
|
public List<PlayerInfo> ParseJsonRaw(string json)
|
|
|
|
|
{
|
|
|
|
|
List<PlayerInfo> players = new List<PlayerInfo>();
|
|
|
|
|
string tempjson = "";
|
|
|
|
|
|
|
|
|
|
bool ignorequotes = false;
|
|
|
|
|
bool specialchar = false;
|
|
|
|
|
bool insideobject = false;
|
|
|
|
|
|
|
|
|
|
foreach (char c in json)
|
|
|
|
|
{
|
|
|
|
|
if (insideobject)
|
|
|
|
|
{
|
|
|
|
|
tempjson += c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c == '\"')
|
|
|
|
|
{
|
|
|
|
|
if (!specialchar)
|
|
|
|
|
{
|
|
|
|
|
ignorequotes = !ignorequotes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(specialchar)
|
|
|
|
|
{
|
|
|
|
|
specialchar = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(c == '\\')
|
|
|
|
|
{
|
|
|
|
|
specialchar = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!ignorequotes)
|
|
|
|
|
{
|
|
|
|
|
if (c == '{')
|
|
|
|
|
{
|
|
|
|
|
insideobject = true;
|
|
|
|
|
tempjson += c;
|
|
|
|
|
}
|
|
|
|
|
else if (c == '}')
|
|
|
|
|
{
|
|
|
|
|
insideobject = false;
|
|
|
|
|
players.Add(ParseJson(tempjson));
|
|
|
|
|
tempjson = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return players;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PlayerInfo ParseJson(string json)
|
|
|
|
|
{
|
|
|
|
|
return JsonUtility.FromJson<PlayerInfo>(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string DisplayInfo(List<PlayerInfo> p)
|
|
|
|
|
{
|
|
|
|
|
string output = "";
|
|
|
|
|
|
|
|
|
|
foreach (PlayerInfo pi in p)
|
|
|
|
|
{
|
|
|
|
|
output += "Player ID: " + pi.user_id + "\t\tUser: " + pi.username + "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
2018-04-17 14:16:10 -05:00
|
|
|
|
}
|