playgameservices / play-games-plugin-for-unity

Google Play Games plugin for Unity
Other
3.43k stars 952 forks source link

Sign in not working, please help #3136

Open hranthakobian opened 2 years ago

hranthakobian commented 2 years ago

Hello everyone, I'm faced with a problem here, when I open the game nothing happens, I thought it was a mistake to sign-in, but I also think that there is a problem with keeping and reading. Let me warn you, this is the first time I'm doing this and I do not even really understand what I wrote, I only read a ReadMe and watched one video I wrote this code with it, here it is:

`using System; using System.Text; using UnityEngine; using UnityEngine.UI; using GooglePlayGames; using System.Threading.Tasks; using GooglePlayGames.BasicApi; using System.Collections.Generic; using UnityEngine.SocialPlatforms; using GooglePlayGames.BasicApi.SavedGame;

public class GPGSManager : MonoBehaviour {

bool isSaving;

public DataManager DataManager;

DateTime startDateTime;

public void Awake() {
    PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
    PlayGamesPlatform.Activate();
}

internal void ProcessAuthentication(SignInStatus status) {
    if (status == SignInStatus.Success) {
        startDateTime = DateTime.Now;
    } else {

    }
}

public void OpenSaved(bool saving) {
    isSaving = saving;
    OpenSavedGame("SAVE");
}

void OpenSavedGame(string filename) {
    ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
    savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
        ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);
}

public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {
    if (status == SavedGameRequestStatus.Success) {
        if(isSaving == true) {
            string json = JsonUtility.ToJson(DataManager.data);
            byte[] barray = Encoding.UTF8.GetBytes(json);
            SaveGame(game, barray);
        } else if (isSaving == false) {
            LoadGameData(game);
        }
    } else {
        // handle error
    }
}

void SaveGame(ISavedGameMetadata game, byte[] savedData) {
    TimeSpan currentSpan = DateTime.Now - startDateTime;
    TimeSpan totalPlaytime = game.TotalTimePlayed + currentSpan;

    ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;

    SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();
    builder = builder
        .WithUpdatedPlayedTime(totalPlaytime)
        .WithUpdatedDescription("Saved game at " + DateTime.Now);
    SavedGameMetadataUpdate updatedMetadata = builder.Build();
    savedGameClient.CommitUpdate(game, updatedMetadata, savedData, OnSavedGameWritten);
}

public void OnSavedGameWritten(SavedGameRequestStatus status, ISavedGameMetadata game) {
    if (status == SavedGameRequestStatus.Success) {
        Debug.Log("Yes, Datas");
    } else {
        // handle error
    }
}

void LoadGameData(ISavedGameMetadata game) {
    ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
    savedGameClient.ReadBinaryData(game, OnSavedGameDataRead);
}

public void OnSavedGameDataRead(SavedGameRequestStatus status, byte[] data) {
    if (status == SavedGameRequestStatus.Success) {
        if(data.Length > 0) {
            string json = JsonUtility.ToJson(data);
            DataManager.data = JsonUtility.FromJson<GameData>(json);
        } else {
            Debug.Log("No Datas");
        }
    } else {
        // handle error
    }
}

}`

I also wrote in GameManager that I want him to read the data in Awake

`using System.Text; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO;

public class DataManager : MonoBehaviour {

public GameData data = new GameData();
public GPGSManager GPGSManager;

void Awake() {
    data.foneLockerId = new int[2];
    data.winID = new int[4];
    data.winsCounts = new int[4];
    data.buyID = new int[1];
    data.skinID = new int[2];
}

void Start() {
    // #if UNITY_ANDROID && !UNITY_EDITOR
    //         path = Path.Combine(Application.persistentDataPath, "Save.json");
    // #else
    //         path = Path.Combine(Application.dataPath, "Save.json");
    // #endif
    if (!PlayerPrefs.HasKey("Save")) Debug.Log("gfr");
    else {
        data = JsonUtility.FromJson<GameData>(PlayerPrefs.GetString("Save"));
    }
}

#if UNITY_ANDROID && !UNITY_EDITOR
private void OnApplicationPause(bool pause) {
    GPGSManager.OpenSavedGame(true);
    PlayerPrefs.SetString("Save", JsonUtility.ToJson(data));
}
#endif

private void LateUpdate() {
    GPGSManager.OpenSaved(true);
    PlayerPrefs.SetString("Save", JsonUtility.ToJson(data));
}

}`

and here I wrote to for writing in LateUpdate

`public GPGSManager GPGSManager;

void Awake() { id = DataManager.data.skin; GPGSManager.OpenSaved(false); }`

(; Please help me :)

BlackCatKPI commented 2 years ago

Hey! I'd advise you to do everything step by step. E.g. I got a code

PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);

private void ProcessAuthentication(SignInStatus status)
{
    if (status == SignInStatus.Success)
    {
    }
    else
    {
    }
}

public void ManualAuthenication()
{
    PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication);
}

and it works

/GooglePlayGameServices 0.11.01v/

I got huge troubles with authenication. This helped me:

Follow the minify setting. (player setting > publishing settings >

minify release : proguard, minify debug : proguard, Check 'user proguard file' enable, Copy and paste the text(copy below text) into the file(Assets\Plugins\Android\proguard-user.txt)).

-keep class com.google.android.gms.games.leaderboard. { *; } -keep class com.google.android.gms.games.snapshot.* { ; } -keep class com.google.android.gms.games.achievement. { *; } -keep class com.google.android.gms.games.event. { *; } -keep class com.google.android.gms.games.stats.* { ; } -keep class com.google.android.gms.games.video. { ; } -keep class com.google.android.gms.games. { ; } -keep class com.google.android.gms.common.api.ResultCallback { ; } -keep class com.google.android.gms.signin. { *; } -keep class com.google.android.gms.dynamic.* { ; } -keep class com.google.android.gms.dynamite. { *; } -keep class com.google.android.gms.tasks. { *; } -keep class com.google.android.gms.security.* { ; } -keep class com.google.android.gms.base. { *; } -keep class com.google.android.gms.actions. { *; } -keep class com.google.games.bridge.* { ; } -keep class com.google.android.gms.common.ConnectionResult { ; } -keep class com.google.android.gms.common.GooglePlayServicesUtil { ; } -keep class com.google.android.gms.common.api. { ; } -keep class com.google.android.gms.common.data.DataBufferUtils { ; } -keep class com.google.android.gms.games.quest. { *; } -keep class com.google.android.gms.nearby.* { ; }ms.common.ConnectionResult { ; } -keep class com.google.android.gms.common.GooglePlayServicesUtil { ; } -keep class com.google.android.gms.common.api. { ; } -keep class com.google.android.gms.common.data.DataBufferUtils { ; } -keep class com.google.android.gms.games.quest.* { ; } -keep class com.google.android.gms.nearby.* { ; }

userqt commented 2 years ago

@hrastgh1 were you able to resolve it? I believe you are probably getting an error - look here: https://github.com/playgameservices/play-games-plugin-for-unity/issues/3154

And that is the reason that nothing is shown, at least on my side. Otherwise you should take a look if the Google Play and the Cloud Platform settings are properly set up.

The sollution by @BlackCatKPI does not seem to be working. Though I am not sure, which Unity version he is using, there is no such option user proguard file. There is Custom Proguard File option in Unity 2021.3.2. And also three checkboxes: Use R8, Release and Debug. I dont see anywhere, written proguard.

Any ideas how to resolve this?

hranthakobian commented 2 years ago

2022-05-22_21-47-46

Makes such an error with me and it still does not work, is this normal? By the way, I am testing this in a closed test.