Script updates.

This commit is contained in:
shadow8t4 2018-04-18 18:15:11 -05:00
parent 6bc5863f80
commit 36fd467819
6 changed files with 165 additions and 10 deletions

View file

@ -0,0 +1,12 @@
using System;
namespace AssemblyCSharp
{
public class EmptyClass
{
public EmptyClass ()
{
}
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 9ccd5f892b119d749980bbde89a80e70
timeCreated: 1524075474
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,35 @@
using UnityEngine;
using System.Collections;
public class PlayerInfo
{
public string username = "[{\"username\":\"test\", \"user_id\": 1}]";
public int user_id = 0;
public PlayerInfo()
{
username = "";
user_id = 0;
}
public void setUsername(string user)
{
username = user;
}
public string getUsername()
{
return username;
}
public void setUserId(int id)
{
user_id = id;
}
public int getUserId()
{
return user_id;
}
}

View file

@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: db6fec37a2679474e90cb02700d9533e
timeCreated: 1524075765
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View file

@ -1,11 +1,21 @@
using UnityEngine; using UnityEngine;
using UnityEngine.Networking; using UnityEngine.Networking;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI; using UnityEngine.UI;
/*
* 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.
*
* */
public class GetData : MonoBehaviour public class GetData : MonoBehaviour
{ {
//string displayText = "";
Text objectText; Text objectText;
void Start() void Start()
@ -13,16 +23,14 @@ public class GetData : MonoBehaviour
objectText = GetComponentInParent<Text> (); objectText = GetComponentInParent<Text> ();
} }
public void DisplayStuff() public void DisplayStuff(string request)
{ {
StartCoroutine (MakeRequest ()); StartCoroutine (MakeRequest (request));
//Debug.Log (displayText);
//objectText.text = displayText;
} }
IEnumerator MakeRequest() IEnumerator MakeRequest(string request)
{ {
using (UnityWebRequest www = UnityWebRequest.Get("https://corder.tech/mocha/users/1")) using (UnityWebRequest www = UnityWebRequest.Get(request))
{ {
yield return www.SendWebRequest(); yield return www.SendWebRequest();
@ -31,14 +39,88 @@ public class GetData : MonoBehaviour
Debug.Log(www.error); Debug.Log(www.error);
} }
else else
{ {
//this should be saved somewhere else...
List<PlayerInfo> players;
// Show results as text // Show results as text
//Debug.Log(www.downloadHandler.text); players = ParseJsonRaw(www.downloadHandler.text);
objectText.text = www.downloadHandler.text; objectText.text = DisplayInfo (players);
// Or retrieve results as binary data // Or retrieve results as binary data
byte[] results = www.downloadHandler.data; byte[] results = www.downloadHandler.data;
} }
} }
} }
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;
}
} }