Created
September 16, 2019 01:35
-
-
Save Raziel619/2636dc4c6aaa7f7076432339fa1f8e62 to your computer and use it in GitHub Desktop.
Unity Function for getting External IP Address
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.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