简体   繁体   中英

Unity 2022.2.0b1 get IP of device

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.

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;
}
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!");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM