I wonder what code i can use to get the IP of the player in my Unity game for mobile (IOS and Android) in the Unity 2022.2.0b1 using C#.
Have been looking up here on other questions, but they seems outdated.
CodePudding user response:
I've found the solution for my issue, if anyone else has this issue in Unity 2022 this code can be used to obtain IP of the player
public string getIP()
{
string userIP = new WebClient().DownloadString("https://icanhazip.com/");
return userIP;
}
CodePudding user response:
using System.Net;
using System.Net.Sockets;
using UnityEngine;
public class DeviceIPManager : MonoBehaviour
{
public string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new System.Exception("No network adapters with an IPv4 address in the system!");
}
}