facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ロビーと部屋に入る)
(ロビーと部屋に入る)
行46: 行46:
 
  Debug.Log ("ルームが一つもありません");
 
  Debug.Log ("ルームが一つもありません");
 
  } else {
 
  } else {
  for (int i = 0; i < rooms.Length; i++) {
+
  foreach (RoomInfo room in rooms) {
  Debug.Log ("RoomName:" + rooms [i].name);
+
  Debug.Log ("RoomName:"   + room.name);
  Debug.Log ("userName:" + rooms[i].customProperties["userName"]);
+
  Debug.Log ("userName:" + room.customProperties["userName"]);
  Debug.Log ("userId:" + rooms[i].customProperties["userId"]);
+
  Debug.Log ("userId:"   + room.customProperties["userId"]);
 
  }
 
  }
 
  }
 
  }

2017年10月15日 (日) 05:25時点における版

ロビーと部屋に入る

  1. ヒエラルキーにGameObjectを作成して
  2. 以下csを貼り付ける

PhotonLobby.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhotonLobby : Photon.MonoBehaviour {
	// Use this for initialization
	void Start () {
		PhotonNetwork.ConnectUsingSettings("v1.0");
	}
	// Update is called once per frame
	void Update () {
	}
	void OnJoinedLobby ()
	{
		Debug.Log ("OnJoinedLobby");
		CreateRoom ();
	}
	public void CreateRoom(){
		string userName = "ユーザ1";
		string userId = "user1";
		PhotonNetwork.autoCleanUpPlayerObjects = false;
		ExitGames.Client.Photon.Hashtable customProp = new ExitGames.Client.Photon.Hashtable();
		customProp.Add ("userName", userName);
		customProp.Add ("userId", userId);
		PhotonNetwork.SetPlayerCustomProperties(customProp);
		RoomOptions roomOptions = new RoomOptions ();
		roomOptions.customRoomProperties = customProp;
		roomOptions.customRoomPropertiesForLobby = new string[]{ "userName","userId"};
		roomOptions.maxPlayers = 2; // 部屋の最大人数
		roomOptions.isOpen = true; // 入室許可する
		roomOptions.isVisible = true; // ロビーから見えるようにする
		PhotonNetwork.JoinOrCreateRoom (userId, roomOptions, null);
	}
	// ルーム入室した時
	void OnJoinedRoom() {
		Debug.Log ("OnJoinedRoom");
	}
	// ルーム一覧が取れた時
	void OnReceivedRoomListUpdate(){
		RoomInfo[] rooms = PhotonNetwork.GetRoomList();
		if (rooms.Length == 0) {
			Debug.Log ("ルームが一つもありません");
		} else {
			foreach (RoomInfo room in rooms) {
				Debug.Log ("RoomName:"   + room.name);
				Debug.Log ("userName:" + room.customProperties["userName"]);
				Debug.Log ("userId:"   + room.customProperties["userId"]);
			}
		}
	}
}

接続を検証するために1つはプレビュー版、もう一つはアプリにビルドしたものを動作させておく。

参考

http://sleepnel.hatenablog.com/entry/2016/05/29/120200