ClientRpc 函数未在 Unity3d c# 中的所有客户端上执行
ClientRpc Function not being carried out on all clients in Unity3d c#
目前我正在开发一款联网的 2d 平台游戏。我有一个脚本应该实例化名为 JetpackManager 的玩家喷气背包。然而,当玩家被生成到场景中时,代码只会将喷气背包生成到主机场景中,而不是生成到客户端场景中。这导致只有主机场景正常工作,而客户场景中的所有玩家都没有喷气背包。这是我的 JetpackManager 代码:
using UnityEngine;
using UnityEngine.Networking;
public class JetpackManager : NetworkBehaviour {
[SerializeField]
private Jetpack[] jetpacks;
[SerializeField]
private Transform jetPackHold;
private PlayerController playerController;
private Jetpack currentJetpack;
void Start(){
playerController = GetComponent<PlayerController> ();
if (isLocalPlayer) {
CmdEquipJetpack (0);
}
}
[Command]
void CmdEquipJetpack(int jetpackNumber){
RpcEquipJetpack (jetpackNumber);
}
[ClientRpc]
void RpcEquipJetpack(int jetpackNumber){
if (currentJetpack != null) {
Destroy (currentJetpack.gameObject);
}
currentJetpack = Instantiate (jetpacks[jetpackNumber], jetPackHold.position, jetPackHold.rotation);
currentJetpack.transform.SetParent (jetPackHold);
playerController.InitialiseJetpackVariables (currentJetpack);
}
}
所以基本上我的问题是 RpcEquipJetpack
函数中的代码出于某种原因只在主机上调用,而不是在任何客户端上调用。
您应该使用网络 class
实例化网络对象
Network instantiate a prefab.
The given prefab will be instanted on all clients in the game.
Synchronization is automatically set up so there is no extra work
involved. The position, rotation and network group number are given as
parameters. Note that in the example below there must be something set
to the playerPrefab in the Editor. You can read more about
instantiations in the object reference Object.Instantiate.
目前我正在开发一款联网的 2d 平台游戏。我有一个脚本应该实例化名为 JetpackManager 的玩家喷气背包。然而,当玩家被生成到场景中时,代码只会将喷气背包生成到主机场景中,而不是生成到客户端场景中。这导致只有主机场景正常工作,而客户场景中的所有玩家都没有喷气背包。这是我的 JetpackManager 代码:
using UnityEngine;
using UnityEngine.Networking;
public class JetpackManager : NetworkBehaviour {
[SerializeField]
private Jetpack[] jetpacks;
[SerializeField]
private Transform jetPackHold;
private PlayerController playerController;
private Jetpack currentJetpack;
void Start(){
playerController = GetComponent<PlayerController> ();
if (isLocalPlayer) {
CmdEquipJetpack (0);
}
}
[Command]
void CmdEquipJetpack(int jetpackNumber){
RpcEquipJetpack (jetpackNumber);
}
[ClientRpc]
void RpcEquipJetpack(int jetpackNumber){
if (currentJetpack != null) {
Destroy (currentJetpack.gameObject);
}
currentJetpack = Instantiate (jetpacks[jetpackNumber], jetPackHold.position, jetPackHold.rotation);
currentJetpack.transform.SetParent (jetPackHold);
playerController.InitialiseJetpackVariables (currentJetpack);
}
}
所以基本上我的问题是 RpcEquipJetpack
函数中的代码出于某种原因只在主机上调用,而不是在任何客户端上调用。
您应该使用网络 class
实例化网络对象Network instantiate a prefab.
The given prefab will be instanted on all clients in the game. Synchronization is automatically set up so there is no extra work involved. The position, rotation and network group number are given as parameters. Note that in the example below there must be something set to the playerPrefab in the Editor. You can read more about instantiations in the object reference Object.Instantiate.