简体   繁体   中英

Unity Networking Lobby : Set player name

I work on my Networking Lobby of my 2D Game and I have a little issue with the name of my player :

I downloaded this example to help me

But as you can see, the name of the player can be changed when you have join the game and you entered in the lobby.

But in my project, I want to write my name, click on Valid, and connect to the lobby. Like that :

在此处输入图片说明

My problem is I don't know how can I use my InputField text as name for my player in my Lobby.

I try to save the name in a static Class with static property, but every players have the same name.

This is my code :

//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); 
    }
}

I know my static class not working because I have just ONE instance.

(In the asset example, the playerName can be changed in the lobby panel, but I don't want the same configuration)

Can you help me ?

Thanks

看一下NetworkIdentity -localPlayerAuthority。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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