facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「LobbyScene.cs <pre> using UnityEngine; using UnityEngine.UI; public class LobbyScene : MonoBehaviour { PhotonManager manager; void Start() { GameObject.Find("InputF...」)
(相違点なし)

2021年8月11日 (水) 17:55時点における版

LobbyScene.cs

using UnityEngine;
using UnityEngine.UI;

public class LobbyScene : MonoBehaviour
{
	PhotonManager manager;
	void Start()
	{
		GameObject.Find("InputField").GetComponent<InputField>().text = "room1";
		GameObject.Find("Button").GetComponent<Button>().onClick.AddListener(OnClick);
	}
	void OnClick()
	{
		string roomId = GameObject.Find("InputField").GetComponent<InputField>().text;
		manager = PhotonManager.Instance;
		manager.roomId = roomId;
		manager.userId = "user" + Random.RandomRange(1, 1000);
		manager.ConnectStart();
	}
}

PhotonManager.cs

using System.Collections.Generic;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Linq;

public class PhotonManager : MonoBehaviourPunCallbacks
{
	public string roomId = "room1";
	public string userId = "user1";
	private static PhotonManager mInstance;
	private PhotonManager()
	{
	}
	public static PhotonManager Instance
	{
		get
		{
			if (mInstance == null)
			{
				GameObject go = new GameObject("PhotonManager");
				mInstance = go.AddComponent<PhotonManager>();
			}
			return mInstance;
		}
	}
	public void ConnectStart()
	{
		Debug.Log("ConnectStart");
		if (PhotonNetwork.IsConnected == false)
		{
			PhotonNetwork.GameVersion = "v1.0";
			PhotonNetwork.ConnectUsingSettings();
		}
	}
	// マスターサーバーに接続した時
	public override void OnConnectedToMaster()
	{
		Debug.Log("OnConnectedToMaster");
		if (PhotonNetwork.IsConnected)
		{
			PhotonNetwork.JoinLobby();
		}
	}
	public override void OnJoinedLobby()
	{
		Debug.Log("OnJoinedLobby");

		// PhotonNetwork.JoinRandomRoom();
		CreateRoom();
	}
	public void CreateRoom()
	{
		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 = 5; // 部屋の最大人数
		roomOptions.IsOpen = true; // 入室許可する
		roomOptions.IsVisible = true; // ロビーから見えるようにする
		PhotonNetwork.JoinOrCreateRoom(roomId, roomOptions, null);

	}
	// ルーム入室した時
	public override void OnJoinedRoom()
	{
		Debug.Log("OnJoinedRoom");
		RoomInfo r = PhotonNetwork.CurrentRoom;
		Debug.Log("roomname=" + r.Name); // 部屋名
		Debug.Log("PlayerCount=" + r.PlayerCount); // 現在の人数
		Debug.Log("maxPlayer=" + r.MaxPlayers); // 最大人数
		Debug.Log("userId=" + userId);

		SceneManager.LoadScene("RpcViewScene");
	}
	public override void OnRoomListUpdate(List<RoomInfo> roomList)
	{
		Debug.Log("OnRoomListUpdate");
		List<RoomInfo> tmpRoomList = roomList.Where(s => s.IsOpen).ToList();
	}

	public override void OnJoinRandomFailed(short returnCode, string message)
	{
		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とすると7d60e607-263f-4360-8bf9-51b3d4ae2348な感じのnameになる
		PhotonNetwork.CreateRoom(null, roomOptions, null);
	}
}