简体   繁体   English

Unity Google Play服务

[英]Unity Google Play Services

I am trying to get Google play services into the game I have developed. 我正在尝试将Google Play服务纳入我开发的游戏中。 Here is the script I have on my main screen. 这是我在主屏幕上显示的脚本。 I get no response out of it at all. 我完全没有回应。 I have tried all the different SHA1 codes and I am not sure what is wrong. 我已经尝试了所有不同的SHA1代码,但不确定是什么问题。 Any Ideas??? 有任何想法吗???

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GooglePlayGames;
using UnityEngine.UI;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;

public class GPS_Main : MonoBehaviour {

    private bool IsConnectedToGoogleServices;
    public Text SignIn;

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

    // Use this for initialization
    void Start () {

        LogIn();

    }

    // Update is called once per frame
    void Update () {

    }

    void LogIn()
    {
        Social.localUser.Authenticate(success => { });

    }

    public bool ConnectToGoogleServices()
    {
        if (!IsConnectedToGoogleServices)
        {
            Social.localUser.Authenticate((bool success) =>
            {
                IsConnectedToGoogleServices = success;
            });

        }
        return IsConnectedToGoogleServices;
        }
    public static void ToAchievementUI()
    {
        if (Social.localUser.authenticated)
        {
            Social.ShowAchievementsUI();
        }
        else
        {
            Debug.Log("Not authenticated");
        }
    }
}

This has really been an annoying event. 这确实是一个令人讨厌的事件。 I have been through many videos and books trying to find the correct fix. 我已经浏览过许多视频和书籍,试图找到正确的解决方案。

There are many things that cause this to happen, the best way to find it is to try connecting your phone and then using adb logcat to see what is causing the problem. 导致这种情况发生的原因有很多,找到它的最佳方法是尝试连接手机,然后使用adb logcat查看是什么引起了问题。

Also i have found a little bug in your function ConnectToGoogleServices : 我也发现您的函数ConnectToGoogleServices有一个小错误:

public bool ConnectToGoogleServices()
{
    if (!IsConnectedToGoogleServices)
    {
        Social.localUser.Authenticate((bool success) =>
        {
            IsConnectedToGoogleServices = success;
        });

    }
    return IsConnectedToGoogleServices;
}

This function is always returning IsConnectedToGoogleServices initial state. 此函数始终返回IsConnectedToGoogleServices初始状态。

I tried a bit of explanation in this post Google Play Sign in for Unity , however I can't understand from your question what are all options you have tried and where you are stuck in your problem. 我在本文的Google Play登录Unity中尝试了一些解释,但是从您的问题中我无法理解您尝试过的所有选项是什么,以及在哪里遇到了问题。 6 months old question at the time of me writing this. 在我撰写本文时,有6个月大的问题。 Hope you've sorted this out already. 希望您已经解决了这个问题。 If so, please post your findings here that may help others. 如果是这样,请在此处发布您的发现,这可能会对其他人有所帮助。

For anyone landing on this question for an answer, pls refer to my answer on the other post (link above) if that is of any help for your problem and also note down the problem in the code (on the question), I think AminSojoudi tried pointing it out as well. 对于在此问题上寻求答案的任何人,请参考我在另一篇文章(上面的链接)上的答案,如果这对您的问题有帮助,还请在代码中(在问题上)记下该问题,我认为AminSojoudi也尝试指出来。 Social.localUser.Authenticate() is taking the argument of a callback function (refer documentation ). Social.localUser.Authenticate()使用回调函数的参数(请参阅文档 )。 So don't expect it to assign IsConnectedToGoogleServices the result (success or failure) within ConnectToGoogleServices() scope itself. 因此,不要期望它在ConnectToGoogleServices()范围本身内为IsConnectedToGoogleServices分配结果(成功或失败)。 If Authenticate() call runs faster than your code execution, you may get a success, but that won't happen and your function won't return the actual success status of Authenticate() function call anytime. 如果Authenticate()调用的运行速度快于代码执行的速度,则可能会成功,但是不会成功,并且函数不会随时返回Authenticate()函数调用的实际成功状态。 If your rest of the code (leaderboard, achievements) relies on IsConnectedToGoogleServices Boolean flag, then those will not work as well. 如果您的其余代码(排行榜,成就)依赖于IsConnectedToGoogleServices布尔值标记,则这些标记将无法正常工作。

public bool ConnectToGoogleServices()
{
    if (!IsConnectedToGoogleServices)
    {
        Social.localUser.Authenticate((bool success) =>
        {
            IsConnectedToGoogleServices = success; // <-- callback result
        });

    }
    return IsConnectedToGoogleServices;  // <-- this won't return the result of a callback result.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM