C# 什么是;SystemInfo.deviceUniqueIdentifier“;在Android版本中使用?

C# 什么是;SystemInfo.deviceUniqueIdentifier“;在Android版本中使用?,c#,android,unity3d,C#,Android,Unity3d,该文件记录了iOS/Windows版本中使用的标识符,但没有记录Android版本中使用的标识符。SystemInfo.deviceUniqueIdentifier在Android上使用什么标识符?我不明白为什么文档中没有提到这个 上次我检查时,Unity使用获取Android ID字符串,然后将其转换为字符串。如果这样做,则应使用与相同的值。这就是在我的设备上发生的事情 不幸的是,更多的事情都发生在幕后 Unity将其唯一标识符的详细信息记录在其应用程序上 1。使用context.getSys

该文件记录了iOS/Windows版本中使用的标识符,但没有记录Android版本中使用的标识符。
SystemInfo.deviceUniqueIdentifier
在Android上使用什么标识符?

我不明白为什么文档中没有提到这个

上次我检查时,Unity使用获取Android ID字符串,然后将其转换为字符串。如果这样做,则应使用与相同的值。这就是在我的设备上发生的事情

不幸的是,更多的事情都发生在幕后

Unity将其唯一标识符的详细信息记录在其应用程序上

1。使用
context.getSystemService(context.TElEPHONY\u SERVICE).getDeviceId()获取设备ID

2。如果#1失败,则使用
context.getContentResolver().getString(Secure.Android\u ID)获取Android ID

3。如果#2失败,获取Mac地址

4。将结果从#1#2#3(哪一个成功)转换为MD5哈希

这篇文章值得一读,因为在一些Unity版本中,行为有点不同

下面是他们提供的关于它的示例代码:

// Hash an input string and return the hash as
// a 32 character hexadecimal string.
static string getMd5Hash(string input)
{
    if (input == "")
        return "";
    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
    StringBuilder sBuilder = new StringBuilder();
    for (int i = 0; i < data.Length; i++)
        sBuilder.Append(data[i].ToString("x2"));
    return sBuilder.ToString();
}

static string generateDeviceUniqueIdentifier(bool oldBehavior)
{
    string id = "";
    AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaClass contextClass = new AndroidJavaClass("android.content.Context");
    string TELEPHONY_SERVICE = contextClass.GetStatic<string>("TELEPHONY_SERVICE");
    AndroidJavaObject telephonyService = activity.Call<AndroidJavaObject>("getSystemService", TELEPHONY_SERVICE);
    bool noPermission = false;
    try
    {
        id = telephonyService.Call<string>("getDeviceId");
    }
    catch (Exception e) {
        noPermission = true;
    }
    if(id == null)
        id = "";
    // <= 4.5 : If there was a permission problem, we would not read Android ID
    // >= 4.6 : If we had permission, we would not read Android ID, even if null or "" was returned
    if((noPermission && !oldBehavior) || (!noPermission && id == "" && oldBehavior))
    {
        AndroidJavaClass settingsSecure = new AndroidJavaClass("android.provider.Settings$Secure");
        string ANDROID_ID = settingsSecure.GetStatic<string>("ANDROID_ID");
        AndroidJavaObject contentResolver = activity.Call<AndroidJavaObject>("getContentResolver");
        id = settingsSecure.CallStatic<string>("getString", contentResolver, ANDROID_ID);
        if(id == null)
            id = "";
    }
    if(id == "")
    {
        string mac = "00000000000000000000000000000000";
        try
        {
            StreamReader reader = new StreamReader("/sys/class/net/wlan0/address");
            mac = reader.ReadLine();
            reader.Close();
        }
        catch (Exception e) {}
        id = mac.Replace(":", "");
    }
    return getMd5Hash(id);
}
//对输入字符串进行散列,并将散列返回为
//32个字符的十六进制字符串。
静态字符串getMd5Hash(字符串输入)
{
如果(输入==“”)
返回“”;
MD5CryptoServiceProvider md5Hasher=新的MD5CryptoServiceProvider();
byte[]data=md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sBuilder=新StringBuilder();
for(int i=0;i