Skip to content

Instantly share code, notes, and snippets.

@Raziel619
Created September 16, 2019 01:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Raziel619/2636dc4c6aaa7f7076432339fa1f8e62 to your computer and use it in GitHub Desktop.
Save Raziel619/2636dc4c6aaa7f7076432339fa1f8e62 to your computer and use it in GitHub Desktop.
Unity Function for getting External IP Address
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class GetExternalIPAddress : MonoBehaviour {
void Start()
{
StartCoroutine(GetIPAddress());
}
//Taken from Goodgulf
IEnumerator GetIPAddress()
{
UnityWebRequest www = UnityWebRequest.Get("http://checkip.dyndns.org");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
string result = www.downloadHandler.text;
// This results in a string similar to this: <html><head><title>Current IP Check</title></head><body>Current IP Address: 123.123.123.123</body></html>
// where 123.123.123.123 is your external IP Address.
// Debug.Log("" + result);
string[] a = result.Split(':'); // Split into two substrings -> one before : and one after.
string a2 = a[1].Substring(1); // Get the substring after the :
string[] a3 = a2.Split('<'); // Now split to the first HTML tag after the IP address.
string a4 = a3[0]; // Get the substring before the tag.
Debug.Log("External IP Address = "+a4);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment