Photon PUN 二 大厅 & 房间
一, 简介
玩过 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 = }, 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
Photon PUN 二 大厅 & 房间的更多相关文章
- Photon PUN 三 RPCs & RaiseEvent
官方文档地址 https://doc.photonengine.com/en-us/pun/current/manuals-and-demos/rpcsandraiseevent 一, RPC P ...
- Photon PUN 一 介绍
有句话说的好 , 官网永远是最好的学习地方 . 虽然国内的资料不多 , 但是官网的资料还是很充足 , 这就带着英汉词典就着作阅读理解的劲头去官网学习吧 https://doc.photonengine ...
- Unity - Photon PUN 本地与网络同步的逻辑分离 (二)
上篇实现了事件系统的设计,这篇就来结合发送RPC消息 并且不用标记 [PunRPC] 先来看下上编的代码 GameEnvent.cs private static Dictionary<Comm ...
- Unity - Photon PUN 本地与网络同步的逻辑分离 (一)
服务器大家可以使用Photon官网提供的,这样会变得很简单,直接搭建下就好.或者下载到本地开启本地端Photon服务器 (大家也可以使用和我一样方式有时间做了个winform 程序用来管理本地服务器开 ...
- 使用Photon引擎进行unity网络游戏开发(三)——网络游戏大厅及房间
使用Photon引擎进行unity网络游戏开发(三)--网络游戏大厅及房间 Photon PUN Unity 网络游戏开发 连接到Photon ConnectUsingSettings 设置你的客户端 ...
- 使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍
使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍 Photon PUN Unity 网络游戏开发 Photon常用类介绍: IPunCallback PUNGIPunCa ...
- 使用Photon引擎进行unity网络游戏开发(四)——Photon引擎实现网络游戏逻辑
使用Photon引擎进行unity网络游戏开发(四)--Photon引擎实现网络游戏逻辑 Photon PUN Unity 网络游戏开发 网络游戏逻辑处理与MasterClient 网络游戏逻辑处理: ...
- 使用Photon引擎进行unity网络游戏开发(一)——Photon引擎简介
使用Photon引擎进行unity网络游戏开发(一)--Photon引擎简介 Photon PUN Unity 网络游戏开发 Photon引擎简介: 1. 服务器引擎: 服 务 器 引 擎 介 绍 服 ...
- Unity3d客户端与Photon服务器数据通信
今天先介绍一下Photon服务器是什么,可以做什么,为什么要使用它? Photon:开发多人联网游戏最轻松的方案!可以迅速简单实现多人实时在线网络游戏(pvp). Photon:透过位于各地的Phot ...
随机推荐
- Linux 下使用 killall 命令终止进程的 8 大用法
Linux 的命令行提供很多命令来杀死进程.比如,你可以向 kill 命传递一个PID来杀死进程:pkill 命令使用一个正则表达式作为输入,所以和该模式匹配的进程都被杀死. 但是还有一个命令叫 ki ...
- 简单的vector--- 2
如何重载operator[] 及其相关细节 如何使用 const_cast<>( ) 和 static_cast<>( ) 模板类 如何内部声明,外部定义友元函数 使用 ...
- 11-Arrays工具类的使用
1.理解:① 定义在java.util包下.② Arrays:提供了很多操作数组的方法. 2.使用: //1.boolean equals(int[] a,int[] b):判断两个数组是否相等. i ...
- python6.2类的封装
class Card(object): def __init__(self,num,pwd,ban): self.num=num#卡号 self.pwd=pwd#密码 self.__ban=ban#余 ...
- Fault-Tolerance, Fast and Slow: Exploiting Failure Asynchrony in Distributed Systems
本文(OSDI 18')主要介绍一种新的副本复制协议:SAUCR(场景可感知的更新与故障恢复).它是一种混合的协议: 在一定场景(正常情况)下:副本复制的数据缓存在内存中. 故障发生时(多个节点挂掉, ...
- 【系统之音】WindowManager工作机制详解
前言 目光所及,皆有Window!Window,顾名思义,窗口,它是应用与用户交互的一个窗口,我们所见到视图,都对应着一个Window.比如屏幕上方的状态栏.下方的导航栏.按音量键调出来音量控制栏.充 ...
- Kibana配置nginx反代并本地ca加密nginx
简介 我们部署完ELK Stack后,虽然可以直接浏览器访问kibana进行访问,但这样对一些重要数据来说是不安全的,可以利用密码验证设置权限访问,在Kibana所在的服务器上安装Nginx服务,利用 ...
- c++之广度优先搜索
广度优先搜索BFS(Breadth First Search)也称为宽度优先搜索,它是一种先生成的结点先扩展的策略. 在广度优先搜索算法中,解答树上结点的扩展是按它们在树中的层次进行的.首先生成第一层 ...
- find the lowest number location
before #设定路径列表Path def find_path2(heightmap, x, y, water_level=557,path=[]): #global path #设定坐标 右0 左 ...
- MySQL设置传输包大小
MySQL执行插入或更新时, 当数据量过大时, 可能由于"max_allowed_packet"参数的限制导致执行失败.此时, 可以重新设置该参数的值. "max_all ...