ConnectUsingSettings 設置你的客戶端的遊戲版本並使用一個由PUN設置嚮導寫入的配置文件,該配置文件保存在PhotonServerSettings裏面。html
PhotonNetwork.ConnectUsingSettings("1.0");
MasterServer會管理多個遊戲大廳,當客戶端鏈接至MasterServer後,須要選擇一個遊戲大廳進入web
/// <summary>
/// 進入大廳回調
/// </summary>
public override void OnJoinedLobby() {
Debug.Log("歡迎 " + PhotonNetwork.player.NickName);
UserMessageText.text = "歡迎 " + PhotonNetwork.player.NickName;
}
經過CreateRoom方法咱們能夠建立遊戲房間,其中RoomOption類能夠幫助咱們設置房間的一些屬性,如房間中的人數,房間大廳中是否可見等網絡
PhotonNetwork.CreateRoom(roomName.text, roomOptions, TypedLobby.Default);
public void ClickConfirmCreateRoomButton() {
RoomOptions roomOptions = new RoomOptions();
RectTransform toggleRectTransform = MaxPlayerToggle.GetComponent<RectTransform>();
int childCount = toggleRectTransform.childCount;
for (int i = 0; i < childCount; i++)
{
if (toggleRectTransform.GetChild(i).GetComponent<Toggle>().isOn)
{
roomOptions.MaxPlayers = maxPlayerNum[i];
break;
}
}
RoomInfo[] roomInfos = PhotonNetwork.GetRoomList();
bool isRoomNameReapeat = false;
foreach (RoomInfo roomInfo in roomInfos)
{
if (roomName.text == roomInfo.Name)
{
isRoomNameReapeat = true;
}
}
if (isRoomNameReapeat)
roommNameHint.text = "房間名稱重複!";
else
{
PhotonNetwork.CreateRoom(roomName.text, roomOptions, TypedLobby.Default);
CreateRoomPanel.SetActive(false);
roomLoadingPanel.SetActive(true);
}
}
PhotonNetwork.JoinRandomRoom();
PhotonNetwork.JoinRoom(roomName);
經過Photon提供的OnReceivedRoomListUpdate()回調函數,咱們能夠處理房間內大廳更新時的邏輯,例如更新UI中顯示的房間列表。dom
/// <summary>
/// 覆寫回調函數,當房間更新時調用
/// 更新遊戲大廳中的房間列表信息
/// </summary>
public override void OnReceivedRoomListUpdate() {
roomInfo=PhotonNetwork.GetRoomList();
...
ShowRoomMessage();
}
private void ShowRoomMessage() {
//經過roomInfo信息來顯示房間信息
...
}
/// <summary>
/// 進入大廳回調
/// </summary>
public override void OnJoinedLobby() /// <summary> /// 鏈接失敗回調 /// </summary> /// <param name="cause"></param> public override void OnConnectionFail(DisconnectCause cause) //進入遊戲房間回調函數 public override void OnJoinedRoom() //客戶端鏈接到masterserver時調用,加入默認遊戲大廳 //等同於Photonseversetting中勾選Auto join lobby public override void OnConnectedToMaster() /// <summary> /// 當房間列表更新時調用 /// </summary> public override void OnReceivedRoomListUpdate() //當加入房間失敗時調用 public override void OnPhotonJoinRoomFailed(object[] codeAndMsg)