简体   繁体   English

使用镜像和 playfab 的 Unity 团队多人游戏

[英]Unity team multiplayer using mirror and playfab

I want to make a team vs team game using mirror in Unity.我想在 Unity 中使用镜像制作团队对团队游戏。

This game goes by rounds, so once one team eliminates the members of the other team, the winner team gets one point and when a team get a certain number of points, the game ends.这场比赛以回合制进行,因此一旦一队淘汰另一队的成员,获胜队将获得一分,当一队获得一定数量的积分时,比赛结束。

To select teamm the player selects in a dropdown menu in a previous scene which team wants, and store it in the PlayerPrefs, and then when the player gameobject is instantiated, I get the PlayerPrefs and pass it to the Game Manager.要选择团队,玩家在先前场景的下拉菜单中选择团队想要的,并将其存储在 PlayerPrefs 中,然后当玩家游戏对象被实例化时,我获取 PlayerPrefs 并将其传递给游戏管理器。

 playerTeam = PlayerPrefs.GetInt("Team");           
 GameManager.sharedIstance.AddNewPlayer(playerId, playerTeam);`

Then in the GameManager method I update the numberOfPlayers in team然后在 GameManager 方法中,我更新了团队中的 numberOfPlayers

public void AddNewPlayer(int playerID, int team)
    {
        ActivePlayers.Add(playerID);
        //Cuando se añaden los jugadores se incrementa el contador de jugadores en el equipo correspondiente
        switch (team)
        {
         
            case 0:
                numberOfPlayerTeam1++;
                break;
            case 1:
                numberOfPlayerTeam2++;
                break;
        }

This works as intended and the players are separated in diferent teams so they can play.这可以按预期工作,并且球员被分开在不同的球队中,以便他们可以比赛。

At the end I restart the scene to play again but both players now belong to the same team最后我重新开始场景再次玩,但现在两个玩家都属于同一支球队

private NetworkManager Room
    {
        get
        {
            if (room != null) { return room; }
            return room = NetworkManager.singleton;
        }
    }

 private void RestartRound()
    {
        Room.ServerChangeScene("InGameScene");
    }

This game also uses Playfab and also tried to store there the team info, and get the playerPrefs updated by the GetUserData and SetUserData methods该游戏还使用 Playfab 并尝试将团队信息存储在那里,并通过 GetUserData 和 SetUserData 方法获取 playerPrefs 更新

 public void SetUserData()
 {
    PlayFabClientAPI.UpdateUserData(new UpdateUserDataRequest()
    {
        Data = new Dictionary<string, string>() {
        {"Player", PlayerPrefs.GetInt("Team").ToString()}            
    }
    },
result => Debug.Log("Successfully updated user data"),
error => {
    Debug.Log("Got error setting user data Ancestor to Arthur");
    Debug.Log(error.GenerateErrorReport());
});



}

public void GetUserData(string myPlayFabeId)
{
    PlayFabClientAPI.GetUserData(new GetUserDataRequest()
    {
        PlayFabId = myPlayFabeId,
        Keys = null
    }, result => {
        Debug.Log("Got user data:");
        if (result.Data == null || !result.Data.ContainsKey("Team")) Debug.Log("Team");
        else
        {
            PlayerPrefs.SetInt("Team", Int32.Parse(result.Data["Player"].Value));
          
        }
    }, (error) => {
        Debug.Log("Got error retrieving user data:");
        Debug.Log(error.GenerateErrorReport());
    });
}

And then when the Player is instantiated然后当 Player 被实例化时

PlayfabController.sharedInstance.GetUserData(PlayerPrefs.GetString("PlayFabId"));
playerTeam = PlayerPrefs.GetInt("Team");

But the result it's the same.但结果是一样的。 The first round works as intented, but in the next rounds, all player changes to the same team.第一轮按预期进行,但在接下来的几轮中,所有球员都换到同一支球队。

Could you tell me how can I persist the team selection data between rounds please?你能告诉我如何在两轮之间保留团队选择数据吗?

The PlayerPrefs are variables that are stored in your user registry. PlayerPrefs 是存储在用户注册表中的变量。 So if you're testing on the same computer, these variables ends the same.因此,如果您在同一台计算机上进行测试,这些变量的结果相同。

So, my question is.所以,我的问题是。 Do you test it on the same computer using Parallel Sync or are you using different devices ?您是使用 Parallel Sync 在同一台计算机上测试它还是使用不同的设备?

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

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