Unity – How to get local IP address
In Unity 2018 getting the player local IP address has changed. Previously we could use the UnityEngine.Network class, but it has become obsolete.
string ipAddress = UnityEngine.Network.player.ipaddress; // obsolete
After doing some research I found the C# way of getting the local IP address of the user:
public static class IPManager { public static string GetLocalIPAddress() { var host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { return ip.ToString(); } } throw new System.Exception("No network adapters with an IPv4 address in the system!"); } }
This can be used to start a local server or direct connect to one.
Before trying to get the IP, you can also check if you are connected with the following line:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
Sources
Thanks to Mrchief: https://stackoverflow.com/questions/6803073/get-local-ip-address