【问题标题】:Unity Google Play Services: Authentication Failed always:Unity Google Play 服务:身份验证始终失败:
【发布时间】:2016-10-25 13:09:03
【问题描述】:

我的身份验证有问题。身份验证总是失败,我无法理解原因(不仅在编辑器中失败,在我的设备中也是如此)。这是代码:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 using UnityEngine.Advertisements;
 using System.Collections;
 using System.Collections.Generic;
 using GooglePlayGames;
 using UnityEngine.SocialPlatforms;
 using GooglePlayGames.BasicApi;
 using UnityEngine.Analytics;

 public class Menu : MonoBehaviour {

 public Text record;

 /**
     Menu when the player isn´t authenticate
  */
 public GameObject signIn;

 public GameObject test;

 void Awake()
 {
     PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder ().Build ();
     PlayGamesPlatform.InitializeInstance(config);
     GooglePlayGames.PlayGamesPlatform.DebugLogEnabled = false;
     GooglePlayGames.PlayGamesPlatform.Activate ();
 }

 void Start()
 {
     Advertisement.Initialize ("CORRECT_NUMBER", true);


     if (PlayerPrefs.HasKey("record"))
     {
         record.text = "Record actual: " + PlayerPrefs.GetInt("record");
         Analytics.CustomEvent ("Start Play", new Dictionary<string, object>{ { "Record", PlayerPrefs.GetInt("record")} });
         if (Social.localUser.authenticated) 
         {
             Social.ReportScore (PlayerPrefs.GetInt ("record"), "CORRECT_CODE", (bool success) => { });
         }
     } else {
             record.text = "Consigue un record!!";
     }

     if (Social.localUser.authenticated) 
     {
         signIn.SetActive (false);           
     }
     test.SetActive (false);
 }

 public void OnPlay()
 {
     ShowAd ();
     SceneManager.LoadScene("Play");
     Time.timeScale = 0;
 }

 public void OnArchievements()
 {
     if (Social.localUser.authenticated) {
         Social.ShowAchievementsUI ();
     } else {
         Social.localUser.Authenticate ((bool success) => {
             if (success) {
                 Social.ShowAchievementsUI ();           
             }
         });
     }

 }

 public void OnMatching()
 {
     if (Social.localUser.authenticated) {
         Social.ShowLeaderboardUI ();
     } else {
         Social.localUser.Authenticate ((bool success) => {
             if (success) {
                 Social.ShowLeaderboardUI ();        
             }
         });
     }
 }

 public void OnExit()
 {
     Analytics.CustomEvent ("Stop Play", new Dictionary<string, object>{ { "Record", PlayerPrefs.GetInt("record")} });
     Application.Quit();
 }

 public void SignIn()
 {
     Social.localUser.Authenticate((bool success) => { 
         if (success)
         {
             Debug.Log ("Sign in");
             signIn.SetActive (false);
         }
         else
         {
             Debug.Log ("Fail for some reason...");
             test.SetActive (true);
         }

     });

 }

 public void Later()
 {
     signIn.SetActive (false);
 }

 public void ShowAd()
 {
     if (Advertisement.IsReady())
     {
         Advertisement.Show();
     }
 }
 }

对象“测试”只是一个似乎知道身份验证失败的白框。控制台没有显示任何类型的错误,所以我不知道在哪里找到问题......知道问题出在哪里吗? PD:我使用版本 0.9.34 和 Unity 5.3.5f1

提前致谢!

【问题讨论】:

  • 您好亚历杭德罗,我认为您在这里遇到了 2 个问题: 1:您忘记对用户进行身份验证-> Social.Active.localUser.Authenticate(ProcessAuthentication); GPGS 所做的是创建一个 ISocialPlatform 并将其设置为活动平台。 2-您应该在“Start()”中执行“Awake()”部分。告诉我。
  • 对不起你的例子,这将是正确的方法:当她/他点击按钮时对用户进行身份验证-> Social.Active.localUser.Authenticate(SignIn);
  • 对不起,我不明白你的意思(这是我第一次尝试使用google play服务)。我像你说的改变了清醒的部分,但我不知道你说的身份验证怎么做。我必须在 Authenticate 的“参数”中调用一个方法吗?对不起我的无知。
  • 非常感谢您的时间和帮助。

标签: c# authentication google-play-services google-play-games unity5


【解决方案1】:

没问题。 我将在这里写下回复,因为它可能会很长。

Unity 将默认的 ISocialPlatform 设置为 Apple。执行“PlayGamesClientConfiguration”,您将默认的 ISocialPlatform 更改为 Google+。

我的评论是关于你的 Awake() 函数。我建议你把它放在 Start() 中,如下所示:

    void Start()
         {
    //added code
     PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder ().Build ();
         PlayGamesPlatform.InitializeInstance(config);
         GooglePlayGames.PlayGamesPlatform.DebugLogEnabled = false;


         //GooglePlayGames.PlayGamesPlatform.Activate (); //this is wrong

      //WHAT YOU ARE MISSING
      PlayGamesPlatform.Activate();
      //END OF WHAT YOU ARE MISSING

             Advertisement.Initialize ("CORRECT_NUMBER", true);


             if (PlayerPrefs.HasKey("record"))
             {
                 record.text = "Record actual: " + PlayerPrefs.GetInt("record");
                 Analytics.CustomEvent ("Start Play", new Dictionary<string, object>{ { "Record", PlayerPrefs.GetInt("record")} });

      //you should do this once the user is authenticated and logged in
                 if (Social.localUser.authenticated) 
                 {
                     Social.ReportScore (PlayerPrefs.GetInt ("record"), "CORRECT_CODE", (bool success) => { });
                 }
             } else {
                     record.text = "Consigue un record!!";
             }

             if (Social.localUser.authenticated) 
             {
                 signIn.SetActive (false);           
             }
             test.SetActive (false);
         }

然后,您可以在场景中的检查器中创建一个按钮并调用它:

 public void SignIn()
 {
     Social.localUser.Authenticate( ProcessAuthentication)
 }

CallBackFunction 是一个接受布尔值的 void 函数。 (你做得对)我喜欢让我的代码可读且易于理解。

    void ProcessAuthentication (bool success) 
        {
            if (true == success) 
            {
                Debug.Log ("AUTHENTICATED!");
                if (Social.localUser.authenticated) 
                 {
                     Social.ReportScore (PlayerPrefs.GetInt ("record"), "CORRECT_CODE", (bool success) => { });
                 }
             } else {
                     record.text = "Consigue un record!!";
             }

             if (Social.localUser.authenticated) 
             {
                 signIn.SetActive (false);   

                 //EDIT: you need to load the achievements to use them
                 Social.LoadAchievements (ProcessLoadedAchievements);

                Social.LoadScores(leaderboardID, CALLBACK) // Callback has to be a Action<IScore[]> callback
            }
             test.SetActive (false);
            }
            else
            {
                Debug.Log("Failed to authenticate");
                signIn.SetActive (true);
            }
        }



void ProcessLoadedAchievements (IAchievement[] achievements) {
        if (achievements.Length == 0)
        {
            Debug.Log ("Error: no achievements found");
        }
        else
        {
            Debug.Log ("Got " + achievements.Length + " achievements");
        }
    }

告诉我。 (如果你愿意,我也可以用西班牙语向你解释) 致敬!

【讨论】:

  • 好的,我想我明白了,除非最后一个方法(ProcessAuthentication),我在哪里调用它?还是引擎的一种方法(如启动方法)?如果你用西班牙语jajaja写作,你真的会帮助我。再次,非常感谢!! PD:我真的不按你说的做,因为我不在家。
  • 抱歉,我编辑了 ProcessAuthentication 函数。我想给你一个 CallBackFunction 的例子。您可以根据需要重命名它。谢谢。
  • 我写了一个答案,因为我需要给你看代码,而在评论中我不能。
【解决方案2】:

我按照这个教程:google tutorial in GitHub (README) 并且教程没有说我必须实现那个方法,事实上,当我阅读它时,我明白这些方法是引擎的一部分。 这是我的新代码:

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.Advertisements;
using System.Collections;
using System.Collections.Generic;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
using GooglePlayGames.BasicApi;
using UnityEngine.Analytics;

public class Menu : MonoBehaviour {

public Text record;

public GameObject test;

/**
    Indicador del jugador de que no ha iniciado sesión, y no podrá acceder al  ranking ni a los logros.
 */
public GameObject signIn;

void Start()
{       
    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder ().Build ();
    PlayGamesPlatform.InitializeInstance(config);
    GooglePlayGames.PlayGamesPlatform.DebugLogEnabled = true;
    PlayGamesPlatform.Activate();

    Advertisement.Initialize ("NUMBER", true);
    test.SetActive (false);
     
    //EDIT:
    SignIn ();

    if (PlayerPrefs.HasKey("record"))
    {
        record.text = "Record actual: " + PlayerPrefs.GetInt("record");
        Analytics.CustomEvent ("Start Play", new Dictionary<string, object>{ { "Record", PlayerPrefs.GetInt("record")} });

    } else {
            record.text = "Consigue un record!!";
    }

}

public void OnPlay()
{
    ShowAd ();
    SceneManager.LoadScene("Jugar");
    Time.timeScale = 0;
}

public void OnArchievements()
{
    if (Social.Active.localUser.authenticated) {
        Social.Active.ShowAchievementsUI ();
    } else {
        Social.Active.localUser.Authenticate ((bool success) => {
            if (success) {
                Social.Active.ShowAchievementsUI ();            
            }
        });
    }

}

public void OnMatching()
{
    if (Social.Active.localUser.authenticated) {
        Social.Active.ShowLeaderboardUI ();
    } else {
        Social.Active.localUser.Authenticate ((bool success) => {
            if (success) {
                Social.Active.ShowLeaderboardUI ();     
            }
        });
    }
}

public void OnExit()
{
    Analytics.CustomEvent ("Stop Play", new Dictionary<string, object>{ { "Record", PlayerPrefs.GetInt("record")} });
    Application.Quit();
}

public void SignIn()
{
    Social.Active.localUser.Authenticate (ProcessAuthentication);
}

public void Later()
{
    signIn.SetActive (false);
}

public void ShowAd()
{
    if (Advertisement.IsReady())
    {
        Advertisement.Show();
    }
}
//EDIT:
void ProcessAuthentication (bool success) 
{
    if (true == success) 
    {
        Debug.Log ("AUTHENTICATED!");
        if (Social.Active.localUser.authenticated) 
        {
            Social.Active.LoadAchievements (ProcessLoadedAchievements);
            Social.Active.LoadScores (Fireball.GPGSIds.leaderboard_ranking, ProcessLoadedScores );
            if (PlayerPrefs.HasKey ("record"))
            {
                Social.Active.ReportScore (PlayerPrefs.GetInt ("record"),Fireball.GPGSIds.leaderboard_ranking, (bool report) => { });
            }
            signIn.SetActive (false);
            test.SetActive (true);
        }
    } else {
        Debug.Log("Failed to authenticate");
        signIn.SetActive (true);
    }
 }
   
  void ProcessLoadedAchievements (IAchievement[] achievements) 
{
    if (achievements.Length == 0)
    {
        Debug.Log ("Error: no achievements found");
    }
    else
    {
        Debug.Log ("Got " + achievements.Length + " achievements");
    }
}

void ProcessLoadedScores (IScore[] scores) 
{
    if (scores.Length == 0)
    {
        Debug.Log ("Error: no scores found");
    }
    else
    {
        Debug.Log ("Got " + scores.Length + " scores");
    }
}
}

编辑: 再次在编辑器中我无法进行身份验证(在我的设备中它也失败了),我真的不明白原因......这里是调试日志(我激活了它,可能会有所帮助):

调试:激活 PlayGamesPlatform。

调试:PlayGamesPlatform 已激活:GooglePlayGames.PlayGamesPlatform。

调试:创建特定于平台的 Play 游戏客户端。

调试:在编辑器中创建 IPlayGamesClient,使用 DummyClient。

调试:在 DummyClient 上接收到方法调用 - 使用存根实现。

最后一条消息,是当我尝试进行身份验证时,也许你知道调试日志的问题。

PD:我阅读了你发给我的文档,但它是面向 GameCenter (iOS) 的,但我现在了解更多,非常感谢。

再次感谢您的宝贵时间。

【讨论】:

  • 对不起,我没有意识到你想使用成就和排行榜...检查我原始回复的编辑:)
  • 另外,阅读 Social class 文档以完全理解它会有所帮助。 -> docs.unity3d.com/ScriptReference/Social.html希望对你有帮助! :D
  • 阅读您发给我的链接后,在您的帮助下,我根据我的实际情况和我的问题更新回复(逐步创建所有成就等。请检查。谢谢。
  • 我真正不明白的是为什么我写时会失败。我不需要激活谷歌平台插件吗?如果你也能解释这个细节,我真的很感谢你。
  • 哦,对不起,我翻译失败,你说你缺少什么,(我忘了)我翻译我错了,真的很抱歉,我更新解码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
相关资源
最近更新 更多