Unity's solution to the problem of obtaining a unique identifier

Environment: only for PC

One: the pit encountered

    Unity provided me with a set of API links to obtain hardware information  . A post on Manniu: Link

    The article mentions a unique identifier to obtain a device


    But I can tell you that this value will change

    View api documentation

    

    It is obtained by encryption or other algorithms based on the motherboard serial number, BIOS serial number, cpu information, hard disk serial number, and operating system information. Perhaps something will change over time and the entire obtained identifier will change. Of (does not work GG)

Two: The second method is still a pit

    Since I can't get the identifier through SystemInfo, I wonder if I can get it with the C# related api, so I found a post: link

    Then copy the code in Unity. . . . BUT~ I can’t get it. At first I thought it was wrong to add System.Management.

    Because the added is under unity, I thought it was castrated by unity, so I can't get it

    

Then I found the entire dll in the operating system folder, and finally found that it was still wrong, and an error display


I guess it may be that the mono virtual machine does not support it. What should I do?

Three: Solution

    First, the whole solution is a bit unpleasant. . . .

    Although unity can't, we can use c# to get it. So I wrote a console program to get the cpu serial number and everything in it, and then found that it was successful

    (Again confirmed that unity mono cannot be obtained)

    ojxk, and then I wrote the obtained into a text, the following code:

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("start getserialnumber!!!");
            //保存到当前路径
            File.WriteAllText("pwd.txt", GetCpuID()+ GetDiskID());
            Console.WriteLine("write end!!!");
            Console.WriteLine(".......");
            Console.WriteLine(".......");
        }

        /// <summary>
        /// 获取cpu硬件信息
        /// </summary>
        /// <returns></returns>
        private static string GetCpuID()
        {
            try
            {
                string cpuInfo = "";//cpu序列号 
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        /// <summary>
        /// 获取硬盘ID 
        /// </summary>
        /// <returns></returns>
        private static string GetDiskID()
        {
            try
            {
                String HDid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

    }    

Then take out the exe compiled by this console program

Then put the entire exe into the StreamingAsstens folder of unity

The idea that comes to mind is to run the entire console program when Unity starts and then read the content in the generated txt as a unique identifier

(I will explain the problem here. Originally, I put the console program under the StreamingAsstes folder. I thought that running the program can directly produce a txt file under StreamingAssets. I found that it didn’t work. I tried several paths. Finally, I found that it will all be produced. (Below the path of the exe folder at the same level after publishing)

Code:

using UnityEngine;
using sysDia = System.Diagnostics;
	void Start()
	{
		string id = PlayerPrefs.GetString("xxxxx");
        if (string.IsNullOrEmpty(id))
        {
            Debug.Log("PlayerPrefs没有id,启动GetSerialNumber程序");
            sysDia.Process.Start(Application.streamingAssetsPath + "/GetSerialNumber.exe");
            StartCoroutine(IEConfigPwd());
            return;
        }
		else
		{
			
			
		}
		
	}
    private IEnumerator IEConfigPwd()
    {
        string pwdPath = "pwd.txt";
        Debug.Log("检测正在获取写入");
        //当有这个文档的时候
        yield return new WaitUntil(() => File.Exists(pwdPath));

        Debug.Log("写入成功");
        Debug.Log("开始读取");
        string pwdstr = File.ReadAllText(pwdPath);
		bool havaID=false;
       //这里自己做验证
	   //。。。。。
	   //。。。。。
        if (haveID)
        {
            PlayerPrefs.SetString("xxxxx", pwdstr);
            Debug.Log("获取成功!!!!!");
            File.Delete(pwdPath);
           
        }
        else
        {
            Debug.Log("设备类型不一致!!!!!");
            QuitExe();
        }

    }


This is probably the idea. I wrote it into the registry here to facilitate the next startup. I can get the entire value directly from the registry. There is no need to start the console program again. If there are other good methods, remember to tell me that I am also a technology. This is the only way to do it~

Guess you like

Origin blog.csdn.net/K20132014/article/details/79977444