room selector for lobby added

This commit is contained in:
Steven 2017-09-28 01:42:24 -05:00
parent aa85cea860
commit 80ad880d65
4 changed files with 148 additions and 305 deletions

View file

@ -129,7 +129,18 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 114067761447900238}
m_OnClick:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 114427775473896594}
m_MethodName: RoomSelected
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_TypeName: UnityEngine.UI.Button+ButtonClickedEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
--- !u!114 &114418020196636122
@ -178,6 +189,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
roomNameText: {fileID: 114647180428938562}
numPlayersText: {fileID: 114418020196636122}
launcher: {fileID: 0}
--- !u!114 &114647180428938562
MonoBehaviour:
m_ObjectHideFlags: 1

View file

@ -276,7 +276,6 @@ RectTransform:
m_Children:
- {fileID: 1035400198}
- {fileID: 580129344}
- {fileID: 1051807739}
- {fileID: 1174787944}
- {fileID: 1009736659}
m_Father: {fileID: 1435142555}
@ -784,80 +783,6 @@ CanvasRenderer:
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 606582670}
--- !u!1 &798783880
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 798783881}
- component: {fileID: 798783883}
- component: {fileID: 798783882}
m_Layer: 5
m_Name: Text
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &798783881
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 798783880}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1051807739}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &798783882
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 798783880}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 14
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 10
m_MaxSize: 40
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Join Room
--- !u!222 &798783883
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 798783880}
--- !u!1 &871952079
GameObject:
m_ObjectHideFlags: 0
@ -983,12 +908,12 @@ RectTransform:
m_Children:
- {fileID: 1139586349}
m_Father: {fileID: 41482507}
m_RootOrder: 4
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0}
m_AnchorMax: {x: 0.5, y: 0}
m_AnchoredPosition: {x: 0, y: 10}
m_SizeDelta: {x: 160, y: 30}
m_SizeDelta: {x: 200, y: 30}
m_Pivot: {x: 0.5, y: 0}
--- !u!114 &1009736660
MonoBehaviour:
@ -1207,124 +1132,13 @@ MonoBehaviour:
m_HorizontalOverflow: 1
m_VerticalOverflow: 1
m_LineSpacing: 1
m_Text: Available Rooms
m_Text: Rooms
--- !u!222 &1035400200
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1035400197}
--- !u!1 &1051807738
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 1051807739}
- component: {fileID: 1051807742}
- component: {fileID: 1051807741}
- component: {fileID: 1051807740}
m_Layer: 5
m_Name: JoinRoomButton
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1051807739
RectTransform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1051807738}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 798783881}
m_Father: {fileID: 41482507}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0}
m_AnchorMax: {x: 0.5, y: 0}
m_AnchoredPosition: {x: 70, y: 70}
m_SizeDelta: {x: 120, y: 30}
m_Pivot: {x: 0.5, y: 0}
--- !u!114 &1051807740
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1051807738}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 1392445389, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 1051807741}
m_OnClick:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.Button+ButtonClickedEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
--- !u!114 &1051807741
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1051807738}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: -765806418, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
--- !u!222 &1051807742
CanvasRenderer:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1051807738}
--- !u!1 &1139586348
GameObject:
m_ObjectHideFlags: 0
@ -1392,7 +1206,7 @@ MonoBehaviour:
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Create Room
m_Text: Create New Room
--- !u!222 &1139586351
CanvasRenderer:
m_ObjectHideFlags: 0
@ -1551,11 +1365,11 @@ RectTransform:
m_Children:
- {fileID: 1365427910}
m_Father: {fileID: 41482507}
m_RootOrder: 3
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0}
m_AnchorMax: {x: 0.5, y: 0}
m_AnchoredPosition: {x: -70, y: 70}
m_AnchoredPosition: {x: 0, y: 70}
m_SizeDelta: {x: 120, y: 30}
m_Pivot: {x: 0.5, y: 0}
--- !u!114 &1174787945

View file

@ -1,125 +1,121 @@
using UnityEngine;
using UnityEngine.UI;
namespace Com.MyCompany.MyGame
public class Launcher : Photon.PunBehaviour
{
public class Launcher : Photon.PunBehaviour
public byte MaxPlayersPerRoom = 4;
[Tooltip("The Ui Panel to let the user enter name, connect and play")]
public GameObject controlPanel;
[Tooltip("The UI Label to inform the user that the connection is in progress")]
public Text progressLabel;
public GameObject nameSelectorPanel, roomSelectorPanel;
public GameObject availableRoomsPanel;
public GameObject roomStatusPanelPrefab;
string _gameVersion = "1";
bool isConnecting;
void Awake()
{
public byte MaxPlayersPerRoom = 4;
// #Critical
// we don't join the lobby. There is no need to join a lobby to get the list of rooms.
PhotonNetwork.autoJoinLobby = false;
[Tooltip("The Ui Panel to let the user enter name, connect and play")]
public GameObject controlPanel;
// #Critical
// This makes sure we can use PhotonNetwork.LoadLevel() on the master client
// and all clients in the same room sync their level automatically
PhotonNetwork.automaticallySyncScene = true;
}
[Tooltip("The UI Label to inform the user that the connection is in progress")]
public Text progressLabel;
void Start()
{
controlPanel.SetActive(true);
}
public GameObject nameSelectorPanel, roomSelectorPanel;
public GameObject availableRoomsPanel;
public GameObject roomStatusPanelPrefab;
string _gameVersion = "1";
/// <summary>
/// Keep track of the current process. Since connection is asynchronous and is based on several callbacks from Photon,
/// we need to keep track of this to properly adjust the behavior when we receive call back by Photon.
/// Typically this is used for the OnConnectedToMaster() callback.
/// </summary>
bool isConnecting;
void Awake()
public void Connect()
{
isConnecting = true;
progressLabel.text = "Connecting...";
if (PhotonNetwork.connected)
{
// #Critical
// we don't join the lobby. There is no need to join a lobby to get the list of rooms.
PhotonNetwork.autoJoinLobby = false;
// #Critical
// This makes sure we can use PhotonNetwork.LoadLevel() on the master client
// and all clients in the same room sync their level automatically
PhotonNetwork.automaticallySyncScene = true;
PhotonNetwork.JoinLobby();
}
void Start()
else
{
controlPanel.SetActive(true);
}
public void Connect()
{
isConnecting = true;
progressLabel.text = "Connecting...";
// we check if we are connected or not, we join if we are , else we initiate the connection to the server.
if (PhotonNetwork.connected)
{
PhotonNetwork.JoinLobby();
}
else
{
PhotonNetwork.ConnectUsingSettings(_gameVersion);
}
}
public override void OnConnectedToMaster()
{
Debug.Log("Launcher: OnConnectedToMaster() was called by PUN");
// we don't want to do anything if we are not attempting to join a room.
// this case where isConnecting is false is typically when you lost or quit the game, when this level is loaded, OnConnectedToMaster will be called, in that case
// we don't want to do anything.
if (isConnecting)
{
// #Critical: The first we try to do is to join a potential existing room. If there is, good, else, we'll be called back with OnPhotonRandomJoinFailed()
PhotonNetwork.JoinLobby();
//PhotonNetwork.JoinRandomRoom();
}
}
public override void OnJoinedLobby()
{
Debug.Log("Launcher: Entered Lobby");
nameSelectorPanel.SetActive(false);
roomSelectorPanel.SetActive(true);
RefreshRoomsList();
}
public void RefreshRoomsList()
{
RoomInfo[] roomsList = PhotonNetwork.GetRoomList();
Debug.Log("Number of rooms available: " + roomsList.Length);
foreach (Transform child in availableRoomsPanel.transform)
{
Destroy(child.gameObject);
}
foreach (RoomInfo roomInfo in roomsList)
{
var roomStatusPanel = Instantiate(roomStatusPanelPrefab, availableRoomsPanel.transform);
roomStatusPanel.GetComponent<RoomStatusPanel>().SetInformation(roomInfo);
}
}
public void CreateNewRoom()
{
PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = MaxPlayersPerRoom }, null);
}
public override void OnDisconnectedFromPhoton()
{
Debug.LogWarning("Launcher: OnDisconnectedFromPhoton() was called by PUN");
}
/*
public override void OnPhotonRandomJoinFailed(object[] codeAndMsg)
{
Debug.Log("Launcher:OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one.");
// #Critical: we failed to join a random room, maybe none exists or they are all full. No worries, we create a new room.
PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = MaxPlayersPerRoom }, null);
}*/
public override void OnJoinedRoom()
{
Debug.Log("Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
// #Critical: We only load if we are the first player, else we rely on PhotonNetwork.automaticallySyncScene to sync our instance scene.
PhotonNetwork.LoadLevel("Lobby");
PhotonNetwork.ConnectUsingSettings(_gameVersion);
}
}
public override void OnConnectedToMaster()
{
Debug.Log("Launcher: OnConnectedToMaster() was called by PUN");
if (isConnecting)
{
PhotonNetwork.JoinLobby();
}
}
public override void OnJoinedLobby()
{
Debug.Log("Launcher: Entered Lobby");
nameSelectorPanel.SetActive(false);
roomSelectorPanel.SetActive(true);
RefreshRoomsList();
}
public override void OnPhotonJoinRoomFailed(object[] codeAndMsg)
{
RefreshRoomsList();
}
public override void OnReceivedRoomListUpdate()
{
RefreshRoomsList();
}
public void RefreshRoomsList()
{
RoomInfo[] roomsList = PhotonNetwork.GetRoomList();
foreach (Transform child in availableRoomsPanel.transform)
{
Destroy(child.gameObject);
}
foreach (RoomInfo roomInfo in roomsList)
{
var panel = Instantiate(roomStatusPanelPrefab, availableRoomsPanel.transform);
var roomStatusPanel = panel.GetComponent<RoomStatusPanel>();
roomStatusPanel.SetInformation(roomInfo);
roomStatusPanel.launcher = this;
}
}
public void RoomSelected(RoomStatusPanel panel)
{
PhotonNetwork.JoinRoom(panel.GetRoomName());
}
public void CreateNewRoom()
{
string roomName = PhotonNetwork.playerName + "'s Room";
PhotonNetwork.CreateRoom(roomName, new RoomOptions() { MaxPlayers = MaxPlayersPerRoom }, null);
}
public override void OnDisconnectedFromPhoton()
{
Debug.LogWarning("Launcher: OnDisconnectedFromPhoton() was called by PUN");
}
public override void OnJoinedRoom()
{
Debug.Log("Launcher: OnJoinedRoom() called by PUN. Now this client is in a room.");
// #Critical: We only load if we are the first player, else we rely on PhotonNetwork.automaticallySyncScene to sync our instance scene.
PhotonNetwork.LoadLevel("Lobby");
}
}

View file

@ -7,10 +7,31 @@ public class RoomStatusPanel : MonoBehaviour {
public Text roomNameText;
public Text numPlayersText;
public Launcher launcher;
private int numPlayers, maxPlayers;
public void SetInformation(RoomInfo info)
{
roomNameText.text = info.Name;
numPlayersText.text = info.PlayerCount.ToString() + "/" + info.MaxPlayers.ToString();
numPlayers = info.PlayerCount;
maxPlayers = info.MaxPlayers;
numPlayersText.text = numPlayers.ToString() + "/" + maxPlayers.ToString();
if (numPlayers == maxPlayers)
{
numPlayersText.color = Color.red;
}
}
public void RoomSelected()
{
if (numPlayers <= maxPlayers)
{
launcher.RoomSelected(this);
}
}
public string GetRoomName()
{
return roomNameText.text;
}
}