unity Pc获取本机Mac地址

2023-12-13 03:49:41

?1.此方法只能获取众多Mac中的一个

    private static string GetMacAddress()
    {
        string physicalAddress = "";
 
        NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
 
        foreach (NetworkInterface adaper in nice)
        {
 
            Debug.Log(adaper.Description);
 
            if (adaper.Description == "en0")
            {
                physicalAddress = adaper.GetPhysicalAddress().ToString();
                break;
            }
            else
            {
                physicalAddress = adaper.GetPhysicalAddress().ToString();
 
                if (physicalAddress != "")
                {
                    break;
                };
            }
        }
 
        return physicalAddress;
    }

2.此处获取活跃的Mac:当前wifi使用的mac地址,如果想要以太网的mac? 将Wireless80211 改为Ethernet

public static string GetmacAddress()
    {
        string strphysicalAddress = "";
        var interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var iface in interfaces)
        {
            if (iface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && iface.OperationalStatus == OperationalStatus.Up)
            {
                var physicalAddress = iface.GetPhysicalAddress();
                var macAddress = physicalAddress.ToString();
                Debug.Log("Current MAC address: " + macAddress);
                strphysicalAddress = macAddress;
                break;
            }
            else
            {
                var physicalAddress = iface.GetPhysicalAddress();
                var macAddress = physicalAddress.ToString();
                Debug.Log("没网的 MAC address: " + macAddress);
            }
        }
       
        return strphysicalAddress;
    }

文章来源:https://blog.csdn.net/fanfan_hongyun/article/details/134936768
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。