Skip to content

Instantly share code, notes, and snippets.

@H0neyCake
Created March 27, 2021 13:29
Show Gist options
  • Save H0neyCake/f70c07ac7219fcef8a1e35a730074eb2 to your computer and use it in GitHub Desktop.
Save H0neyCake/f70c07ac7219fcef8a1e35a730074eb2 to your computer and use it in GitHub Desktop.
Load geo data
using System;
using UnityEngine;
using UnityEngine.UI;
[Serializable]
public class GeoCountry
{
public string status;
public string country;
}
public class GeoData : MonoBehaviour
{
private void Start()
{
var response = CreateFromJson(LoadDataExternal());
Debug.Log($"<color=green>Geo data load! {response.country}</color>");
}
private static GeoCountry CreateFromJson(string jsonString) => JsonUtility.FromJson<GeoCountry>(jsonString);
private static string LoadDataExternal()
{
var filename = "http://ip-api.com/json";
WWW reader = new WWW(filename);
while (!reader.isDone)
{
}
if (string.IsNullOrEmpty(reader.error))
Debug.Log("<color=green>Geo data load!</color>");
else
Debug.LogError("<color=red>Geo data ERROR!</color>");
var dataExternal = reader.text;
return dataExternal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment