简体   繁体   English

unity Photon PUN RPC 调用

[英]Unity Photon PUN RPC call

I have a question about RPC and Photon PUN.我有一个关于 RPC 和 Photon PUN 的问题。

I have 1 GameObject in my game that calls an RPC method.我的游戏中有 1 个调用 RPC 方法的 GameObject。 This game object contains a C# script (that contains the RPC method) and the PhotonView component with/without (is should be the same) the observed script and the ObserveOption equals to Off.这个游戏对象包含一个 C# 脚本(包含 RPC 方法)和带有/不带(应该是相同的)观察脚本的 PhotonView 组件,并且 ObserveOption 等于 Off。

I have a character which have to shoot to a cube.我有一个角色必须射击到一个立方体。 This cube has the previously described script and component attach to it.此多维数据集具有先前描述的脚本和附加到其的组件。

The RPC is sent once (when the cube has been collided with a projectile) but in my scene I have many Cube which should receive the message because I have to decrease the health of the hit cube. RPC 发送一次(当立方体与射弹碰撞时),但在我的场景中,我有许多 Cube 应该接收消息,因为我必须降低命中立方体的健康状况。 The problem is that only 1 Cube receive the RPC call (The RPC is caught by the first instantiated object at the start of the game) but the RPC should be received by all the cube (is that right?) in my scene.问题是只有 1 个 Cube 接收到 RPC 调用(RPC 在游戏开始时被第一个实例化的对象捕获),但是我的场景中的所有立方体都应该接收到 RPC(对吗?)。

The cubes are exactly the same but they have a different photonView ID (rightly) so we could check whether the hit cube to which decrease the health is the right one.立方体完全相同,但它们具有不同的光子视图 ID(正确),因此我们可以检查降低生命值的命中立方体是否正确。

This is the significant part of the code:这是代码的重要部分:

public static void reduceHealthRPC(float damage, int viewID)    
{         
    photonView.RPC("reduceHealth", PhotonTargets.All, damage, viewID);    
}

[RPC]
public void reduceHealth(float damage, int viewID)
{
    if(this._viewID != viewID) 
    {
        Debug.Log ("The view ID is not mine. My viewID is: " + _viewID + ", and the one which is coming form RPC is: " + viewID);
        return;
    }

    currentHealth -= damage;
    Debug.Log("My life is: " + currentHealth);
}

Please help me to unerstand what I'm doing wrong.请帮助我了解我做错了什么。 Thank you.谢谢你。

If you need call RPC for all cubes, do it for every cube.如果您需要为所有多维数据集调用 RPC,请为每个多维数据集执行此操作。 Currently you are calling RPC only for object referenced by static 'photonView' variable.目前,您只为静态“photonView”变量引用的对象调用 RPC。 Depending on how it's initialized, RPC called on that object.根据它的初始化方式,RPC 在该对象上调用。

我可能会迟到,但请尝试PhotonTargets.AllBuffered ,这将使 RPC 调用在稍后加入的玩家上运行。

Might be super late but have you tried this?可能会超级晚,但你试过这个吗? cube.cs is attached to all cubes. cube.cs附加到所有多维数据集。 in main.csmain.cs

cube[] allCubes = FindObjectsOfType<cube>();

for (int x = 0; x < allCubes.length; x++)
allCubes[x].reduceHealthRPC(dam,id);

this will send the code to all cubes with one target id, if this is your goal but you are much better to just rpc on the individual cube.这会将代码发送到具有一个目标 id 的所有多维数据集,如果这是您的目标,但您最好只对单个多维数据集进行 rpc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM