【发布时间】:2016-01-18 16:23:30
【问题描述】:
我正在尝试使用 Unity 5.1.4 和 c# 为 Android 制作网络游戏。首先,我制作了一个可以托管和加入的网络游戏。对于团队管理的原因,我需要一个大厅,首先需要客户端建立与服务器的连接以同步变量。
我使用了 Unity 标准资源并尝试为必要的脚本派生一些方法。将 NetworkLobby 连接到正常工作的网络游戏仍然存在一些问题。
更准确地说,我需要这个大厅让加入的玩家选择一个“团队”,并通过该大厅选择生成哪个玩家预制件。
情况/想法:
有两个主要脚本正在运行。扩展 NetworkLobbyManager 的 NetworkLobbyManager_custom 和扩展 LobbyPlayer 的 LobbyPlayer_custom。两者都是大厅的标准资产。 NetworkLobbyManager 连接到 GameObject,而 LobbyPlayer_custom 连接到我用作大厅预制件的预制件上。
NetworkLobbyManger_Custom 管理我的菜单和大厅场景中的画布和按钮。它还建立了服务器和客户端之间的连接。
LobbyPlayer_custom 用于保存玩家选择的团队并切换到客户端上的游戏场景。
该应用程序的工作原理如下:
- NetworklobbyManager_custom 允许您启动主机/作为客户端加入。
- 建立连接后,会弹出一个带有两个按钮的画布,用于选择团队。
- 单击按钮时,会检查团队是否有空闲位置,并且应将所选团队保存在 LobbyPlayer_custom 脚本中。
这是我有问题的地方。我无法访问包含脚本或用于保存选择的脚本的 LobbyPlayerPrefabs。
当我尝试访问 LobbyPlayer_custom 脚本以查找做出选择的 LobbyPlayer_custom 的 ID 时,我收到 NullPointerException。
private string GetLobbyPlayersIdByIp (string ip)
{
LobbyPlayer_custom lobbyPlayer;
for(int i=0; i< maxPlayers; i++)
{
if ((lobbySlots[i] != null))
{ // nullpointer here
lobbyPlayer = lobbySlots[i] as LobbyPlayer_custom;
if (lobbyPlayer.GetPlayerIP().Equals(ip))
{
return lobbyPlayer.GetPlayerUID();
}
}
}
return "null";
}
我还尝试通过以下方式查找所有 LobbyPlayerPrefab 实例来访问 LobbyPlayer_custom 脚本:
LobbyPlayer_custom lobbyPlayer;
Object[] objs = Resources.FindObjectsOfTypeAll(typeof(GameObject));
foreach (Object obj in objs)
{
if (obj.name.Contains("player") || obj.name.Contains("Player"))
{
lobbyPlayer = (obj as GameObject).GetComponent<LobbyPlayer_custom>();
//none of the lobbyPlayers methods are accessible
}
}
有人知道如何访问生成的 Prefab 的所有 LobbyPlayer 脚本吗?
非常感谢。
【问题讨论】: