【发布时间】: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