16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Photon Cloud × Unity GameJam 事前勉強 - RPC

Last updated at Posted at 2013-06-26

RPCとは

簡単に言うと全てのPCで同じメソッドを実行する仕組みですね
この機能自体はUnityのRPCを使用しているので調べる時は「Unity RPC」で調べてみるといいかもしれません

サンプルとしてある「DemoBoxes」の OnAwakePhysicsSettings.cs にコードを追加して試してみました

Screen Shot 2013-06-26 at 12.42.46.png

OnAwakePhysicsSettings.cs
using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(PhotonView))]
public class OnAwakePhysicsSettings : Photon.MonoBehaviour
{

	// this tiny script "disables" the rigidbody for remotely controlled GameObjects (owned by someone else)
	void Awake ()
	{
		if (!this.photonView.isMine) {
			Rigidbody attachedRigidbody = this.GetComponent<Rigidbody> ();
			if (attachedRigidbody != null) {
				attachedRigidbody.isKinematic = true;
			}
		}
	}

	///追加
	void OnMouseDown ()
	{
		// Boxをクリックしたら色が変わる
		photonView.RPC ("ChangeColor", PhotonTargets.All);
	}
	///追加
	[RPC]
	void ChangeColor ()
	{
		renderer.material.color = Color.red;
	}
}

チャットの仕組み

RPCを理解していれば簡単に作成出来ます

ChatSample.cs
using UnityEngine;
using System.Collections.Generic;

[RequireComponent(typeof(PhotonView))]
public class ChatSample : Photon.MonoBehaviour
{

	List<string> list = new List<string> ();
	string message = "";

	void OnGUI ()
	{
		//メッセージ入力
		message = GUILayout.TextField (message);
		
		//メッセージ送信
		if (GUILayout.Button ("Send")) {
			photonView.RPC ("AddMessage", PhotonTargets.All, message);
			message = "";
		}
		
		//メッセージ表示
		foreach (var text in list) {
			GUILayout.Label (text);
		}
	}

	[RPC]
	void AddMessage (string text)
	{
		list.Add (text);
	}
}
16
16
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
16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?