- Home /
Reading JSON response from Google Sheets in unity ( most places have WWW.text)
As the title says most of the tutorials im seeing use WWW or www.text, i want to simply have a application use HTTP to manipulate sheets in google. I have a the API KEY but need to understand how to fire it off at a sheet it to auth and let me manipulate the sheet.
I did try asking unity to query without a key it gave me a 404 error or when i use downloadhandler.data it throws up (i know this because its json) need to understand how to have the json data come back at me in the console for learning purposes.
Before you guys tell me about plugins and tools on github, they are all amazing and work but im developing for a magicleap (lumin) so most of these rely on a node.js libraries which dont exist on the leaps CPU. so im going with simple HTTP GET / PUT with an API KEY just need to know how to go about it with the new UnityWebRequest system.
Thank you :)
Answer by rh_galaxy · Aug 06, 2019 at 04:04 PM
This should give you an idea of how to use UnityWebRequest. Untested code but comes from a working script.
const string WEB_HOST = "http://www.url.com/";
UnityWebRequest www;
public bool isDone = false;
public IEnumerator GetSomeUrl()
{
isDone = false;
string url = WEB_HOST + "index.php"; //compose url to whatever
www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
//retrieve result as text
//use www.downloadHandler.text and do something
//...
}
isDone = true;
}
int state = 0;
void Update()
{
if(state==0)
{
StartCoroutine(GetSomeUrl());
//set in the above, but since StartCoroutine returns before it has a chance
// to run we need to set it
isDone = false;
state++;
}
if(state==1)
{
if(isDone)
{
//url load completed or has failed...
//...
}
}
}