C# Unity 5.5.0f3-使用ClientRpc抛出更新对象;找不到Rpc调用的行为…“;
我正在做一场团队比赛。每个团队都有自己的后代,目标是摧毁其他团队的后代。产卵以3个对象的形式在网络中设置-一个实际产卵(稍后称为物理产卵),仅对服务器可见,一个名为MapManager的对象同时存在于客户端和服务器中,它通过Rpc调用和一个简单的spawn图形(后面称为可视化spawn)充当信使,出现在客户端和服务器的本地播放器上C# Unity 5.5.0f3-使用ClientRpc抛出更新对象;找不到Rpc调用的行为…“;,c#,unity3d,unity-networking,C#,Unity3d,Unity Networking,我正在做一场团队比赛。每个团队都有自己的后代,目标是摧毁其他团队的后代。产卵以3个对象的形式在网络中设置-一个实际产卵(稍后称为物理产卵),仅对服务器可见,一个名为MapManager的对象同时存在于客户端和服务器中,它通过Rpc调用和一个简单的spawn图形(后面称为可视化spawn)充当信使,出现在客户端和服务器的本地播放器上 public class SpawnerServer : NetworkBehaviour { //used to determine whether has
public class SpawnerServer : NetworkBehaviour {
//used to determine whether has the health changed
bool isHealthChanged = false;
//indicates the team of this spawner via enum state
public Data.teams myTeamEnum;
...
public RectTransform healthBar;
private void OnTriggerEnter2D(Collider2D collision)
{
//Checking whether the bullet wll damage the spawn
OnChangeHealth(healthPoints);
}
//if the health changed, change the healthbar as well
public void OnChangeHealth(float healthPoints)
{
if (isServer)
{
...
isHealthChanged = true;
}
}
//returns the deltaSize of the healthbar
public Vector2 GetHealthbarSizeDelta()
{
return healthBar.sizeDelta;
}
//switches the healthChanged bool back to false. Called by the MapManager
public void UncheckHealthChanged()
{
isHealthChanged = false;
}
//returns the state of the healthChanged bool
public bool IsHealthChanged()
{
return isHealthChanged;
}
}
public class ClientSpawnControl : NetworkBehaviour {
//the references to scripts of the server spawns, required for checking whether has
//the state of any of the spawns changed
public SpawnerServer serverSpawn1;
public SpawnerServer serverSpawn2;
//the references to spawns that belong to the clients
public SpawnerClient clientSpawn1;
public SpawnerClient clientSpawn2;
//acts as a messenger that forces change of spawn healthbars in clients
[ClientRpc]
private void RpcChangeSpawnersHealthClients(int whatTeam, float healthChangeDelta)
{
if(clientSpawn1 != null)
{
if (whatTeam == (int)clientSpawn1.myTeam)
{
clientSpawn1.OnChangeHealth(healthChangeDelta);
return;
}
}
if(clientSpawn2 != null)
{
if (whatTeam == (int)clientSpawn2.myTeam)
{
clientSpawn2.OnChangeHealth(healthChangeDelta);
return;
}
}
}
// Update is called once per frame
void Update () {
if (!isServer)
return;
if(serverSpawn1.IsHealthChanged())
{
serverSpawn1.UncheckHealthChanged();
RpcChangeSpawnersHealthClients((int)serverSpawn1.myTeamEnum, serverSpawn1.GetHealthbarSizeDelta().x);
}
if (serverSpawn2.IsHealthChanged())
{
serverSpawn2.UncheckHealthChanged();
RpcChangeSpawnersHealthClients((int)serverSpawn2.myTeamEnum, serverSpawn2.GetHealthbarSizeDelta().x);
}
}
每个对象的网络标识设置分别为:
- 仅限服务器
- 本地玩家管理局
- 无-是一种单一行为
public class SpawnerServer : NetworkBehaviour {
//used to determine whether has the health changed
bool isHealthChanged = false;
//indicates the team of this spawner via enum state
public Data.teams myTeamEnum;
...
public RectTransform healthBar;
private void OnTriggerEnter2D(Collider2D collision)
{
//Checking whether the bullet wll damage the spawn
OnChangeHealth(healthPoints);
}
//if the health changed, change the healthbar as well
public void OnChangeHealth(float healthPoints)
{
if (isServer)
{
...
isHealthChanged = true;
}
}
//returns the deltaSize of the healthbar
public Vector2 GetHealthbarSizeDelta()
{
return healthBar.sizeDelta;
}
//switches the healthChanged bool back to false. Called by the MapManager
public void UncheckHealthChanged()
{
isHealthChanged = false;
}
//returns the state of the healthChanged bool
public bool IsHealthChanged()
{
return isHealthChanged;
}
}
public class ClientSpawnControl : NetworkBehaviour {
//the references to scripts of the server spawns, required for checking whether has
//the state of any of the spawns changed
public SpawnerServer serverSpawn1;
public SpawnerServer serverSpawn2;
//the references to spawns that belong to the clients
public SpawnerClient clientSpawn1;
public SpawnerClient clientSpawn2;
//acts as a messenger that forces change of spawn healthbars in clients
[ClientRpc]
private void RpcChangeSpawnersHealthClients(int whatTeam, float healthChangeDelta)
{
if(clientSpawn1 != null)
{
if (whatTeam == (int)clientSpawn1.myTeam)
{
clientSpawn1.OnChangeHealth(healthChangeDelta);
return;
}
}
if(clientSpawn2 != null)
{
if (whatTeam == (int)clientSpawn2.myTeam)
{
clientSpawn2.OnChangeHealth(healthChangeDelta);
return;
}
}
}
// Update is called once per frame
void Update () {
if (!isServer)
return;
if(serverSpawn1.IsHealthChanged())
{
serverSpawn1.UncheckHealthChanged();
RpcChangeSpawnersHealthClients((int)serverSpawn1.myTeamEnum, serverSpawn1.GetHealthbarSizeDelta().x);
}
if (serverSpawn2.IsHealthChanged())
{
serverSpawn2.UncheckHealthChanged();
RpcChangeSpawnersHealthClients((int)serverSpawn2.myTeamEnum, serverSpawn2.GetHealthbarSizeDelta().x);
}
}
MapManager检查物理生成中的标志状态(仅在服务器上检查)以及检测到真值时的标志状态:
- 将标志重置为
public class SpawnerServer : NetworkBehaviour {
//used to determine whether has the health changed
bool isHealthChanged = false;
//indicates the team of this spawner via enum state
public Data.teams myTeamEnum;
...
public RectTransform healthBar;
private void OnTriggerEnter2D(Collider2D collision)
{
//Checking whether the bullet wll damage the spawn
OnChangeHealth(healthPoints);
}
//if the health changed, change the healthbar as well
public void OnChangeHealth(float healthPoints)
{
if (isServer)
{
...
isHealthChanged = true;
}
}
//returns the deltaSize of the healthbar
public Vector2 GetHealthbarSizeDelta()
{
return healthBar.sizeDelta;
}
//switches the healthChanged bool back to false. Called by the MapManager
public void UncheckHealthChanged()
{
isHealthChanged = false;
}
//returns the state of the healthChanged bool
public bool IsHealthChanged()
{
return isHealthChanged;
}
}
public class ClientSpawnControl : NetworkBehaviour {
//the references to scripts of the server spawns, required for checking whether has
//the state of any of the spawns changed
public SpawnerServer serverSpawn1;
public SpawnerServer serverSpawn2;
//the references to spawns that belong to the clients
public SpawnerClient clientSpawn1;
public SpawnerClient clientSpawn2;
//acts as a messenger that forces change of spawn healthbars in clients
[ClientRpc]
private void RpcChangeSpawnersHealthClients(int whatTeam, float healthChangeDelta)
{
if(clientSpawn1 != null)
{
if (whatTeam == (int)clientSpawn1.myTeam)
{
clientSpawn1.OnChangeHealth(healthChangeDelta);
return;
}
}
if(clientSpawn2 != null)
{
if (whatTeam == (int)clientSpawn2.myTeam)
{
clientSpawn2.OnChangeHealth(healthChangeDelta);
return;
}
}
}
// Update is called once per frame
void Update () {
if (!isServer)
return;
if(serverSpawn1.IsHealthChanged())
{
serverSpawn1.UncheckHealthChanged();
RpcChangeSpawnersHealthClients((int)serverSpawn1.myTeamEnum, serverSpawn1.GetHealthbarSizeDelta().x);
}
if (serverSpawn2.IsHealthChanged())
{
serverSpawn2.UncheckHealthChanged();
RpcChangeSpawnersHealthClients((int)serverSpawn2.myTeamEnum, serverSpawn2.GetHealthbarSizeDelta().x);
}
}