question

Andrew Maksimov avatar image
Andrew Maksimov asked

DeviceUniqueIdentifier in Unity SDK

I was faced with the fact that LoginWithCustomID does not work under WebGL (all connected users are authorized on one account).

And also with the fact that the client running in the editor has the same deviceId as the build client (it's a trifle, but still not convenient, since network games usually debug in conjunction with several running clients).

To fix this situation, I made the following changes to the DeviceUniqueIdentifier function.

public static string DeviceUniqueIdentifier
        {
            get
            {
                var deviceId = "";


#if UNITY_EDITOR
                deviceId = SystemInfo.deviceUniqueIdentifier + "-editor";
#elif UNITY_ANDROID
                AndroidJavaClass up = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
                AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject> ("currentActivity");
                AndroidJavaObject contentResolver = currentActivity.Call<AndroidJavaObject> ("getContentResolver");
                AndroidJavaClass secure = new AndroidJavaClass ("android.provider.Settings$Secure");
                deviceId = secure.CallStatic<string> ("getString", contentResolver, "android_id");
#elif UNITY_WEBGL
                if (!PlayerPrefs.HasKey("UniqueIdentifier"))
                    PlayerPrefs.SetString("UniqueIdentifier", Guid.NewGuid().ToString());
                deviceId = PlayerPrefs.GetString("UniqueIdentifier");
#else
                deviceId = SystemInfo.deviceUniqueIdentifier;
#endif
                return deviceId;
            }
        }


Is this a correct practice? Or is there a more competent solution?
unity3dsdksAuthentication
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tuomas Karmakallio avatar image Tuomas Karmakallio commented ·

Great solution! Thank you!

0 Likes 0 ·
brendan avatar image
brendan answered

Yes, the recommendation would be to simply create a new random GUID for the local client (also discussed here: https://community.playfab.com/questions/16596/how-to-get-unique-id-on-the-clinet-webgl.html). Your solution would work fine to create an ID for the client, but do make sure you're saving the player prefs at some point, so that you're not creating a new one for the device on every test run (unless that's your intent).

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

doriandark99 avatar image
doriandark99 answered

Where should I paste this code. I pretty new to unity. I just saw a video on youtube to create login and create account function. So I installed Playfab SDK but for webGl how should I create GUID

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.