请注意C#语法,我正在对PlayFab执行Rest调用,以获取一个播放器配置文件,然后将播放器显示名称分配给一个本地公共变量,但是在执行过程中,我发现它们根本不是顺序的。我不明白为什么像python或java一样,所有函数都是顺序的,它们将在另一个函数完成后执行。
根据逻辑,它应该使用OnLoginWithGoogleAccountPlayFab执行开始函数,该函数将调用GetPlayerProfile,获取播放器名称并在公共字符串PlayerDisplayName,上赋值,然后调用ChangeSceneOnNewPlayer,后者将检查名称是否为null。
但它以这种方式执行在这里输入图像描述
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);
}
}发布于 2020-04-13 20:58:48
您的方法将按顺序执行,但返回的结果将异步返回。这在代码中引入了竞争条件,也是在收到播放机配置文件之前看到正在执行的ChangeSceneOnNewPlayer方法的原因。
让我们在下面的行中详细分析正在发生的事情(为清晰起见删除了调试):
// Both these lines execute synchronously.
PlayFabId = result.PlayFabId;
SessionTicket = result.SessionTicket;
// Here you're invoking a synchronous method, but inside the method
// an asynchronous call is made to PlayFab, and the result will not be immediately
// returned.
GetPlayerProfile();
// Then you immediately call this method, but you haven't waited for the result from
// PlayFab before you make this call.
ChangeSceneOnNewPlayer();你想要的是这样的东西:
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;
// Get the profile, and specify a callback for when the result is returned
// from PlayFab.
GetPlayerProfile(OnGetPlayerProfile);
}, OnPlayFabError);
}
public void GetPlayerProfile(Action<PlayerDisplayName> onGetPlayerDisplayName)
{
PlayFabClientAPI.GetPlayerProfile(new GetPlayerProfileRequest()
{
PlayFabId = PlayFabId,
ProfileConstraints = new PlayerProfileViewConstraints()
{
ShowDisplayName = true,
}
}, onGetPlayerDisplayName, OnPlayFabError);
}
// This method is used as a callback to your GetPlayerProfile function and is used
// to process the information.
public void OnGetPlayerProfile(PlayerDisplayName playerDisplayName)
{
PlayerDisplayName = playerDisplayName;
if (PlayerDisplayName == null)
{
SceneManager.LoadSceneAsync("New_Player", LoadSceneMode.Single);
}
else
{
SceneManager.LoadSceneAsync("City", LoadSceneMode.Single);
}
}这是使用任何RESTful API调用时的标准模式。
https://stackoverflow.com/questions/61195065
复制相似问题