一, 简介

  玩过 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 二 大厅 & 房间的更多相关文章

  1. Photon PUN 三 RPCs & RaiseEvent

    官方文档地址 https://doc.photonengine.com/en-us/pun/current/manuals-and-demos/rpcsandraiseevent 一, RPC   P ...

  2. Photon PUN 一 介绍

    有句话说的好 , 官网永远是最好的学习地方 . 虽然国内的资料不多 , 但是官网的资料还是很充足 , 这就带着英汉词典就着作阅读理解的劲头去官网学习吧 https://doc.photonengine ...

  3. Unity - Photon PUN 本地与网络同步的逻辑分离 (二)

    上篇实现了事件系统的设计,这篇就来结合发送RPC消息 并且不用标记 [PunRPC] 先来看下上编的代码 GameEnvent.cs private static Dictionary<Comm ...

  4. Unity - Photon PUN 本地与网络同步的逻辑分离 (一)

    服务器大家可以使用Photon官网提供的,这样会变得很简单,直接搭建下就好.或者下载到本地开启本地端Photon服务器 (大家也可以使用和我一样方式有时间做了个winform 程序用来管理本地服务器开 ...

  5. 使用Photon引擎进行unity网络游戏开发(三)——网络游戏大厅及房间

    使用Photon引擎进行unity网络游戏开发(三)--网络游戏大厅及房间 Photon PUN Unity 网络游戏开发 连接到Photon ConnectUsingSettings 设置你的客户端 ...

  6. 使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍

    使用Photon引擎进行unity网络游戏开发(二)——Photon常用类介绍 Photon PUN Unity 网络游戏开发 Photon常用类介绍: IPunCallback PUNGIPunCa ...

  7. 使用Photon引擎进行unity网络游戏开发(四)——Photon引擎实现网络游戏逻辑

    使用Photon引擎进行unity网络游戏开发(四)--Photon引擎实现网络游戏逻辑 Photon PUN Unity 网络游戏开发 网络游戏逻辑处理与MasterClient 网络游戏逻辑处理: ...

  8. 使用Photon引擎进行unity网络游戏开发(一)——Photon引擎简介

    使用Photon引擎进行unity网络游戏开发(一)--Photon引擎简介 Photon PUN Unity 网络游戏开发 Photon引擎简介: 1. 服务器引擎: 服 务 器 引 擎 介 绍 服 ...

  9. Unity3d客户端与Photon服务器数据通信

    今天先介绍一下Photon服务器是什么,可以做什么,为什么要使用它? Photon:开发多人联网游戏最轻松的方案!可以迅速简单实现多人实时在线网络游戏(pvp). Photon:透过位于各地的Phot ...

随机推荐

  1. Spring异常总结

    1.  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean o ...

  2. 从jdbc到spring-boot-starter-jdbc

    从jdbc到spring-boot-starter-jdbc jdbc 是什么 JDBC是一种用于执行SQL语句的API,可以为多种关系数据库提供统一访问,它是由一组用Java语言编写的类和接口.是J ...

  3. Android Studio--Activity实现跳转功能

    首先,完成一个布局文件,名字就叫做activity_text_view.xml <?xml version="1.0" encoding="utf-8"? ...

  4. 8 Java 条件逻辑语句

    生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,在上学的时候,如果期末考试成绩在全校能拿到前100名,则奖励一个 iPhone 11 .对于这种“需要先判断条件,条件满足后才执行的情况 ...

  5. java多线程(三):多线程单例模式,双重检查,volatile关键字

    一.事先准备 首先准备一个运行用的代码: public class Singleton { public static void main(String[] args) { Thread[] thre ...

  6. 图数据库HugeGraph源码解读 (1) —— 入门介绍

    HugeGraph介绍 以下引自官方文档: HugeGraph是一款易用.高效.通用的开源图数据库系统(Graph Database,GitHub项目地址), 实现了Apache TinkerPop3 ...

  7. C#LeetCode刷题之#100-相同的树(Same Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4066 访问. 给定两个二叉树,编写一个函数来检验它们是否相同. ...

  8. C#设计模式之2-抽象工厂模式

    抽象工厂模式(Abstract Factory Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/391 ...

  9. Nginx同时支持Http和Https

    现在的网站支持Https几乎是标配功能,Nginx能很好的支持Https功能.下面列举一个配置同时支持Http和Https的功能. 需要注意的是:既然选择使用Https,就是为了保证通信安全,那么就没 ...

  10. 自建本地服务器,自建Web服务器——保姆级教程!

    搭建本地服务器,Web服务器--保姆级教程! 本文首发于https://blog.chens.life/How-to-build-your-own-server.html. 先上图!大致思路就是如此. ...