Unity_PUN2多人联机API汇总

PUN2多人联机API汇总

代码仅为函数区别和属性展示,并不针对指定功能
按照需要调用即可

服务器相关API和回调

c 复制代码
  #region 服务器相关
  public void 连接服务器()
  {
    PhotonNetwork.ConnectUsingSettings();
    PhotonNetwork.GameVersion = "1";
  }
  public void 离开服务器()
  {
    PhotonNetwork.Disconnect();
  }
  public override void OnConnectedToMaster()
  {
    Debug.Log("加入服务器时回调");
  }
  public override void OnConnected()
  {
    Debug.Log("连接到服务器时回调");
  }
  public override void OnDisconnected(DisconnectCause cause)
  {
    Debug.Log("离开服务器时回调");
  }
  #endregion

大厅相关API和回调

c 复制代码
 #region 大厅相关
  public void 加入大厅()
  {
    PhotonNetwork.JoinLobby();
    PhotonNetwork.CurrentLobby.Name = "山西一区";
    Debug.Log(PhotonNetwork.CurrentLobby.Name);
    Debug.Log(PhotonNetwork.CurrentLobby.Type); //大厅类型
  }
  public void 离开大厅()
  {
    PhotonNetwork.LeaveLobby();
  }
  public void 大厅()
  {
    TypedLobby tl = new TypedLobby("山西一区",LobbyType.Default); //创建大厅
    Debug.Log(tl.Name); // 大厅名字
    Debug.Log(tl.Type); //大厅类型
  }
  public void 大厅信息()
  {
    //大厅的子类
    TypedLobbyInfo tli = new TypedLobbyInfo();
    Debug.Log(tli.PlayerCount);//当前大厅内的玩家数
    Debug.Log(tli.RoomCount);  //当前大厅内的房间数
  }
  public override void OnJoinedLobby()
  {
    Debug.Log("加入大厅时回调");
  }
  public override void OnLeftLobby()
  {
    Debug.Log("离开大厅时回调");
  }
  #endregion

房间相关API和回调

c 复制代码
  #region 房间相关
  public void 创建房间()
  {
    RoomOptions ro = new RoomOptions();
    ro.IsOpen = true;
    ro.IsVisible = true;
    ro.MaxPlayers = 4;
    if (PhotonNetwork.CreateRoom("谢同学测试房间", ro))
    {
      Debug.Log("手动调用创建房间函数 ");
    }
  }
  public void 加入房间()
  {
    PhotonNetwork.JoinRoom("谢同学测试房间");
  }
  public void 离开房间()
  {
    PhotonNetwork.LeaveRoom();
  }
  public void 房间选项()
  {
    //用于创建房间
    RoomOptions ro = new RoomOptions();
    ro.IsVisible = true;                    //是否可见
    ro.IsOpen = true;                       //是否可以加入
    ro.MaxPlayers = 4;                      //最大人数设置
    ro.PlayerTtl = 1;                       //玩家断线超时删除时间
    ro.EmptyRoomTtl = 1;                    //空房间超时删除时间
    ro.CustomRoomProperties = null;         //自定义房间属性
    ro.CustomRoomPropertiesForLobby = null; //大厅中列出的自定义房间属性。
    ro.PublishUserId = true;                //房间内是否公开名字
    ro.BroadcastPropsChangeToAll = true;    //广播属性给所有玩家
  }
  public void 房间信息()
  {
    //用于大厅房间列表
    RoomInfo ri = null;
    Debug.Log(ri.RemovedFromList); //房间满了是否继续从列表中移除
    Debug.Log(ri.masterClientId);  //显示房主信息
    Debug.Log(ri.CustomProperties);//显示房间属性
    Debug.Log(ri.Name);           //显示房间名
    Debug.Log(ri.PlayerCount);    //显示当前房间人数
    Debug.Log(ri.MaxPlayers);     //显示房间最大人数
    Debug.Log(ri.IsOpen);         //显示房间是否公开
  }
  public void 房间()
  {
    //房间信息的子类
    Room ri = null;
    Debug.Log(ri.IsOffline);                  //是否离线
    Dictionary<int, Player> tmp = ri.Players; //房间中的玩家
    string[] tmp2 = ri.ExpectedUsers;         //预期加入的玩家列表,提前分配机票

  }
  public override void OnCreatedRoom()
  {
    Debug.Log("创建房间成功时回调");
  }
  public override void OnCreateRoomFailed(short returnCode, string message)
  {
    Debug.Log("创建房间失败时回调");
  }
  public override void OnJoinedRoom()
  {
    Debug.Log("加入房间时回调");
    Debug.Log("房间当前人数:" + PhotonNetwork.CurrentRoom.PlayerCount);
    PhotonNetwork.LoadLevel("Game");
  }
  public override void OnJoinRoomFailed(short returnCode, string message)
  {
    Debug.Log("加入房间失败时回调");
  }
  public override void OnLeftRoom()
  {
    Debug.Log("离开房间时调用");
  }
  public override void OnPlayerEnteredRoom(Player newPlayer)
  {
    Debug.Log(newPlayer.NickName + "进入了服务器");
  }
  #endregion

PhotonNetwork相关

c 复制代码
  #region PhotonNetwork相关
  public void 光子网络相关()
  {
    Debug.Log(PhotonNetwork.ServerAddress); //服务器地址
    Debug.Log(PhotonNetwork.CloudRegion);   //服务器区域
    Debug.Log(PhotonNetwork.AppVersion);    //应用版本
    Debug.Log(PhotonNetwork.GameVersion);   //游戏版本
    Debug.Log(PhotonNetwork.InRoom);        //是否在房间内
    Debug.Log(PhotonNetwork.InLobby);       //是否在大厅内
    Debug.Log(PhotonNetwork.IsConnected);   //是否连接服务器
    Debug.Log(PhotonNetwork.IsConnectedAndReady);//是否连接服务器并已准备
    Debug.Log(PhotonNetwork.IsMasterClient);//是否为主机
    Debug.Log(PhotonNetwork.LocalPlayer.NickName) ;//本地玩家名字
    Debug.Log(PhotonNetwork.MasterClient);  //返回主机玩家
    Debug.Log(PhotonNetwork.NetworkClientState);//网络客户端状态
    Debug.Log(PhotonNetwork.NickName);      //本机名字
    Debug.Log(PhotonNetwork.OfflineMode);
    Debug.Log(PhotonNetwork.PhotonServerSettings);
    Debug.Log(PhotonNetwork.PlayerList);
  }
  #endregion

PhotonStream相关

c 复制代码
  #region PhotonStream相关
  public void PhotonStream相关()
  {
    PhotonStream ps = new PhotonStream(true,new object[1]);
    object o1 = new object();
    Debug.Log(ps.IsReading); //是否可读
    Debug.Log(ps.IsWriting); //是否可写,只有本机才可以写
    Debug.Log(ps.Count);     //数量
    Debug.Log(ps.ReceiveNext());
    Debug.Log(ps.PeekNext());
  }
  #endregion

更新显示房间列表

c 复制代码
  public override void OnRoomListUpdate(List<RoomInfo> roomList)
  {
    Debug.Log("打印所有房间信息");
    foreach (RoomInfo item in roomList)
    {
      Debug.Log(item.Name);
    }
  }
相关推荐
独泪了无痕3 分钟前
Vue3 Hooks使用实战解析
前端·vue.js
shawxlee1 小时前
vue3在public下封装config.js自定义配置动态数据,可在打包后直接修改,方便后端部署及后续维护
前端·javascript·经验分享·vue·团队开发·js·项目优化
用户059540174462 小时前
把记忆存储的回归测试从手工换成 Playwright + GitHub Actions,线上缺陷降低 80%
前端·css
触底反弹2 小时前
🚀 从 DOM0 级到 React 合成事件:前端事件监听的 20 年演进史
前端·react.js·面试
kyriewen2 小时前
我给前端项目的接口请求套了6层保护——才发现以前一直在裸奔
前端·javascript·面试
颜酱2 小时前
04 | 召回前置准备:搭好召回所需的四个数据库
前端·人工智能·后端
郝亚军4 小时前
如何安装webstorm、Node.js和vue CLI
前端·javascript·vue.js
IT_陈寒4 小时前
React的useEffect依赖项把我坑惨了
前端·人工智能·后端
东方小月4 小时前
从零开发一个Coding Agent:monorepo项目搭建
前端·后端·node.js
葬送的代码人生4 小时前
别再让 AI 瞎写代码了!Vibe Coding 三步法教你写出靠谱代码
前端·设计模式·架构