Last active
March 2, 2016 07:26
A class for P.U.N (Photon Unity Networking).
This class will look for an available room which is useful if you want to check room properties in advance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class RoomSelectionHelper : MonoBehaviour { | |
public bool onMasterServer = false; | |
public bool inTheLobby = false; | |
public int roomCount = 0; | |
public int selectedIndex = -1; | |
void Start(){ PhotonNetwork.ConnectUsingSettings("v4.2"); } | |
void OnConnectedToMaster(){ | |
onMasterServer = true; | |
PhotonNetwork.JoinLobby (); | |
} | |
// You could select a room here but it should be safer | |
// To keep checking since room state change over time. | |
void OnJoinedLobby(){ inTheLobby = true; } | |
void Update(){ | |
inTheLobby = PhotonNetwork.insideLobby; | |
if (inTheLobby) SelectRoom (); | |
} | |
void SelectRoom(){ | |
RoomInfo[] rooms = PhotonNetwork.GetRoomList (); | |
roomCount = rooms.Length; | |
foreach (RoomInfo room in rooms) { | |
if (room.playerCount > 0 && room.open) { | |
if (room.playerCount < room.maxPlayers || room.maxPlayers == 0) { | |
DoSelectRoom (room.name); | |
} | |
} | |
} | |
} | |
// This part is custom. In our case we parse | |
// the room name to extract the level index. | |
void DoSelectRoom(string name){ | |
int i = name.IndexOf('-'); | |
string indexString = name.Substring (i + 1); | |
selectedIndex = int.Parse (indexString); | |
GetComponent<MainMenuActions> ().survivalLevelIndex = selectedIndex; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment