Created
March 27, 2021 13:29
-
-
Save H0neyCake/f70c07ac7219fcef8a1e35a730074eb2 to your computer and use it in GitHub Desktop.
Load geo data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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