Unity獲取PC端獲取本機Wifi網絡的IP

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


public class test : MonoBehaviour
{
    public Text tt01;
    // Start is called before the first frame update
    void Start()
    {

        GetIP();
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    /// <summary>
    /// 獲取本機IP
    /// </summary>
    /// <param name="Addfam">要獲取的IP類型</param>
    /// <returns></returns>
    public  string GetIP()
    {

        string output = "";

        foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
        {

            NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211;  //無線局域網適配器  


            if ((item.NetworkInterfaceType == _type1) && item.OperationalStatus == OperationalStatus.Up)

            {
                foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                {

                        if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            output = ip.Address.ToString();
                            tt01.text = output;
                           // Debug.Log("IP:" + output);
                        }       
                }
            }
        }
        return output;
    }

}

 

FR:徐海濤(hunk Xu)
QQ技術交流羣:386476712

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章