我开始一个新游戏,然后尝试在其上实现网络。 我启动服务器,可以将客户端连接到该服务器,但是,我想获取网络客户端ipadress。 但这给了我超出范围的错误
有问题的错误就在网上 clientip = Network.connections [0] .ipAddress;
这是错误
IndexOutOfRangeException:数组索引超出范围。 菜单。更新()(位于Assets / net / menu.cs:72)
这是我当前的代码
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class menu : MonoBehaviour {
public string IP= "127.0.0.1";
public int Port = 25001;
public bool switchoffnetwork;
public GameObject btnon;
public GameObject btnoff;
public string username = "wizard";
public int idst = 0;
public string ID;
public bool IsClientConnectx= false;
public bool IsServerConnectx= false;
public string clientip;
void OnGUI ()
{
if (switchoffnetwork == true)
{
if (Network.peerType == NetworkPeerType.Disconnected) {
if (GUI.Button (new Rect (100, 100, 100, 25), "Start Client")) {
Network.Connect (IP, Port);
}
if (GUI.Button (new Rect (100, 125, 100, 25), "Start Server")) {
Network.InitializeServer (10, Port);
}
} else {
if (Network.peerType == NetworkPeerType.Client) {
GUI.Label (new Rect (100, 100, 100, 25), "Connect Player");
if (GUI.Button (new Rect (100, 125, 100, 25), "Logout")) {
Network.Disconnect (250);
}
if (GUI.Button (new Rect (100, 150, 140, 25), "Disconnect Player"))
if (Network.connections.Length > 0) {
Debug.Log("Disconnecting: " + Network.connections[0].ipAddress + ":" + Network.connections[0].port);
Network.CloseConnection(Network.connections[0], true);
}
}
if (Network.peerType == NetworkPeerType.Server) {
GUI.Label (new Rect (100, 100, 100, 25), "Server");
GUI.Label (new Rect (100, 125, 100, 25), "Connections: " + Network.connections.Length);
if (GUI.Button (new Rect (100, 150, 100, 25), "Logout")) {
Network.Disconnect (250);
}
}
}
}
}
public void onswitch()
{
switchoffnetwork = true;
btnon.SetActive (false);
btnoff.SetActive (true);
}
public void onswitchbtn()
{
switchoffnetwork = false;
btnon.SetActive (true);
btnoff.SetActive (false);
}
void Update ()
{
clientip = Network.connections[0].ipAddress;
if (Network.peerType == NetworkPeerType.Disconnected)
{
Debug.Log("Player Have disconected" + clientip);
}
}
}