Unity Networking Lobby:设置玩家名称

时间:2017-01-06 10:53:03

标签: c# networking unity3d

我在2D游戏的网络大厅工作,我的播放器名称有点问题:

我下载了this example来帮助我

但正如您所看到的,当您加入游戏并进入大厅时,可以更改玩家的名称。

但在我的项目中,我想写下我的名字,点击有效,然后连接到大厅。像那样:

enter image description here

我的问题是我不知道如何在我的大厅中使用我的InputField文本作为我的播放器的名称。

我尝试将名称保存在具有静态属性的静态类中,但每个玩家都有相同的名称。

这是我的代码:

//Attached to my Name Panel
public class LobbyName : MonoBehaviour
{
    public LobbyManager LobbyManager;
    public Text TxtName;

    public void OnClickValidName()
    {
        Constants.PlayerName = TxtName.text;

        LobbyManager.ChangeTo(LobbyManager.MenuLobby);
    }
}

//Attached to my canvas
public class LobbyManager : NetworkLobbyManager
{
    public Button BackButton;

    private RectTransform _currentPanel;
    public RectTransform ListLobby;
    public RectTransform MenuLobby;
    public RectTransform NameLobby;

    public string PlayerName { get; set; }

    private void Start()
    {
        _currentPanel = NameLobby; 

        NameLobby.gameObject.SetActive(true);
    }

    public void ChangeTo(RectTransform newPanel)
    {
        if (_currentPanel != null)
            _currentPanel.gameObject.SetActive(false);

        if (newPanel != null)
            newPanel.gameObject.SetActive(true);

        _currentPanel = newPanel;
    }
}

//Attached to my Player Item
public class LobbyPlayer : NetworkLobbyPlayer
{
    public Text TxtPlayerName;

    public string PlayerName = "";

    public override void OnClientEnterLobby()
    {
        base.OnClientEnterLobby();

        TxtPlayerName.text = Constants.PlayerName;

        LobbyPlayerList.Instance.AddPlayer(this);
    }
}

//Attached to my Menu lobby panel
public class LobbyMenu : MonoBehaviour
{
    public LobbyManager LobbyManager; 

    public void OnClickConnect()
    {
        LobbyManager.StartClient();

        LobbyManager.ChangeTo(LobbyManager.ListLobby); 
    }
}

我知道我的静态类没有工作,因为我只有一个实例。

(在资产示例中,可以在大厅面板中更改playerName,但我不想要相同的配置)

你能帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

查看NetworkIdentity -localPlayerAuthority。