当我忙于为服务器执行命令时,我收到了来自 Unity 的错误。错误是:
在服务器上调用的命令函数 CmdSpawnPlayer。
UnityEngine.Debug:LogError(对象)
NetworkHandler:CallCmdSpawnPlayer(Boolean) NetworkHandler:OnEnable() (在 Assets/Scripts/Managers/NetworkHandler.cs:69)
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
特此我使用的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
public class NetworkHandler : NetworkBehaviour
{
public Player NetworkPlayer
{
get;
private set;
}
public bool HasInternet
{
get
{
return Network.HavePublicAddress();
}
}
public bool IsConnectedToServer
{
get
{
return networkManager.isNetworkActive;
}
}
[SerializeField]
private NetworkManager networkManager;
[SerializeField]
private GameObject playerPrefab, spectatorPrefab;
public void StartClient()
{
networkManager.StartClient();
}
public void StartServer()
{
networkManager.StartServer();
}
[Command]
private void CmdSpawnPlayer(bool isSpectator)
{
SpawnPlayer(isSpectator);
}
private void SpawnPlayer(bool isSpectator)
{
if (isSpectator)
{
NetworkServer.SpawnWithClientAuthority(Instantiate(spectatorPrefab), connectionToClient);
}
else
{
NetworkServer.SpawnWithClientAuthority(Instantiate(playerPrefab), connectionToClient);
}
}
private void OnEnable()
{
if (IsConnectedToServer)
{
CmdSpawnPlayer(true);
Game.CameraHandler.InstantiateMode();
}
}
}
Unity 版本使用的是 5.3.4f1。
我希望你能帮我解决这个问题。
斯基特吕尔