本文主要是介绍Unity开发:Photon入门与常用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
官方给了很多文档,其中大部分的功能在Public API Model中都能查阅的到,这里为了方便今后使用,摘录其中部分API
初步连接,并加入随机房间
void Awake()
{PhotonNetwork.autoJoinLobby = false;// 这个确保我们能在服务端使用PhotonNetwork.LoadLevel()并让所有在一个房间的客户端同步自动加载他们的场景PhotonNetwork.automaticallySyncScene = true;
}if (PhotonNetwork.connected)
{PhotonNetwork.JoinRandomRoom();
}
else
{// #Critical, we must first and foremost connect to Photon Online Server.PhotonNetwork.ConnectUsingSettings(_gameVersion);
}
获取房间内人数
PhotonNetwork.room.PlayerCount//获取房间内总人数
PhotonNetwork.room.MaxPlayers
目前先写到这..之后有用到新方法再更新..最后附一个我写的通用性比较强的网络连接脚本
public class WWWManager : PunBehaviour
{public static WWWManager instance;private void Awake(){instance = this;PhotonNetwork.autoJoinLobby = false;Connect();}public bool Connected(){return PhotonNetwork.connected;}public bool InLobby(){return PhotonNetwork.insideLobby;}public bool InRoom(){return PhotonNetwork.inRoom;}public string RoomName(){if (InRoom()){return PhotonNetwork.room.name;}return null;}public int RoomPlayerCount(){if (InRoom()){return PhotonNetwork.room.PlayerCount;}return 0;}public int MaxRoomPlayer(){if (InRoom()){return PhotonNetwork.room.MaxPlayers;}return 0;}public PhotonPlayer[] GetRoomPlayers(){if (InRoom()){return PhotonNetwork.playerList; }return null;}public PhotonPlayer[] GetOtherPlayers(){if (InRoom()){return PhotonNetwork.otherPlayers;}return null;}/// <summary>/// 获取所有的房间信息,每个房间信息可以得到房间名,房间人数,最大房间数等信息/// </summary>/// <returns></returns>public RoomInfo[] GetRoomList(){if (InLobby()){return PhotonNetwork.GetRoomList(); }return null;}/// <summary>/// 连接服务器/// </summary>public void Connect(){Debug.Log("连接中");PhotonNetwork.ConnectUsingSettings("1");}/// <summary>/// 加入到大厅,需要在连接后进行/// </summary>public void JoinLobby(){if (Connected()){PhotonNetwork.JoinLobby();}else{Debug.Log("未连接");}}/// <summary>/// 创建一个房间,并且指定房间名/// </summary>/// <param name="room"></param>public void CreateRoom(string room){if (InLobby()){PhotonNetwork.CreateRoom(room, new RoomOptions() { MaxPlayers = 4 }, null); }else{Debug.Log("还未加入大厅");}}/// <summary>/// 加入指定名字的房间/// </summary>/// <param name="room"></param>public void JoinRoom(string room){if (InLobby()){PhotonNetwork.JoinRoom(room); }}/// <summary>/// 随机加入一个房间/// </summary>public void JoinRandomRoom(){if (InLobby()){PhotonNetwork.JoinRandomRoom(); }}/// <summary>/// 离开当前房间/// </summary>public void LeaveRoom(){if (InRoom()){PhotonNetwork.LeaveRoom(false); }}public void LeaveLobby(){if (InLobby()){if (PhotonNetwork.LeaveLobby()){Debug.Log("离开大厅");}}}private void OnConnectedToServer(){Debug.Log("连接服务器成功");}public override void OnJoinedLobby(){Debug.Log("joinLobby");updateRoomList();}public override void OnJoinedRoom(){Debug.Log("joinRoom");updatePlayerList();}public override void OnPhotonJoinRoomFailed(object[] codeAndMsg){Debug.Log("join failed");}public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer){Debug.Log("有玩家进入房间");updatePlayerList();}public override void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer){Debug.Log("有玩家离开房间");updatePlayerList();}public override void OnReceivedRoomListUpdate(){Debug.Log("房间列表更新");updateRoomList();}private void updatePlayerList(){PhotonPlayer[] photonPlayers = PhotonNetwork.playerList;foreach (PhotonPlayer player in photonPlayers){string playerNickName = player.NickName;Debug.Log(playerNickName);}}private void updateRoomList(){RoomInfo[] roomInfos = PhotonNetwork.GetRoomList();foreach (RoomInfo info in roomInfos){byte roomMaxPlayers = info.MaxPlayers;int roomPlayerCount = info.PlayerCount;string roomName = info.Name;Debug.Log(roomName + roomPlayerCount + roomMaxPlayers);}}
}
这篇关于Unity开发:Photon入门与常用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!