最新文章
Cocos2d-x游戏开发实例详解7:对象释放时机
03-25 13:59
Cocos2d-x游戏开发实例详解6:自动释放池
03-25 13:55
Cocos2d-x游戏开发实例详解5:神奇的自动释放
03-25 13:49
Cocos2d-x游戏开发实例详解4:游戏主循环
03-25 13:44
Cocos2d-x游戏开发实例详解3:无限滚动地图
03-25 13:37
Cocos2d-x游戏开发实例详解2:开始菜单续
03-25 13:32
Unity获取安卓mac地址源码
2015年01月27日 09:45
0 点赞
0 评论
更新于 2025-11-21 15:31
下面为大家提供Unity获取安卓MAC地址的源码,供参考学习。若大家有更好、更快获取MAC地址的方法,欢迎分享。接下来,直接展示源码。
源码实现
1. 添加引用
首先,在项目中添加以下引用:
using System.Management;
using System.Management.Instrumentation;
2. 获取硬件信息的方法
获取CPU、硬盘和网卡信息
private void GetInfo()
{
// CPU序列号
string cpuInfo = "";
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
// 这里的Response.Write应该是在Web环境下使用,在Unity中可能需要替换为Debug.Log
// 示例:Debug.Log("cpu序列号:" + cpuInfo);
// Response.Write("cpu序列号:" + cpuInfo.ToString());
}
// 获取硬盘ID
String HDid;
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDid = (string)mo.Properties["Model"].Value;
// 这里的Response.Write应该是在Web环境下使用,在Unity中可能需要替换为Debug.Log
// 示例:Debug.Log("硬盘序列号:" + HDid);
// Response.Write("硬盘序列号:" + HDid.ToString());
}
// 获取网卡硬件地址
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if ((bool)mo["IPEnabled"] == true)
{
// 这里的Response.Write应该是在Web环境下使用,在Unity中可能需要替换为Debug.Log
// 示例:Debug.Log("MAC address\t" + mo["MacAddress"].ToString());
// Response.Write("MAC address\t{0}" + mo["MacAddress"].ToString());
}
mo.Dispose();
}
}
获取CPU使用率
public static float GetCPUPersent()
{
float cpuload = 0;
const string categoryname = "processor";
const string countername = "% processor time";
const string instancename = "_total";
PerformanceCounter pc = new PerformanceCounter(categoryname, countername, instancename);
int i = 10;
while (i > 0)
{
System.Threading.Thread.Sleep(1000); // 等待1秒
cpuload = pc.NextValue();
if (cpuload > 0)
{
break;
}
i--;
}
return cpuload;
}
获取磁盘空间信息
public static void GetDiskSpace(string path, out long DiskAll, out long DiskActive)
{
DiskAll = 0;
DiskActive = 0;
long a, b, c;
int aaa = GetDiskFreeSpaceEx(path, out a, out b, out c);
DiskAll = (long)(b / 1024 / 1024);
DiskActive = (long)(a / 1024 / 1024);
}
获取内存信息
public static void GetMemoryInfo(out uint MemoryAll, out uint MemoryUsed)
{
MemoryAll = 0;
MemoryUsed = 0;
MEMORY_INFO MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref MemInfo);
MemoryAll = MemInfo.dwTotalPhys / 1024 / 1024;
MemoryUsed = (MemInfo.dwTotalPhys - MemInfo.dwAvailPhys) / 1024 / 1024;
}
3. 外部调用函数和结构体定义
全局内存状态函数
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
// 定义内存的信息结构
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct MEMORY_INFO
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
获取磁盘可用空间函数
[System.Runtime.InteropServices.DllImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceExA")]
public static extern int GetDiskFreeSpaceEx(string lpRootPathName, out long lpFreeBytesAvailable, out long lpTotalNumberOfBytes, out long lpTotalNumberOfFreeBytes);
注意事项
- 代码中的
Response.Write通常用于Web环境,在Unity中需要替换为Debug.Log来输出信息。 - 此代码主要是在Windows环境下通过
System.Management命名空间获取硬件信息,在安卓环境下无法直接使用,需要使用安卓特定的API或者通过Unity的安卓插件来实现获取安卓设备的MAC地址。
以上就是Unity获取安卓MAC地址相关源码内容。若后续有更优的源码,会第一时间分享给大家。