Help us understand the problem. What is going on with this article?

Photon Cloud × Unity GameJam 事前勉強 - RPC

More than 5 years have passed since last update.

RPCとは

http://e-words.jp/w/RPC.html

簡単に言うと全ての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);
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした