一, 简介
玩过 LOL , dota2, 王者荣耀 等MOBA类的游戏,就很容易理解大厅和房间了.
LOL中一个服务器就相当与一个大厅; 什么电一,电二 ,,, 联通一区等 每一个区就相当于一个大厅.
而开始游戏创建一个自定义游戏的时候 , 就是创建了一个房间 对应 PhotonNetwork.CreateRoom() 函数 .
LOL普通匹配就像是一个有对应匹配算法的 PhotonNetwork.JoinRandom() .
二, 使用
① 连接到服务器
服务器的地址在这里设置
勾选Auto-Join Lobby 后程序就会自动加入到房间 , 不需要再在代码中实现加入大厅
在适当的地方调用 PhotonNetwork.ConnectUsingSettings(string _GameVersion) ; 即可连接到大厅.
连接大厅成功与失败对应两个函数
首先脚本需要继承自 Photon.PunBehaviour
public class Launcher : Photon.PunBehaviour { /// <summary> /// 成功连接到大厅 /// </summary> public override void OnConnectedToPhoton() { } /// <summary> /// 连接大厅失败 /// </summary> /// <param name="error"></param> private void OnFailedToConnect(NetworkConnectionError error) { Debug.Log("fail to Connect"); } }
② 监控房间列表
重写 Photon.PunBehaviour.OnReceivedRoomListUpdate() ;
public override void OnReceivedRoomListUpdate(){ }
③ 设置玩家昵称 PhotonNetwork.playerName = "PlayerName";
④ 创建房间 PhotonNetwork.CreateRoom("roomName"
,
new
RoomOptions() { MaxPlayers = MaxPlayersPerRoom },(TypedLobby)null
);
第一个参数是房间名, 第二个参数房间参数, 第三个参数 房间所在的大厅 默认大厅是null
⑤ 加入房间
/// <param name="roomName">独一无二的房间名</param>
/// <param name="expectedUsers">玩家在房间中的顺序</param>
/// <returns>加入是否成功</returns>
public static bool JoinRoom(string roomName, string[] expectedUsers)
三. 完整代码
using System; using System.Collections; using UnityEngine; using UnityEngine.UI; namespace com.Lobby { public class Launcher : Photon.PunBehaviour { #region PUBLIC //客户端版本 public string _gameVersion = "1.0"; //玩家名字 public InputField nameField; //房间列表 public RectTransform LobbyPanel; #endregion #region PRIVATE private bool isConnecting; #endregion private void Awake() { //#不重要 //强制Log等级为全部 PhotonNetwork.logLevel = PhotonLogLevel.Full; //#关键 //我们不加入大厅 这里不需要得到房间列表所以不用加入大厅去 PhotonNetwork.autoJoinLobby = true; //#关键 //这里保证所有主机上调用 PhotonNetwork.LoadLevel() 的时候主机和客户端能同时进入新的场景 PhotonNetwork.automaticallySyncScene = true; } // Use this for initialization void Start() { Connect(); SetPlayerName(); } /// <summary> /// 连接到大厅 /// </summary> private void Connect() { isConnecting = true; //已經連接上了服務器 if (PhotonNetwork.connected) { Debug.Log("Connected"); } else { PhotonNetwork.ConnectUsingSettings(_gameVersion); } } /// <summary> /// 成功连接到大厅 /// </summary> public override void OnConnectedToPhoton() { base.OnConnectedToPhoton(); } /// <summary> /// 连接大厅失败 /// </summary> /// <param name="error"></param> private void OnFailedToConnect(NetworkConnectionError error) { Debug.Log("fail to Connect"); } public void CreateARoom() { if (PhotonNetwork.connected) { //创建房间成功 if (PhotonNetwork.CreateRoom(nameField.text, new RoomOptions { MaxPlayers = 4 }, null)) { Debug.Log("Launcher.CreateARoom 成功"); PhotonNetwork.LoadLevel("Room"); } } } public override void OnPhotonCreateRoomFailed(object[] codeAndMsg) { Debug.Log("Launcher Create Room faileds"); } public void PlayerNameChanged() { if (string.IsNullOrEmpty(nameField.text)) { PlayerPrefs.SetString("PlayerName", "default"); } else { SetPlayerName(); } } public void SetPlayerName() { if (string.IsNullOrEmpty(nameField.text)) { if (PlayerPrefs.HasKey("PlayerName")) { nameField.text = PlayerPrefs.GetString("PlayerName"); } } PhotonNetwork.playerName = nameField.text; PlayerPrefs.SetString("PlayerName", nameField.text); } public override void OnReceivedRoomListUpdate() { Debug.Log("OnReceivedRoomListUpdate"); RoomInLobby[] ts = LobbyPanel.GetComponentsInChildren<RoomInLobby>(); foreach (RoomInLobby t in ts) { Destroy(t.gameObject); } RoomInfo[] rooms = PhotonNetwork.GetRoomList(); foreach (RoomInfo room in rooms) { GameObject g = GameObject.Instantiate(Resources.Load("Lobby/RoomItem") as GameObject); Text t = g.transform.Find("Text").GetComponent<Text>(); t.text = room.Name; g.name = room.Name; g.transform.SetParent(LobbyPanel); g.transform.localScale = Vector3.one; } } } }
这里有整个工程的代码 https://github.com/Luckeee/mahjong