playgameservices / play-games-plugin-for-unity

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

Google play services cloud save not responding #3123

Closed saqibkhan2523 closed 2 years ago

saqibkhan2523 commented 2 years ago

The following is the script and I am calling signing functions and OpenSave(bool saving) from another script on button click. Now its not working. I do not know why.

    public static PlayServicesScript instance;
    [SerializeField] Text test_dataText;
    [SerializeField] Text test_dataStatusText;
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(this.gameObject);
        }
        else
        {
            Destroy(this.gameObject);
        }
        try
        {

            PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().EnableSavedGames().Build();
            PlayGamesPlatform.InitializeInstance(config);
            PlayGamesPlatform.DebugLogEnabled = true;
            PlayGamesPlatform.Activate();

        }
        catch (Exception exception)
        {
            Debug.Log(exception);
        }
    }
    public void SignIn(Action successCallback = null, Action errorCallback = null)
    {
        try
        {
            Social.localUser.Authenticate((bool success) =>
            {
                if (success)
                {
                    successCallback?.Invoke();
                }
            });
        }
        catch (Exception e)
        {
            Debug.Log(e);
            errorCallback?.Invoke();
        }
    }
    public void SignOut()
    {
        if (Social.localUser.authenticated)
        {
            PlayGamesPlatform.Instance.SignOut();
        }
    }

    #region CLOUD_SAVE

    string saveStr;
    string fileName;
    public void SetFileName(string fName)
    {
        fileName = fName;
    }

    private string GetFileName()
    {
        return fileName;
    }

    private string GetStringToSave()
    {
        return saveStr;
    }

    public void SetStringToSave(string str)
    {
        saveStr = str;
    }

    private bool isSaving = false;
    byte[] bData;

    public void OpenSave(bool saving)
    {
        if (Social.localUser.authenticated)
        {
            Debug.Log("Inside OpenSave()");
            isSaving = saving;

            ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution("testit",
                DataSource.ReadCacheOrNetwork,
                ConflictResolutionStrategy.UseLongestPlaytime,
                SavedGameOpen);
        }
    }

    private void SavedGameOpen(SavedGameRequestStatus reqStatus, ISavedGameMetadata metadata)
    {

        if (reqStatus == SavedGameRequestStatus.Success)
        {
            Debug.Log("Inside SavedGameOpen()");
            if (isSaving)
            {
                byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetStringToSave());
                SavedGameMetadataUpdate metaDataUpdate = new SavedGameMetadataUpdate.Builder().WithUpdatedDescription("Saved at: " + System.DateTime.Now.ToString()).Build();
                ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(metadata, metaDataUpdate, data, SaveUpdate);
            }

            else
            {
                ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(metadata, SaveRead);
            }
        }
    }

    private void SaveUpdate(SavedGameRequestStatus reqStatus, ISavedGameMetadata metadata)
    {
        if (reqStatus == SavedGameRequestStatus.Success)
        {
            Debug.Log("Inside SaveUpdate()");
            test_dataStatusText.text = "Saved Successfully";
            Debug.Log("Saved Successfully");
        }
        else
        {

            test_dataStatusText.text = "Saved Failed";
            Debug.LogError("Saved Failed");
        }
    }

    private void SaveRead(SavedGameRequestStatus reqStatus, byte[] data)
    {
        if (reqStatus == SavedGameRequestStatus.Success)
        {
            Debug.Log("Inside SaveRead()");
            test_dataText.text = System.Text.ASCIIEncoding.ASCII.GetString(data);
            Debug.Log("Data: " + test_dataText.text);
            test_dataStatusText.text = "Read Successfully";
            Debug.Log("Read Successfully");
        }
        else
        {
            test_dataStatusText.text = "Read Failed";
            Debug.LogError("Read Failed");
        }
    }

    #endregion

Logcat shows following whenever I call OpenSave()

03-08 22:37:28.441  5227  5255 I Unity   : Inside OpenSave()
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.Logger:Log(LogType, Object)
03-08 22:37:28.441  5227  5255 I Unity   : PlayServicesScript:OpenSave(Boolean)
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.Events.UnityAction:Invoke()
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.Events.UnityEvent:Invoke()
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.EventSystems.EventFunction`1:Invoke(T1, BaseEventData)
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean)
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents()
03-08 22:37:28.441  5227  5255 I Unity   : UnityEngine.EventSystems.StandaloneInputModule:Process()
03-08 22:37:28.441  5227  5255 I Unity   :

Today I opened and ran the app again and it worked. Can anyone tell me why is this? My wifi was connected so no issues there.

Also how can I save a static object on cloud. I am Unable to convert it to byte data.