photon 特定名のルームを作成するかそこに入室する

どうしても同じルームに入り,customRoomPropertiesを取得して,同じルームに入室している人全員に同じ設定を与えたかったので.

(1)まずルーム作成のスクリプト

これは空のゲームオブジェクトに当てる

using UnityEngine;
using System.Collections;
using Hashtable = ExitGames.Client.Photon.Hashtable; //require for generate hashtable

public class photonsystem : Photon.MonoBehaviour {
	public string objectName;//use for photon instantiate
	private bool connectFailed = false; //use for network status check

	//set player name
	private string playerName = "GuestAAA";//a player name
	//set room name
	private string roomName = "liub2";//liub2という専用のルーム名を作成

	void OnJoinedLobby() {
			Debug.Log("ロビーに入室");
			//ランダムにルームへ参加
		}


	//call when room list upadate "must use"
	void OnReceivedRoomListUpdate(){
		CreateRoom ();
	}


	/////create room with custom room options

	public void CreateRoom (){
		RoomOptions roomOptions = new RoomOptions ();
		roomOptions.isVisible = true;
		roomOptions.isOpen = true;
		roomOptions.maxPlayers = 50;//test 50 max players
		roomOptions.customRoomProperties = new ExitGames.Client.Photon.Hashtable (){{"CustomProperties", "111"},{"r", "blue"}};
		roomOptions.customRoomPropertiesForLobby = new string[] {"CustomProperties"};

		if (PhotonNetwork.GetRoomList ().Length == 0) {
			PhotonNetwork.CreateRoom (roomName, roomOptions, null);
			return;
		}
		foreach (RoomInfo roomInfo in PhotonNetwork.GetRoomList()) {
			if (roomInfo.name != roomName) {
				PhotonNetwork.CreateRoom (roomName, roomOptions, null);
			} else {
				//room name matched
				PhotonNetwork.JoinRoom(roomName);
			}
		}
	}






		/// 部屋に入るとき呼ばれます。
		/// これは参加する際だけでなく作成する際も含みます。
		void OnJoinedRoom() {
		Debug.Log("部屋に入室" + PhotonNetwork.room.name);

		//ゲームオブジェクト作成する時用
		//GameObject cube = PhotonNetwork.Instantiate(objectName, Vector3.zero, Quaternion.identity, 0);

		//make cube
		//GameObject player = PhotonNetwork.Instantiate("testobj", this.transform.position, this.transform.rotation, 0);
		}

		/// JoinRandom()の入室が失敗した場合に後に呼び出されます。
		void OnPhotonRandomJoinFailed() {
		//void OnPhotonJoinFailed() {
			Debug.Log("部屋入室失敗");
			//名前のないルームを作成
		//	PhotonNetwork.CreateRoom(null);

		/////以下はテスト用コメントアウトしています 
		//create custom property
	//	RoomOptions myOptions = new RoomOptions ();// require use custom property
	//	myOptions.isVisible = true;
	//	myOptions.isOpen = true;
	//	myOptions.maxPlayers = 50;
	//	myOptions.customRoomProperties = new ExitGames.Client.Photon.Hashtable (){{"CustomProperties", "111"},{"r", "0.2"}};
	//	myOptions.customRoomPropertiesForLobby = new string[] {"CustomProperties"};

		// ルームの作成
	//	PhotonNetwork.JoinOrCreateRoom(roomName, myOptions, null);


		}


		void Awake() {
			//マスターサーバーへ接続
			PhotonNetwork.ConnectUsingSettings("v0.1");


		if (PhotonNetwork.playerName==null)
		{
			//ランダムにプレイヤーの名前を生成
			this.playerName = "Guest" + UnityEngine.Random.Range(1, 9999);
			//Photonにプレイヤーを登録
			PhotonNetwork.playerName = this.playerName; 
		}else{
			//Photonにプレイヤーを登録
			this.playerName = PhotonNetwork.playerName;
		}

		}


	//部屋作成に成功したときにコール
	public void OnCreatedRoom(){
		Debug.Log("OnCreatedRoom");
	}
	//接続が切断されたときにコール
	public void OnDisconnectedFromPhoton(){
		Debug.Log("Disconnected from Photon.");
	}
	//接続失敗時にコール
	public void OnFailedToConnectToPhoton(object parameters){
		this.connectFailed = true;
		Debug.Log("OnFailedToConnectToPhoton. StatusCode: " + parameters + " ServerAddress: " + PhotonNetwork.networkingPeer.ServerAddress);
	}

	}

 

(2)そのカスタムプロパティを呼び出すスクリプト

これをライトにアサインしています

using UnityEngine;
using System.Collections;
using Hashtable = ExitGames.Client.Photon.Hashtable;
using UnityEngine.UI;

public class cpLight : MonoBehaviour {
	public Light lightObj2;
	private string cpstring = "red";

	// Use this for initialization
	void Start () {
		lightObj2 = GetComponent<Light>();
	}
	
	// Update is called once per frame
	void Update () {

	
	}


	//cp changed is here
	void OnPhotonCustomRoomPropertiesChanged(){
	
		if (PhotonNetwork.inRoom) {// ルームの状態を取得
			Room room = PhotonNetwork.room;
			if (room == null) {
				return;
			} else {
				Hashtable cp = room.customProperties;
				//cpstring = (string)cp ["r"];
				cpstring = (string)cp ["r"];
				//Debug.Log (cp ["r"]);
				if (cpstring == "blue") {
					lightObj2.color = Color.blue;
					//GetComponent<Light>.color = Color.blue;
				}
				if (cpstring == "red") {
					lightObj2.color = Color.red;
					//GetComponent<Light>.color = Color.blue;
				}
				if (cpstring == "green") {
					lightObj2.color = Color.green;
					//GetComponent<Light>.color = Color.blue;
				}
			}
		}
	
	}


}

 

(3)テスト用にボタンを作成し,そのボタンにあてるスクリプトです

using UnityEngine;
using System.Collections;
using Hashtable = ExitGames.Client.Photon.Hashtable;

public class lightColor : Photon.MonoBehaviour {

	public Light lightObj;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {


	//	if (!photonView.isMine) {
			//transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime * 5);
			//transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);


	//	}
	
	}

	public void colorRed(){
		//lightObj.color = Color.red;
	ExitGames.Client.Photon.Hashtable h = new ExitGames.Client.Photon.Hashtable() { { "r", "red" } };
	PhotonNetwork.room.SetCustomProperties(h);

	}


	public void colorGreen(){
	//	lightObj.color = Color.green;
		ExitGames.Client.Photon.Hashtable h = new ExitGames.Client.Photon.Hashtable() { { "r", "green" } };
		PhotonNetwork.room.SetCustomProperties(h);
	}

	public void colorBlue(){

		ExitGames.Client.Photon.Hashtable h = new ExitGames.Client.Photon.Hashtable() { { "r", "blue" } };
		PhotonNetwork.room.SetCustomProperties(h);
	//	lightObj.color = Color.blue;
	}

}

これで,ボタンをクリックすると

photon2

このアプリの画面は全て同じ色になります.タイムラグは1秒以内くらいです