Skip to content

Instantly share code, notes, and snippets.

@oismaelash
Last active December 15, 2017 01:16
Show Gist options
  • Save oismaelash/b7102dc100940ab3b570a0fea6d84b7b to your computer and use it in GitHub Desktop.
Save oismaelash/b7102dc100940ab3b570a0fea6d84b7b to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
public class GameManager : MonoBehaviour
{
[SerializeField]
public PlayerJson playerJson;
[SerializeField]
public PlayerInfo playerInfo;
[SerializeField]
public string gameDataProjectFilePath = "/data.json";
private void Start()
{
StartCoroutine(GetText());
}
[ContextMenu("ShowJsonPlayerJson")]
public void ShowJsonPlayerJson()
{
playerJson.SaveToString();
}
[ContextMenu("ShowJsonPlayerInfo")]
public void ShowJsonPlayerInfo()
{
playerInfo.SaveToString();
}
private IEnumerator GetText()
{
using (UnityWebRequest www = UnityWebRequest.Get("http://thegamingworld.info/"))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
// Show results as text
Debug.Log(www.downloadHandler.text);
string dataAsJson = www.downloadHandler.text;
string JSONToParse = "{\"values\":" + dataAsJson + "}";
Debug.Log(JSONToParse);
PlayerInfo auxPI = JsonUtility.FromJson<PlayerInfo>(JSONToParse);
print(auxPI.values[0].id);
print(auxPI.values[0].nome);
print(auxPI.values[0].senha);
}
}
}
//[ContextMenu("CreateObj PlayerInfo")]
//public void CreateObj()
//{
// string filePath = Application.dataPath + gameDataProjectFilePath;
// if (File.Exists(filePath))
// {
// string dataAsJson = File.ReadAllText(filePath);
// string JSONToParse = "{\"values\":" + dataAsJson + "}";
// Debug.Log(JSONToParse);
// PlayerInfo auxPI = JsonUtility.FromJson<PlayerInfo>(JSONToParse);
// foreach (PlayerJson item in auxPI.values.ToList())
// {
// Debug.Log(
// string.Format("{0}{1}{2}",
// item.id,
// item.nome,
// item.senha
// ));
// }
// }
//}
//[ContextMenu("CreateObj PlayerJson")]
//public void CreateObjJson()
//{
// string filePath = Application.dataPath + gameDataProjectFilePath;
// if (File.Exists(filePath))
// {
// string dataAsJson = File.ReadAllText(filePath);
// Debug.Log(dataAsJson);
// //PlayerJson pla = PlayerJson.CreateFromJSON(dataAsJson);
// PlayerJson auxPJ = JsonUtility.FromJson<PlayerJson>(dataAsJson);
// Debug.Log(
// string.Format("{0}{1}{2}",
// auxPJ.id,
// auxPJ.nome,
// auxPJ.senha
// ));
// }
//}
}
[System.Serializable]
public class PlayerInfo
{
public PlayerJson[] values;
public static PlayerJson CreateFromJSON(string jsonString)
{
Debug.Log(jsonString);
return JsonUtility.FromJson<PlayerJson>(jsonString);
}
public string SaveToString()
{
Debug.Log(JsonUtility.ToJson(this));
return JsonUtility.ToJson(this);
}
}
[System.Serializable]
public class PlayerJson
{
[SerializeField]
public string id;
[SerializeField]
public string nome;
[SerializeField]
public string senha;
public static PlayerJson CreateFromJSON(string jsonString)
{
Debug.Log(jsonString);
return JsonUtility.FromJson<PlayerJson>(jsonString);
}
public string SaveToString()
{
Debug.Log(JsonUtility.ToJson(this));
return JsonUtility.ToJson(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment