簡體   English   中英

Unity Networking Lobby:設置播放器名稱

[英]Unity Networking Lobby : Set player name

我正在2D游戲的網絡大廳工作,但我的播放器名稱有一個小問題:

我下載了此示例以幫助我

但是如您所見,當您加入游戲並進入大廳時,可以更改玩家的名字。

但是在我的項目中,我想寫下我的名字,單擊“有效”,然后連接到大廳。 像那樣 :

在此處輸入圖片說明

我的問題是我不知道如何在大廳中使用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); 
    }
}

我知道我的靜態類無法正常工作,因為我只有一個實例。

(在資產示例中,可以在大廳面板中更改玩家名稱,但我不需要相同的配置)

你能幫助我嗎 ?

謝謝

看一下NetworkIdentity -localPlayerAuthority。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM