1

I'm working on a Unity game where the player builds a city and the user evolution data is kept on a server. With this in mind I need to detect when the player connects on a different device to provide him with his own city (and not a new one). What is the best way to achieve this and if possible, one that doesn't require an action from the player, like clicking on a login button or accepting permissions?

For now I'm only developing for Android and my understanding is that I need the App to provide the server with a TokenId that it can validate (and retrieve an associated player id). How should I do this? I'm trying to use Google Play Games (plugin for Unity) but it seems to have a lot of features that I don't need. Besides that and probably because of those features, in my first attempt asked the user for permissions on:

  • Know who you are on Google
  • Manage your game activity for this game

I've played other games that connect with Google Play account and don't ask for such permissions, just say "Welcome XXX". Can anyone give me some tips on how to do this?

Janvi Vyas
  • 732
  • 5
  • 16
Bruno
  • 669
  • 1
  • 5
  • 4

1 Answers1

1

What is the best way to achieve this and if possible, one that doesn't require an action from the player, like clicking on a login button or accepting permissions?

You can't detect the-same user on different devices without permission. You need must have a way to detect something common on those different devices in order to determine if it's the-same user.

In this case, the only information possible to do this is the account registered on the device and it requires permission. No other way. That's possibly the only way to do this.

This is the only permission needed and can be easily added to your Unity project. No plugins required:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Get the account or email tied to the device and determine if it's on the server with the WWW or UnityWebRequest API then pull the data saved on that account.

Here is how to get all accounts on the Android device with C#:

string[] getAllEmailAccounts()
{
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

    AndroidJavaClass ACManager = new AndroidJavaClass("android.accounts.AccountManager");
    AndroidJavaObject ACManagerAct = ACManager.CallStatic<AndroidJavaObject>("get", currentActivity);

    AndroidJavaObject accounts = ACManagerAct.Call<AndroidJavaObject>("getAccountsByType", "com.google");
    AndroidJavaObject[] accountArray = AndroidJNIHelper.ConvertFromJNIArray<AndroidJavaObject[]>(accounts.GetRawObject());

    string[] accountName = new string[accountArray.Length];

    for (int i = 0; i < accountName.Length; i++)
    {
        accountName[i] = accountArray[i].Get<string>("name");
    }

    return accountName;
}

By doing this, you will avoid using the Google Play Games plugin just for this.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you for your detailed answer. In this way how can I guarantee the veracity of the data received from the App on the Server? Imagine that someone creates a script that tries different account names and by luck gets a valid one. – Bruno Apr 28 '18 at 22:58
  • Last time I checked, the first account returned in that array is linked to android market which requires username and password which means that it is safe to use that first item. I don't know if this has changed since then. I suggest you run a test and verify before using this. – Programmer Apr 28 '18 at 23:10
  • In my previous answer maybe I didn't explain correctly, but I need to validate the user identification data received on the Server. Otherwise, one player could manipulate the data and get the city from another one. Following the suggestion on Google Developers site ( https://developers.google.com/identity/sign-in/android/backend-auth ) we shouldn't accept plain user IDs, so I will be using the TokenId method (with the Google Play Games plugin for Unity). Nonetheless I appreciate your answer, thank you! – Bruno Apr 29 '18 at 20:11