0

我对 PunRpc 调用有点困惑,我尝试了 PhotonTarget.Others 看看它是否会健康——;在其他客户端上,但它没有工作。

此刻我想知道我怎样才能做到健康——;当我按空格键时在其他客户端上。

这是我尝试过的:这是命令:

photonView.RPC("healthReduction", PhotonTargets.Others, null);

这是 RPC

    [PunRPC]
void healthReduction()
{
    health--;
    Debug.Log("Health--");
}

但它仍然无法正常工作。

4

2 回答 2

0

我通过使用一个实例并使用 2 个不同的命名脚本解决了这个问题,例如,如果我想从 PlayerManager 访问 PlayerManager1 脚本,我会这样做PlayerManager1.Instance.photonView.RPC("reduceMyHealth",PhotonTargets.All,null)

这将调用 PlayerManager1 中的 reduceMyHealth() PunRpc。

为此,您必须将此代码添加到要访问的脚本中: static public PlayerManager1 Instance;并在 start(){}Instance = this;

于 2016-10-05T14:10:03.890 回答
0

我认为这很容易解决。源代码如下。

public class TestPhoton : Photon.PunBehaviour
{
    public PhotonView gameView;

    void Start()
    {
        gameView = this.GetComponent<PhotonView>();
    }

    public void OnClickTest()
    {
        photonView.RPC("HealthReduction", PhotonTargets.Others);
    }

    [PunRPC]
    public void HealthReduction()
    {
        health--;
    }
}
于 2018-12-13T14:29:29.430 回答