【问题标题】:Unity Networking Lobby : Set player nameUnity Networking Lobby : 设置玩家名称
【发布时间】:2017-01-06 10:53:03
【问题描述】:

我在我的 2D 游戏的网络大厅工作,我对我的玩家的名字有一点问题:

我下载了this example来帮助我

但是如你所见,当你加入游戏并进入大厅时,玩家的名字是可以更改的。

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

我的问题是我不知道如何在我的大厅中使用 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,但我不想要相同的配置)

你能帮帮我吗?

谢谢

【问题讨论】:

    标签: c# networking unity3d


    【解决方案1】:

    看看 NetworkIdentity -localPlayerAuthority。

    【讨论】:

    • 嗨 Sajmoon,感谢您的回复。我用 PlayerPrefs 解决了我的答案。我知道这不是一个好的解决方案,但我的球员已经有了相同的名字,如果他离开了也一样。你能分享一个名称属性plz的例子吗?
    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多