首页 > 解决方案 > 如何获得与 UnityEngine.SystemInfo.deviceUniqueIdentifier 中相同的硬件 ID?

问题描述

我想在 Windows 窗体应用程序中获得UnityEngine.SystemInfo.deviceUniqueIdentifier与我在游戏中使用时获得的相同的硬件 ID。

如何获得相同的硬件 ID?

标签: c#unity3d

解决方案


I know I'm late but Unity's SystemInfo::deviceUniqueIdentifier is built like this:

Requires: using System.Management; in System.Management.dll (.NET Framework)

private string GetDeviceUniqueIdentifier() {
        string ret = string.Empty;

        string concatStr = string.Empty;
        try {
            using ManagementObjectSearcher searcherBb = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
            foreach (var obj in searcherBb.Get()) {
                concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
            }

            using ManagementObjectSearcher searcherBios = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
            foreach (var obj in searcherBios.Get()) {
                concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
            }

            using ManagementObjectSearcher searcherOs = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
            foreach (var obj in searcherOs.Get()) {
                concatStr += (string)obj.Properties["SerialNumber"].Value ?? string.Empty;
            }

            using var sha1 = SHA1.Create();
            ret = string.Join("", sha1.ComputeHash(Encoding.UTF8.GetBytes(concatStr)).Select(b => b.ToString("x2")));
        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }

        return ret;
    }

I reverse engineered the Unity Editor to find out what it actually queries in the WMI. This is obviously the Windows Implementation.


推荐阅读