你好,我已经解决这个问题3天了,我无法弄清楚它有什么问题。我收到的错误消息是“Player(Clone) (UnityEngine.GameObject) 的 SpawnObject,NetworkServer 未处于活动状态。没有活动服务器就无法生成对象。” 我知道服务器启动正常,因为当我用客户端连接到它然后关闭服务器时,客户端说服务器可能已关闭。这是我到目前为止的代码。 先感谢您
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
public class MENU_CONTROLLER : NetworkBehaviour
{
public GameObject ipField;
public GameObject connectButton;
public GameObject hostButton;
public GameObject player;
public Text ip;
public Text PlayerCount;
private int playercount = 0;
void Update() {
}
void vanishMenu() {
ipField.SetActive(false);
connectButton.SetActive(false);
hostButton.SetActive(false);
}
public void ServerConnect() {
Network.Connect(ip.text.ToString(), 4444);
vanishMenu();
SpawnPlayer();
}
public void SpawnPlayer() {
GameObject p = (GameObject)Instantiate(player, transform.position,transform.rotation);
NetworkServer.Spawn(p);
}
public void ServerStart() {
Network.InitializeServer(30, 4444, true);
Network.Connect("localhost", 4444);
SpawnPlayer();
}
void OnPlayerConnected(NetworkPlayer player) {
playercount++;
print("PLAYER CONNECTED");
print(player.ipAddress);
PlayerCount.text = playercount.ToString();
}
}