【问题标题】:Social.localuser.image is always returning nullSocial.localuser.image 总是返回 null
【发布时间】:2016-08-08 21:15:25
【问题描述】:

我正在使用 Unity 以及此处的 Google Play Services Plugin for Unity:https://github.com/playgameservices/play-games-plugin-for-unity

我正在尝试访问要包含在个人资料图片中的玩家头像。问题是当我尝试访问 Texture2D Social.localuser.image 时,它​​总是返回 null。经过更多研究,似乎代码使用某种 AvatarURL 来查找图像,这就是空的东西。我用 string.IsNullOrEmpty(AvatarURL) 来检查这个。有谁知道为什么 AvatarURL 为空,和/或我如何修复它。如果不是这样,有没有其他方法可以访问玩家头像以用于我的游戏中的个人资料图片。

这是我用来测试的代码:

PlayGamesPlatform.Activate();

//Authenticate User
Social.localUser.Authenticate((bool success) => {
    if(success) {
        Debug.Log("Successfully Authenticated");
        Textures.profilePic = Sprite.Create(Social.localUser.image, new Rect(0, 0, Social.localUser.image.width, Social.localUser.image.height), new Vector2(0.5f, 0.5f));
        SceneManager.LoadScene("Main Menu");
    } else {
        Debug.Log("Failed to Authenticate User");
        SceneManager.LoadScene("ErrorCanNotSignIn");
    }
});

设置Textures.profilePic时出现错误(Textures是我创建的另一个存储纹理的类,profilePic是其中的静态Sprite变量)。它说有一个 NullReferenceException: Object reference not set to an instance of the object。

再次根据我所见,我认为错误的来源似乎是 AvatarURL 为空,因为它导致了这段代码,我很确定这是加载图像的原因,而不是运行:

if (!mImageLoading && mImage == null && !string.IsNullOrEmpty(AvatarURL))
{
    Debug.Log("Starting to load image: " + AvatarURL);
    mImageLoading = true;
    PlayGamesHelperObject.RunCoroutine(LoadImage());
}

另外,如果它很重要,我正在安卓设备上进行测试。

【问题讨论】:

    标签: android unity3d google-play-services


    【解决方案1】:

    JeanLuc 在this SO question 上回答的问题

    Play Games Unity 的 Social.localUser.image 的实现 插件总是返回 null。

    【讨论】:

    • 好的,这有点奇怪,但我似乎已经让 Social.localUser.image 不返回 null。我让它直接修改了游戏对象的精灵。刚开始的时候,精灵仍然是空的并且完全空白,但是由于某种原因,当我将场景从那个场景切换到另一个场景然后再返回时,它起作用了,并且玩家的个人资料图片在那里
    • 你应该在 GitHub 讨论上发布这个,因为这很奇怪,需要谷歌团队解决。感谢您的回复!
    • 这仍然不是我想要的,因为它仅在玩家切换场景(主菜单到设置屏幕)然后返回到精灵实际更改为人的个人资料图片的原始场景时才有效。你知道它为什么会这样吗?
    • 这听起来像是与初始化“LocalUser”有关的事情,我可以稍后看看并发布我的研究。
    【解决方案2】:

    图片返回null的原因是因为它是异步加载的,所以需要等待。

    此外还有一个错误,您首先需要访问 Social.localUser.userName。

    这段代码应该可以工作:

        Debug.Log(Social.localUser.userName);   // you need to access Social.localUser.userName, otherwise the image will always return null
        StartCoroutine(KeepCheckingAvatar());
    // ...
    
        private IEnumerator KeepCheckingAvatar()
        {
            float secondsOfTrying = 20;
            float secondsPerAttempt = 0.2f;
            while (secondsOfTrying > 0)
            {
                if (Social.localUser.image != null)
                {
                    // Do something with freshly loaded image
    
                    break;
                }
    
                secondsOfTrying -= secondsPerAttempt;
                yield return new WaitForSeconds(secondsPerAttempt);
            }
        }
    

    学分: https://github.com/playgameservices/play-games-plugin-for-unity/issues/1056#issuecomment-212257972

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-04
      • 2016-11-02
      • 2014-05-06
      • 2012-06-05
      • 2014-05-30
      • 2014-02-23
      • 2017-10-12
      • 2013-08-16
      相关资源
      最近更新 更多