How to call PunRPC function on button click Unity (Photon Networking)

时间:2018-06-04 16:44:24

标签: photon

i am making multiplayer game using Photon Networking. I have a PunRPC function that changes color of my NetworkPlayer and other players can see this change. when i call this function in Update method as `

if (Input.GetKeyDown(KeyCode.P))
        {
            if (this.GetComponent<PhotonView>().isMine)
            {
                this.GetComponent<PhotonView>().RPC("SendColor", PhotonTargets.All);
            }
        }

this code works fine. now i want to call this function when user clicks button

1 个答案:

答案 0 :(得分:0)

我是光子的新手,所以我可能不在这里。但是,如果您将rpc设置为单独的方法,然后在按下按钮时调用该方法,则应该可以使用。即

public void sendColor(){
 if (this.GetComponent<PhotonView>().isMine)
            {
                this.GetComponent<PhotonView>().RPC("SendColor", PhotonTargets.All);
            }
}

然后将您将脚本附加到的对象拖放到“单击时”按钮,并引用此方法,它应该可以工作

我确定这确实是倒数,但是我的2美分