Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Created May 4, 2016 14:30
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save sandcastle/436ecb49c749942cb52adb2da169a2d4 to your computer and use it in GitHub Desktop.
Save sandcastle/436ecb49c749942cb52adb2da169a2d4 to your computer and use it in GitHub Desktop.
A Unity 3D behaviour for Geo location lookup via IP address
using UnityEngine;
using System.Collections;
using Newtonsoft.Json;
/// <summary>
/// The Geo data for a user.
///
/// http://ip-api.com/docs/api:json
///
/// <code>
/// {
/// "status": "success",
/// "country": "COUNTRY",
/// "countryCode": "COUNTRY CODE",
/// "region": "REGION CODE",
/// "regionName": "REGION NAME",
/// "city": "CITY",
/// "zip": "ZIP CODE",
/// "lat": LATITUDE,
/// "lon": LONGITUDE,
/// "timezone": "TIME ZONE",
/// "isp": "ISP NAME",
/// "org": "ORGANIZATION NAME",
/// "as": "AS NUMBER / NAME",
/// "query": "IP ADDRESS USED FOR QUERY"
/// }
/// </code>
///
/// </summary>
public class GeoData
{
/// <summary>
/// The status that is returned if the response was successful.
/// </summary>
public const string SuccessResult = "success";
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("query")]
public string IpAddress { get; set; }
}
public class GeoCountry : MonoBehaviour {
// Use this for initialization
void Start () {
HTTP.Request someRequest = new HTTP.Request( "get", "http://ip-api.com/json" );
someRequest.Send( ( request ) => {
// Get Geo data
GeoData data = null;
try {
data = request.response.Get<GeoData>();
}
catch (System.Exception ex) {
// TODO: Hook into an auto retry case
Debug.LogError("Could not get geo data: " + ex.ToString());
return;
}
// Ensure successful
if (data.Status != GeoData.SuccessResult) {
// TODO: Hook into an auto retry case
Debug.LogError("Unsuccessful geo data request: " + request.response.Text);
return;
}
Debug.Log ("User's Country: \"" + data.Country + "\"; Query: \"" + data.IpAddress + "\"");
});
}
// Update is called once per frame
void Update () {
}
}
@Zaidiii
Copy link

Zaidiii commented Jun 19, 2017

Hey! I have tried using your code but got error on "HTTP.Request someRequest = new HTTP.Request( "get", "http://ip-api.com/json" );" and its saying "error CS0234: The type or namespace name Http' does not exist in the namespace System.Net'. Are you missing an assembly reference?" I have tried to solve it by using "System.Net.Http" and added dll for it but failed.Please Help me in this regards. Thankyou

@ThunderboxEntertainment

Thanks! Very useful indeed.
I was able to use your code to make a PlayMaker action to detect the user's country!
Nice work.
:D

@IridiumStudios
Copy link

ThunderboxEntertainment: I'm having the same issue as Zaidiii. How did you solve it?

@jindraregal
Copy link

Http library is available since 4.6 .Net version. For older version you have to use old Unity way to request data: https://docs.unity3d.com/Manual/UnityWebRequest-RetrievingTextBinaryData.html

@sandcastle
Copy link
Author

sandcastle commented Jun 4, 2018

@Zaidiii / @ThunderboxEntertainment / @IridiumStudios / @regy42 - I have only just seen these comments..

I’m not actively developing in Unity any more, and I can't remember the library that was used for this sample.

I would suggest using a library like Best HTTP, this will make things work better cross-platform:
https://assetstore.unity.com/packages/tools/network/best-http-10872

I hear that net standard libraries are coming to Unity if that is the case the built-in HTTP ones might work, but don't take my word for it :)

@mrpacogp
Copy link

How many request we can make with our game to http://ip-api.com/json ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment