facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==ロビーと部屋に入る== #ヒエラルキーにGameObjectを作成して #以下csを貼り付ける PhotonLobby.cs using System.Collections; using System.Colle...」)
 
(ランダムで部屋に入る)
 
(同じ利用者による、間の23版が非表示)
行1: 行1:
 
==ロビーと部屋に入る==
 
==ロビーと部屋に入る==
 +
#importしたプロジェクト内のPhotonUnityNetwork/Resource/PhotonServerSettingのInspectorを開く
 +
#AutoJoinLobbyにチェックを入れる
 
#ヒエラルキーにGameObjectを作成して
 
#ヒエラルキーにGameObjectを作成して
#以下csを貼り付ける
+
#以下LobbyScene.csを貼り付ける
 
+
#以下PhotonManager.csをシングルトンで作成
PhotonLobby.cs
+
LobbyScene.cs
 
  using System.Collections;
 
  using System.Collections;
 
  using System.Collections.Generic;
 
  using System.Collections.Generic;
 
  using UnityEngine;
 
  using UnityEngine;
  public class PhotonLobby : Photon.MonoBehaviour {
+
  public class LobbyScene : MonoBehaviour {
 +
PhotonManager manager;
 
  // Use this for initialization
 
  // Use this for initialization
 
  void Start () {
 
  void Start () {
 +
manager = PhotonManager.Instance;
 +
  manager.userId = "user1";
 +
  manager.roomId = "room1";
 +
manager.Start ();
 +
}
 +
// Update is called once per frame
 +
void Update () {
 +
}
 +
}
 +
 +
PhotonManager.cs
 +
using System.Collections;
 +
using System.Collections.Generic;
 +
using UnityEngine;
 +
public class PhotonManager : Photon.MonoBehaviour {
 +
private static PhotonManager mInstance;
 +
public string userId = "user1";
 +
public string roomId = "room1";
 +
private PhotonManager () {
 +
}
 +
public static PhotonManager Instance {
 +
get {
 +
if (mInstance == null) {
 +
GameObject go = new GameObject("PhotonManager");
 +
mInstance = go.AddComponent<PhotonManager>();
 +
}
 +
return mInstance;
 +
}
 +
}
 +
// Use this for initialization
 +
public void Start () {
 
  PhotonNetwork.ConnectUsingSettings("v1.0");
 
  PhotonNetwork.ConnectUsingSettings("v1.0");
 
  }
 
  }
行17: 行51:
 
  void OnJoinedLobby ()
 
  void OnJoinedLobby ()
 
  {
 
  {
  Debug.Log ("PhotonManager OnJoinedLobby");
+
  Debug.Log ("OnJoinedLobby");
 
  CreateRoom ();
 
  CreateRoom ();
 
  }
 
  }
 
  public void CreateRoom(){
 
  public void CreateRoom(){
  string userName = "ユーザ1";
+
  string userName = "ユーザ" + userId;
string userId = "user1";
+
  PhotonNetwork.autoCleanUpPlayerObjects = true; // 離脱後自動破棄するかどうか
  PhotonNetwork.autoCleanUpPlayerObjects = false;
+
 
  ExitGames.Client.Photon.Hashtable customProp = new ExitGames.Client.Photon.Hashtable();
 
  ExitGames.Client.Photon.Hashtable customProp = new ExitGames.Client.Photon.Hashtable();
 
  customProp.Add ("userName", userName);
 
  customProp.Add ("userName", userName);
行31: 行64:
 
  roomOptions.customRoomProperties = customProp;
 
  roomOptions.customRoomProperties = customProp;
 
  roomOptions.customRoomPropertiesForLobby = new string[]{ "userName","userId"};
 
  roomOptions.customRoomPropertiesForLobby = new string[]{ "userName","userId"};
  roomOptions.maxPlayers = 2; // 部屋の最大人数
+
  roomOptions.maxPlayers = 5; // 部屋の最大人数
 
  roomOptions.isOpen = true; // 入室許可する
 
  roomOptions.isOpen = true; // 入室許可する
 
  roomOptions.isVisible = true; // ロビーから見えるようにする
 
  roomOptions.isVisible = true; // ロビーから見えるようにする
  PhotonNetwork.JoinOrCreateRoom (userId, roomOptions, null);
+
  PhotonNetwork.JoinOrCreateRoom (roomId, roomOptions, null);
 
  }
 
  }
 
  // ルーム入室した時
 
  // ルーム入室した時
 
  void OnJoinedRoom() {
 
  void OnJoinedRoom() {
  Debug.Log ("PhotonManager OnJoinedRoom");
+
  Debug.Log ("OnJoinedRoom");
 
  }
 
  }
  // ルーム一覧が取れた場合
+
  // ルーム一覧が取れた時
 
  void OnReceivedRoomListUpdate(){
 
  void OnReceivedRoomListUpdate(){
  RoomInfo[] rooms = PhotonNetwork.GetRoomList();
+
  RoomInfo[] rooms = PhotonNetwork.GetRoomList(); // 注意:OnReceivedRoomListUpdate()が呼ばれた以後でしか取得できない
 
  if (rooms.Length == 0) {
 
  if (rooms.Length == 0) {
 
  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"]);
// GameObject.Find("StatusText").GetComponent<Text>().text = rooms [i].name;
+
 
  }
 
  }
 
  }
 
  }
 
  }
 
  }
 
  }
 
  }
 +
 +
接続を検証するために1つはプレビュー版、もう一つはアプリにビルドしたものを動作させておく。
 +
 +
==部屋の詳細==
 +
RoomInfo r = PhotonNetwork.room;
 +
Debug.Log("roomname="+r.Name); // 部屋名
 +
Debug.Log("PlayerCount="+r.PlayerCount); // 現在の人数
 +
Debug.Log("maxPlayer="+r.MaxPlayers); // 最大人数
 +
 +
==ランダムで部屋に入る==
 +
PhotonNetwork.JoinRandomRoom();
 +
 +
void OnPhotonRandomJoinFailed(){
 +
string userName = "ユーザ" + userId;
 +
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; // ロビーから見えるようにする
 +
// roomnameをnullとすると1234e607-263f-4360-8bf9-51b3d4ae2348な感じのnameになる
 +
PhotonNetwork.CreateRoom (null, roomOptions, null);
 +
}
 +
 +
==既にある部屋に入る==
 +
PhotonNetwork.JoinRoom("room1");
 +
 +
==部屋から抜ける==
 +
PhotonNetwork.LeaveRoom ();
 +
 +
==切断==
 +
PhotonNetwork.Disconnect ();
  
 
==参考==
 
==参考==
 
http://sleepnel.hatenablog.com/entry/2016/05/29/120200
 
http://sleepnel.hatenablog.com/entry/2016/05/29/120200

2021年9月7日 (火) 10:37時点における最新版

ロビーと部屋に入る

  1. importしたプロジェクト内のPhotonUnityNetwork/Resource/PhotonServerSettingのInspectorを開く
  2. AutoJoinLobbyにチェックを入れる
  3. ヒエラルキーにGameObjectを作成して
  4. 以下LobbyScene.csを貼り付ける
  5. 以下PhotonManager.csをシングルトンで作成

LobbyScene.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LobbyScene : MonoBehaviour {
	PhotonManager manager;
	// Use this for initialization
	void Start () {
		manager = PhotonManager.Instance;
  		manager.userId = "user1";
  		manager.roomId = "room1";
		manager.Start ();
	}
	// Update is called once per frame
	void Update () {
	}
}

PhotonManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhotonManager : Photon.MonoBehaviour {
	private static PhotonManager mInstance;
	public string userId = "user1";
	public string roomId = "room1";
	private PhotonManager () {
	}
	public static PhotonManager Instance {
		get {
			if (mInstance == null) {
				GameObject go = new GameObject("PhotonManager");
				mInstance = go.AddComponent<PhotonManager>();
			}
			return mInstance;
		}
	}
	// Use this for initialization
	public 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 = "ユーザ" + userId;
		PhotonNetwork.autoCleanUpPlayerObjects = true; // 離脱後自動破棄するかどうか
		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 = 5; // 部屋の最大人数
		roomOptions.isOpen = true; // 入室許可する
		roomOptions.isVisible = true; // ロビーから見えるようにする
		PhotonNetwork.JoinOrCreateRoom (roomId, roomOptions, null);
	}
	// ルーム入室した時
	void OnJoinedRoom() {
		Debug.Log ("OnJoinedRoom");
	}
	// ルーム一覧が取れた時
	void OnReceivedRoomListUpdate(){
		RoomInfo[] rooms = PhotonNetwork.GetRoomList(); // 注意:OnReceivedRoomListUpdate()が呼ばれた以後でしか取得できない
		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つはプレビュー版、もう一つはアプリにビルドしたものを動作させておく。

部屋の詳細

RoomInfo r = PhotonNetwork.room;
Debug.Log("roomname="+r.Name); // 部屋名
Debug.Log("PlayerCount="+r.PlayerCount); // 現在の人数
Debug.Log("maxPlayer="+r.MaxPlayers); // 最大人数

ランダムで部屋に入る

PhotonNetwork.JoinRandomRoom();
void OnPhotonRandomJoinFailed(){
	string userName = "ユーザ" + userId;
	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; // ロビーから見えるようにする
	// roomnameをnullとすると1234e607-263f-4360-8bf9-51b3d4ae2348な感じのnameになる
	PhotonNetwork.CreateRoom (null, roomOptions, null);
}

既にある部屋に入る

PhotonNetwork.JoinRoom("room1");

部屋から抜ける

PhotonNetwork.LeaveRoom ();

切断

PhotonNetwork.Disconnect ();

参考

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