Script updates.
This commit is contained in:
parent
6bc5863f80
commit
36fd467819
6 changed files with 165 additions and 10 deletions
12
MoCha/Assets/EmptyClass.cs
Normal file
12
MoCha/Assets/EmptyClass.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
|
||||
namespace AssemblyCSharp
|
||||
{
|
||||
public class EmptyClass
|
||||
{
|
||||
public EmptyClass ()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
MoCha/Assets/EmptyClass.cs.meta
Normal file
13
MoCha/Assets/EmptyClass.cs.meta
Normal 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:
|
35
MoCha/Assets/PlayerInfo.cs
Normal file
35
MoCha/Assets/PlayerInfo.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
|
13
MoCha/Assets/PlayerInfo.cs.meta
Normal file
13
MoCha/Assets/PlayerInfo.cs.meta
Normal 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.
|
@ -1,11 +1,21 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
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
|
||||
{
|
||||
//string displayText = "";
|
||||
Text objectText;
|
||||
|
||||
void Start()
|
||||
|
@ -13,16 +23,14 @@ public class GetData : MonoBehaviour
|
|||
objectText = GetComponentInParent<Text> ();
|
||||
}
|
||||
|
||||
public void DisplayStuff()
|
||||
public void DisplayStuff(string request)
|
||||
{
|
||||
StartCoroutine (MakeRequest ());
|
||||
//Debug.Log (displayText);
|
||||
//objectText.text = displayText;
|
||||
StartCoroutine (MakeRequest (request));
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
|
@ -32,13 +40,87 @@ public class GetData : MonoBehaviour
|
|||
}
|
||||
else
|
||||
{
|
||||
|
||||
//this should be saved somewhere else...
|
||||
List<PlayerInfo> players;
|
||||
|
||||
// Show results as text
|
||||
//Debug.Log(www.downloadHandler.text);
|
||||
objectText.text = www.downloadHandler.text;
|
||||
players = ParseJsonRaw(www.downloadHandler.text);
|
||||
objectText.text = DisplayInfo (players);
|
||||
|
||||
// Or retrieve results as binary 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;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue