You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity集成Firebase时Game Center认证identifier字段为空求助

Game Center认证后Firebase用户DisplayName为空的解决方法

问题原因

Firebase的Game Center认证流程默认不会自动同步Game Center的玩家昵称到Firebase用户的DisplayName字段——Game Center提供的认证凭据仅包含玩家的唯一标识,不附带公开显示名称,这和Play Games/Apple登录的默认行为不同。

解决方案

通过Unity的Social API获取Game Center玩家昵称,再主动更新到Firebase用户的Profile中。

修改代码步骤

在登录成功的回调HandleSignInWithUser中,添加获取Game Center用户名并更新Firebase用户信息的逻辑:

// Called when a sign-in without fetching profile data completes.
void HandleSignInWithUser(Task<Firebase.Auth.FirebaseUser> task)
{
    _authenticationInProgress = false;

    if (task.IsCanceled)
    {
        Debug.LogError("GameCenter Firebase Sign-In was canceled.");
    }
    else if (task.IsFaulted)
    {
        Debug.LogError("GameCenter Firebase Sign-In failed: " + task.Exception);
    }
    else
    {
        FirebaseUser user = task.Result;
        Debug.Log("GameCenter Firebase Sign-In successful: " + user.DisplayName);

        // 1. 获取GameCenter玩家昵称
        string gameCenterUserName = Social.localUser.userName;
        
        // 2. 仅当DisplayName为空且GameCenter用户名有效时更新
        if (!string.IsNullOrEmpty(gameCenterUserName) && string.IsNullOrEmpty(user.DisplayName))
        {
            var profileUpdate = new Firebase.Auth.UserProfile
            {
                DisplayName = gameCenterUserName
            };
            
            // 异步更新用户Profile
            user.UpdateUserProfileAsync(profileUpdate).ContinueWithOnMainThread(updateTask =>
            {
                if (updateTask.IsCompletedSuccessfully)
                {
                    Debug.Log("DisplayName已更新为GameCenter昵称: " + gameCenterUserName);
                    // 更新后可通过auth.CurrentUser获取最新用户信息
                    FirebaseUser updatedUser = auth.CurrentUser;
                    Debug.Log("更新后的DisplayName: " + updatedUser.DisplayName);
                }
                else if (updateTask.IsFaulted)
                {
                    Debug.LogError("更新DisplayName失败: " + updateTask.Exception);
                }
            });
        }

        if (!_firstSignInComplete)
        {
            FirestorePlayerData.Instance.ReadPlayerData(true);
            _firstSignInComplete = true;
        }
    }
}

关键注意事项

  • 确保Social.localUser.authenticated为true时,Social.localUser.userName已正确获取到Game Center昵称——若为空,检查Game Center的沙盒环境配置或玩家是否已设置昵称。
  • 仅在首次登录或DisplayName为空时执行更新,避免重复操作浪费资源。
  • 2023年Firebase发布的Game Center变更文章主要解决的是iOS版本兼容性问题(如iOS 16+的认证流程调整),与DisplayName填充无关,你的12.1.0版本已覆盖该修复。

内容的提问来源于stack exchange,提问作者The702Guy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:50:15