facebook twitter hatena line email

「Unity/photon/pun1/rpc」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(RPCとは)
(引数型一覧)
行70: 行70:
  
 
==引数型一覧==
 
==引数型一覧==
byte, int, float, string, byte[], int[], float[],string[]
+
byte, int, float, string, byte[], int[], float[],string[]
Vecter3, Quaternion
+
Vecter3, Quaternion
 
引数タイプについて公式:https://doc.photonengine.com/ja/realtime/current/reference/serialization-in-photon
 
引数タイプについて公式:https://doc.photonengine.com/ja/realtime/current/reference/serialization-in-photon

2017年10月26日 (木) 17:52時点における版

RPCとは

リモートプロシージャコールというらしく、 情報をその部屋にいる、その他のメンバーに送信できます。

サンプル

  1. Roomに入るまでのスクリプトを作成しておく
  2. ヒエラルキーに"RpcView"というGameObjectを作成
  3. 以下RpcViewScript.csを作成し、RpcViewにAddComponent登録し実行する
  • RpcViewScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RpcViewScript : MonoBehaviour
{
	PhotonView  photonView    = null;
	void Awake()
	{
		photonView    = GetComponent<PhotonView>();
	}
	void Start()
	{
		int ownerID = photonView.ownerId;
	}
	void Update() {
		if (Input.GetKeyDown (KeyCode.Space)) {
			SpaceKey ();
		}
	}
	void SpaceKey()
	{
		photonView.RPC( "CustomMethod", PhotonTargets.All );
	}
	[PunRPC]
	private void CustomMethod()
	{
		Debug.Log ("CustomMethod");
	}
	void LeftArrowKey()
	{
		object[] args = new object[]{
			"hoge",
			new string[] {"taro", "jiro", "saburo"}
		};
		photonView.RPC("Custom2Method", PhotonTargets.All, args);
	}
	[PunRPC]
	private void Custom2Method(string str, string[] names)
	{
		Debug.Log ("CustomMethod " + str);
		foreach (string name in names) {
			Debug.Log ("CustomMethod name=" + name);
		}
	}
}

spaceキーや左矢印を押すと同じ部屋に入っている相手側にもメッセージが表示される

以下 PhotonTargets.All

メソッド実行だけだったり引数を付きでも送ることができる。

ターゲット一覧

  • Photontargets.All // 自分含めて全員へ(自分への送信はPhotonサーバを経由しない)
  • Photontargets.Others // 自分以外全員へ
  • Photontargets.MasterClient // 部屋のホストにのみ送る
  • Photontargets.AllViaServer // 自分含めて全員へ(自分への送信も一旦Photonサーバを経由する)
  • Photontargets.AllBufferedViaServer // 自分含めて全員へ(自分への送信も一旦Photonサーバを経由する、後で部屋に入ってきたメンバーにも送られる)
  • Photontargets.AllBuffered // 自分含めて全員へ(自分への送信はPhotonサーバを経由しない、後で部屋に入ってきたメンバーにも送られる)
  • Photontargets.OthersBuffered // 自分以外全員へ(後で部屋に入ってきたメンバーにも送られる)

引数型一覧

byte, int, float, string, byte[], int[], float[],string[]
Vecter3, Quaternion

引数タイプについて公式:https://doc.photonengine.com/ja/realtime/current/reference/serialization-in-photon