0
我想產生一個客戶端和服務器都可以看到的彈跳球。 當客戶端連接並且下一個場景(球彈起時)被加載時,必須產生球。當客戶端連接時產卵球
問題是,方法OnPlayerConnected()不會被調用,有沒有人知道任何替代方案,爲什麼它不工作?
public class MainMenu : MonoBehaviour {
public string connectionIP = "127.0.0.1";
public int connectionPort = 25001;
bool isClientConnected = false;
void Awake()
{
// Make sure list is empty and request a new list
MasterServer.RequestHostList("MyUniqueGameType");
}
// Use this for initialization
void OnGUI()
{
HostData[] data = MasterServer.PollHostList();
// Go through all the hosts in the host list
foreach (var element in data)
{
//Debug.Log ("server found");
GUILayout.BeginHorizontal();
var name = element.gameName + " " + element.connectedPlayers + "/" + element.playerLimit;
GUILayout.Label (name);
GUILayout.Space (5);
string hostInfo;
hostInfo = "[";
foreach (var host in element.ip)
hostInfo = hostInfo + host + ":" + element.port + " ";
hostInfo = hostInfo + "]";
GUILayout.Label (hostInfo);
GUILayout.Space (5);
GUILayout.Label (element.comment);
GUILayout.Space (5);
GUILayout.FlexibleSpace();
if (GUI.Button (new Rect (10, 30, 120, 20), "Client Connect"))
{
Network.Connect (connectionIP, connectionPort);
isClientConnected = true;
Application.LoadLevel ("scene1");
// Debug.Log ("spawning ball");
// SpawnBall();
}
GUILayout.EndHorizontal();
}
if (GUI.Button (new Rect (10, 50, 120, 20), "Initialize Server"))
{
Network.InitializeServer (2, connectionPort, !Network.HavePublicAddress());
// Network.InitializeServer (2, 25002, !Network.HavePublicAddress());
MasterServer.RegisterHost ("MyUniqueGameType", "JohnDoes game", "l33t game for all");
/*
while(!isClientConnected)
{
Debug.Log("waiting for client");
}
*/
Application.LoadLevel("scene1");
}
}
void OnPlayerConnected(NetworkPlayer player)
{
Debug.Log ("OnPlayerConnected");
SpawnBall();
}
void SpawnBall()
{
GameObject objPrefab = Resources.Load("Ball") as GameObject;
GameObject obj = Instantiate(objPrefab) as GameObject;
}
非常感謝!它在沒有RPC的情況下僅在客戶端上運行,但現在我使用RPC時,我現在得到錯誤: 由於沒有啓動連接,無法發送RPC功能。 UnityEngine.NetworkView:RPC(String,RPCMode,Object []) SpawnBall:Start()(在Assets/scripts/SpawnBall.cs:11) 兩個玩家都可以移動並可以看到彼此移動,所以沒有任何問題連接我猜.. – PrisonMike 2014-09-19 23:55:12
沒關係,我已經將loadlevel放入連接的服務器上的方法,它現在可以工作 – PrisonMike 2014-09-20 10:05:23