LoginSignup
19
19

More than 5 years have passed since last update.

【Photon】RPCの使い方

Posted at

RPC

remote procedure call
ネットワーク上の別の端末の関数等を呼び出す仕組み。

Unityドキュメントより
「RPC は高い信頼性で送信され、順序付けられます」
http://docs.unity3d.com/ja/current/Manual/class-NetworkView.html

呼び出し方

GameObjectの構成

GameObject
    PhotonView … RPC呼び出しの橋渡し
    Photon.MonoBehaviour継承のクラス … RPCメソッドを実装する

RPCメソッドの実装

public class TestRPC : Photon.MonoBehaviour
{
    [PunRPC]
    public void RPCFunc(string msg, byte[] data, PhotonMessageInfo info)
    {
        Debug.Log("Message : " + msg);
        string dataStr = "";
        foreach(byte b in data) dataStr += b + " ";
        Debug.Log("Data : " + dataStr);
    }
}

※PUN ver1.56からは [RPC] が使えなくなっています。代わりに[PunRPC]を使います。
http://forum.unity3d.com/threads/photon-unity-networking.101734/page-17

v1.56 (16. June 2015)
Changed: The RPC attribute got replaced by PunRPC.
Search and replace your code for [RPC] and replace it with PunRPC.
The change is necessary, because the RPC attribute is obsolete in Unity 5.1 and that causes a large amount of warnings at compile time.
The new PunRPC must be used in all versions of Unity, starting with this PUN version.

RPCメソッドの呼び出し

マスタークライアントは即座にメソッドが呼び出されるが、
ネットワーク越しのプレイヤーには当然呼び出されるまでにラグが発生する。

// RPCを呼び出す
{
    PhotonView view;    // TestRPC + PhotonViewを持つGameObjectから取得する

    // RPCメソッドの引数 object[] の配列にする
    object[] args = new object[]{
            "RPC message",          // 第1引数 : string msg
            new byte[] {1, 2, 3}    // 第2引数 : byte[] data
            };

    // RPCメソッドの名前、引数を合わせる
    view.RPC(
        "RPCFunc",                  // メソッド名
        Photontargets.All,          // ネットワークプレイヤー全員に対して呼び出す
        args);                      // 引数
}

RPCの引数に使える型

  • byte, int, float 等の値型
  • byte[], int[], float[] 上記値型の配列
  • string, string[]
  • Vecter3, Quaternion

また、以下のドキュメントにPhotonでシリアライズ可能な型の一覧があります。
確認してないですが、おそらく可能だと思われます。
http://doc.exitgames.com/ja/realtime/current/reference/serialization-in-photon

19
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19