Unity中获取本机Mac地址,常用于类似绑定设备的加密情况中,限制程序只可以在固定的设备上使用。首先我们需要在脚本中引用System.Net.NetworkInformation程序集,然后使用NetworkInterface.GetAllNetworkInterfaces()可以获取所有的网络相关信息,其中GetPhysicalAddress()方法可以拿到设备的物理地址即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;
}