• Unity自用工具.Photon大厅


    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8. using Random = UnityEngine.Random;
    9. public class Lobby : MonoBehaviourPunCallbacks
    10. {
    11. public Text Info;
    12. public static Text S_info;
    13. private int _timeOff=1;
    14. public static int networktype;
    15. public void Start()
    16. {
    17. S_info = Info;
    18. networktype++;
    19. ConnectToPhoton();
    20. }
    21. public void ConnectToPhoton()
    22. {
    23. PhotonNetwork.GameVersion = "1.0";
    24. PhotonNetwork.ConnectUsingSettings();
    25. }
    26. public void FixedUpdate()
    27. {
    28. switch (networktype)
    29. {
    30. case 1:
    31. Info.text = $"正在链接服务器({_timeOff}%)";
    32. break;
    33. case 2:
    34. Info.text = $"正在加入大厅({_timeOff}%)";
    35. break;
    36. case 101:
    37. try
    38. {
    39. Info.text = $"创建房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是拥有者";
    40. networktype = 102;
    41. }
    42. catch (NullReferenceException e)
    43. {
    44. Info.text = $"房间正在创建({((_timeOff>98)? _timeOff : 98)}%)";
    45. if (_timeOff>150)
    46. {
    47. networktype = 0;
    48. Info.text =
    49. "创建房间失败(Connection lost. OnStatusChanged to TimeoutDisconnect. Client state was: ConnectingToGameServer. SCS v0 UDP SocketErrorCode: 0 WinSock)";
    50. }
    51. }
    52. break;
    53. case 102:
    54. Info.text = $"创建房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是拥有者";
    55. break;
    56. case 201:
    57. try
    58. {
    59. Info.text = $"加入房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是客人";
    60. networktype = 202;
    61. }
    62. catch (NullReferenceException e)
    63. {
    64. Info.text = $"房间正在加入({_timeOff}%)";
    65. }
    66. break;
    67. case 202:
    68. Info.text = $"加入房间成功!您的房间名为(Test)\n房间人数:{PhotonNetwork.CurrentRoom.PlayerCount};您是客人";
    69. break;
    70. }
    71. int gt = Random.Range(0, 20);
    72. if (gt >16)
    73. {
    74. if (_timeOff<98)
    75. {
    76. _timeOff += Random.Range(1, 3);
    77. }
    78. }
    79. }
    80. public override void OnConnectedToMaster()
    81. {
    82. _timeOff = 0;
    83. networktype++;
    84. Debug.Log("Connected to Photon Server.");
    85. JoinLobby();
    86. }
    87. public void JoinLobby()
    88. {
    89. PhotonNetwork.JoinLobby();
    90. }
    91. public void BTNCRoom()
    92. {
    93. NetworkManager.IsMulitPlayer = true;
    94. CreateRoom("Test",5);
    95. }
    96. public void BNTJRoom()
    97. {
    98. NetworkManager.IsMulitPlayer = true;
    99. JoinRoom("Test");
    100. }
    101. public void GameStart()
    102. {
    103. if (networktype==102)
    104. {
    105. PhotonNetwork.LoadLevel("Bag");
    106. }
    107. }
    108. public void CreateRoom(string roomName, int maxPlayers)
    109. {
    110. if (networktype==0)
    111. {
    112. networktype++;
    113. ConnectToPhoton();
    114. }
    115. PhotonNetwork.AutomaticallySyncScene = true;
    116. networktype = 101;
    117. RoomOptions roomOptions = new RoomOptions
    118. {
    119. MaxPlayers = (byte)maxPlayers
    120. };
    121. try
    122. {
    123. PhotonNetwork.CreateRoom(roomName, roomOptions);
    124. }
    125. catch (Exception e)
    126. {
    127. Console.WriteLine(e);
    128. throw;
    129. }
    130. }
    131. public void JoinRoom(string roomName)
    132. {
    133. PhotonNetwork.AutomaticallySyncScene = true;
    134. networktype = 201;
    135. PhotonNetwork.JoinRoom(roomName);
    136. }
    137. public override void OnJoinedLobby()
    138. {
    139. _timeOff = 0;
    140. networktype++;
    141. Info.text = "准备完成";
    142. Debug.Log("Joined Lobby.");
    143. // You can perform additional actions here, like creating a room or listing available rooms.
    144. }
    145. public override void OnCreatedRoom()
    146. {
    147. Debug.Log("Room created successfully.");
    148. // You can perform additional actions when the room is created successfully.
    149. }
    150. public override void OnJoinRoomFailed(short returnCode, string message)
    151. {
    152. Debug.LogError("Failed to join room: " + message);
    153. }
    154. }

  • 相关阅读:
    弘辽科技:淘宝咨询量少的原因是什么?如何提高点击率?
    Glide源码解析二---into方法
    vue案例
    redis
    假冒 Skype 应用程序网络钓鱼分析
    部署Kubernetes Dashboard
    网络安全运维工程师数据库的核心能力有什么?
    自游家NV回应:所有订单将在48小时内退款
    CNN经典模型之ALexNet、ResNet、DenseNet总结和比较
    FastAPI 学习之路(二十一)请求体 - 更新数据
  • 原文地址:https://blog.csdn.net/qq_58804985/article/details/133564105