Java 统一+;PlayFab c#顺序执行

Java 统一+;PlayFab c#顺序执行,java,c#,unity3d,c#-4.0,Java,C#,Unity3d,C# 4.0,请提供C#语法方面的建议,我正在对PlayFab进行Rest API调用,以获取播放器配置文件,然后将播放器显示名称分配给本地公共变量,但在执行函数序列的过程中,我发现它们根本不是顺序的。我不明白为什么在python或java中,所有函数都是顺序的,它们将在另一个函数完成后执行 根据逻辑,它应该使用OnLoginWithGoogleAccountPlayFab执行start函数,该函数将调用GetPlayerProfile获取播放器名称并分配到公共字符串PlayerDisplayName,然后调

请提供C#语法方面的建议,我正在对PlayFab进行Rest API调用,以获取播放器配置文件,然后将播放器显示名称分配给本地公共变量,但在执行函数序列的过程中,我发现它们根本不是顺序的。我不明白为什么在python或java中,所有函数都是顺序的,它们将在另一个函数完成后执行

根据逻辑,它应该使用OnLoginWithGoogleAccountPlayFab执行start函数,该函数将调用GetPlayerProfile获取播放器名称并分配到公共字符串PlayerDisplayName,然后调用ChangeSceneOnNewPlayer,如果名称为空,则检查名称

但它是这样执行的

方法是按顺序执行的,但返回的结果是异步返回的。这会在代码中引入竞争条件,也是您在收到玩家配置文件之前看到执行
ChangeSceneOnNewPlayer
方法的原因

让我们将以下几行中发生的情况进行分解(为了清晰起见,已删除调试):

//这两行同步执行。
PlayFabId=result.PlayFabId;
SessionTicket=result.SessionTicket;
//这里调用的是一个同步方法,但在该方法内部
//对PlayFab进行异步调用,结果不会立即显示
//返回。
GetPlayerProfile();
//然后您立即调用此方法,但尚未等待来自的结果
//在你打电话之前先玩游戏。
ChangeSceneOnNewPlayer();
你想要的是更像这样的东西:

publicplayerDisplayName;
void Start()
{
仅使用GoogleAccountPlayFab()进行登录;
}
public void OnLoginWithGoogleAccountPlayFab()
{
PlayFabClientAPI.LoginWithGoogleAccount(新的LoginWithGoogleAccountRequest()
{
TitleId=PlayFabSettings.TitleId,
ServerAuthCode=AuthCode,
CreateAccount=true
},(结果)=>
{
PlayFabId=result.PlayFabId;
SessionTicket=result.SessionTicket;
//获取配置文件,并指定返回结果时的回调
//来自PlayFab。
GetPlayerProfile(OnGetPlayerProfile);
},错误);
}
公共无效GetPlayerProfile(操作onGetPlayerDisplayName)
{
PlayFabClientAPI.GetPlayerProfile(新的GetPlayerProfileRequest()
{
PlayFabId=PlayFabId,
ProfileConstraints=new PlayerProfileViewConstraints()
{
ShowDisplayName=true,
}
},onGetPlayerDisplayName,OnPlayFabError);
}
//此方法用作对GetPlayerProfile函数的回调,并用于
//处理信息。
公共无效OnGetPlayerProfile(PlayerDisplayName PlayerDisplayName)
{
PlayerDisplayName=PlayerDisplayName;
如果(PlayerDisplayName==null)
{
SceneManager.LoadSceneAsync(“新播放器”,LoadSceneMode.Single);
}
其他的
{
SceneManager.LoadSceneAsync(“城市”,LoadSceneMode.Single);
}
}

这是使用任何RESTful API调用时的标准模式。

请删除Java标记。这个问题与Java无关。嗨,Aaron,如果我想重用GetPlayerProfile函数,怎么办?@vinilka8没有什么能阻止你重用它。
GetPlayerProfile
方法本质上是异步的,因此您只需在代码的其余部分考虑它。可以缓存值,这样您就可以保存
PlayerDisplayName
并在以后访问它。但是,如果这样做,您可能会遇到问题。假设您缓存了
PlayerDisplayName
值,但随后它们更改了配置文件信息。您需要确保在发生这种情况时刷新该数据,否则您将向用户显示一个“过时”值。您好@Aaron,感谢您的解释,对不起,我的问题不清楚,我希望使用GetPlayerProfile函数进行其他操作,如果我再打一次电话,我将处理再次切换场景的问题,但我不希望这种情况发生。只是在开始的时候。
public PlayerDisplayName;
void Start(){
        OnLoginWithGoogleAccountPlayFab();
}
public void OnLoginWithGoogleAccountPlayFab() {
        PlayFabClientAPI.LoginWithGoogleAccount(new LoginWithGoogleAccountRequest()
        {
            TitleId = PlayFabSettings.TitleId,
            ServerAuthCode = AuthCode,
            CreateAccount = true
        }, (result) =>
        {
            PlayFabId = result.PlayFabId;
            SessionTicket = result.SessionTicket;
            Debug.LogWarning("waaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrr- --------------------- GET PROFILE 1");
            GetPlayerProfile();
            Debug.LogWarning("waaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrr- ---------------------NAME 1 ");
            ChangeSceneOnNewPlayer();
        }, OnPlayFabError);
    }

public void GetPlayerProfile() {
        PlayFabClientAPI.GetPlayerProfile(new GetPlayerProfileRequest()
        {
            PlayFabId = PlayFabId,
            ProfileConstraints = new PlayerProfileViewConstraints()
            {
                ShowDisplayName = true,
            }
        }, (result) =>
        {
            Debug.LogWarning("waaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrr- ---------------------PROFILE 2");
            Debug.Log("The player's DisplayName profile data is: " + result.PlayerProfile.DisplayName);
            PlayerDisplayName = result.PlayerProfile.DisplayName;
            Debug.LogWarning("waaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrr- ---------------------PROFILE 3");
        }, OnPlayFabError);
    }

public void ChangeSceneOnNewPlayer() {
        Debug.Log("Player Info");
        Debug.LogWarning("waaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrr- ---------------------NAME 2 ");
        if (PlayerDisplayName == null) {
            Debug.Log("Player Info is NULL " + PlayerDisplayName);
            Debug.LogWarning("waaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrr- ---------------------NAME 3 ");
            SceneManager.LoadSceneAsync("New_Player", LoadSceneMode.Single);
        } else {
            Debug.Log("Player Info NOT null " + PlayerDisplayName);
            Debug.LogWarning("waaaaaaaaaaaaaaaaaaaaaarrrrrrrrrrrr- ---------------------NAME 3 ");
            SceneManager.LoadSceneAsync("City", LoadSceneMode.Single);
        }
    }