Unity得到本机IP地址

直接上代码

using System.Collections;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using UnityEngine;

public class IPLocalManager : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Debug.LogError(GetIP());
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    private string GetIP()
    {
        NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface adater in adapters)
        {
            if (adater.Supports(NetworkInterfaceComponent.IPv4))
            {
                UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses;
                if (UniCast.Count > 0)
                {
                    foreach (UnicastIPAddressInformation uni in UniCast)
                    {
                        if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            Debug.Log(uni.Address.ToString());
                            return uni.Address.ToString();
                        }
                    }
                }
            }
        }
        return null;
    }
}

 

using System.Collections;using System.Collections.Generic;using System.Net.NetworkInformation;using System.Net.Sockets;using UnityEngine;
public class IPLocalManager : MonoBehaviour {
// Use this for initializationvoid Start () {        Debug.LogError(GetIP());}// Update is called once per framevoid Update () {}    private string GetIP()    {        NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();        foreach (NetworkInterface adater in adapters)        {            if (adater.Supports(NetworkInterfaceComponent.IPv4))            {                UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses;                if (UniCast.Count > 0)                {                    foreach (UnicastIPAddressInformation uni in UniCast)                    {                        if (uni.Address.AddressFamily == AddressFamily.InterNetwork)                        {                            Debug.Log(uni.Address.ToString());                            return uni.Address.ToString();                        }                    }                }            }        }        return null;    }}

 

posted @ 2019-11-06 14:53  WalkingSnail  阅读(689)  评论(0)    收藏  举报