使用 PUN 2 在 Unity 中制作多人游戏
有没有想过如何在 Unity 中创建多人游戏?
与单人游戏不同,多人游戏需要远程服务器充当桥梁的角色,让游戏客户端能够相互通信。
如今,许多服务都负责服务器托管。其中一项服务是 Photon Network,我们将在本教程中使用它。
PUN 2 是其 API 的最新版本,与旧版本相比有了很大的改进。
在这篇文章中,我们将下载必要的文件、设置 Photon AppID 以及编写一个简单的多人游戏示例。
Unity 本教程使用的版本:Unity 2018.3.0f2(64位)
第 1 部分:设置 PUN 2
第一步是从 Asset Store 下载 PUN 2 包。它包含多人集成所需的所有脚本和文件。
- 打开 Unity 项目,然后转到 Asset Store:(窗口 -> 常规 -> AssetStore)或按 Ctrl+9
- 搜索“PUN 2- Free”,然后单击第一个结果或单击此处
- 下载完成后导入PUN 2包
- 导入包后,您需要创建一个 Photon App ID,这是在他们的网站上完成的:https://www.photonengine.com/
- 创建一个新帐户(或登录您现有的帐户)
- 单击配置文件图标,然后单击 "Your Applications" 或访问以下链接,转到“应用程序”页面:https://dashboard.photonengine.com/en-US/PublicCloud
- 在应用程序页面上单击 "Create new app"
- 在创建页面上,对于光子类型,选择 "Photon Realtime",对于名称,键入任意名称,然后单击 "Create"
如您所见,应用程序默认为免费计划。您可以在此处阅读有关定价计划 的更多信息
- 创建应用程序后,复制应用程序名称下的应用程序 ID
- 返回到您的 Unity 项目,然后转到 Window -> Photon Unity Networking -> PUN Wizard
- 在 PUN 向导中单击 "Setup Project",粘贴您的应用程序 ID,然后单击 "Setup Project"
- PUN 2 现已准备就绪!
第 2 部分:创建多人游戏
现在让我们进入实际创建多人游戏的部分。
PUN 2 中处理多人游戏的方式是:
- 首先,我们连接到光子区域(例如美国东部、欧洲、亚洲等),也称为大厅。
- 进入大厅后,我们请求该区域中创建的所有房间,然后我们可以加入其中一个房间或创建我们自己的房间。
- 加入房间后,我们请求连接到房间的玩家列表并实例化他们的 Player 实例,然后通过 PhotonView 与本地实例同步。
- 当有人离开房间时,他们的实例将被销毁,并从玩家列表中删除。
1. 设置大厅
让我们首先创建一个包含大厅逻辑的大厅场景(浏览现有房间、创建新房间等):
- 创建 一个新的 C# 脚本并将其命名为 PUN2_GameLobby
- 创建一个新场景并调用它 "GameLobby"
- 在 GameLobby 场景中创建一个新的 GameObject。将其命名为 "_GameLobby" 并将 PUN2_GameLobby 脚本分配给它
现在打开 PUN2_GameLobby 脚本:
首先,我们通过在脚本开头添加以下行来导入 Photon 名称空间:
using Photon.Pun;
using Photon.Realtime;
此外,在继续之前,我们需要将默认的 MonoBehaviour 替换为 MonoBehaviourPunCallbacks。为了能够使用 Photon 回调,必须执行此步骤:
public class PUN2_GameLobby : MonoBehaviourPunCallbacks
接下来,我们创建必要的变量:
//Our player name
string playerName = "Player 1";
//Users are separated from each other by gameversion (which allows you to make breaking changes).
string gameVersion = "0.9";
//The list of created rooms
List<RoomInfo> createdRooms = new List<RoomInfo>();
//Use this name when creating a Room
string roomName = "Room 1";
Vector2 roomListScroll = Vector2.zero;
bool joiningRoom = false;
然后我们在 void Start() 中调用 ConnectUsingSettings()。这意味着游戏一打开,它就会连接到 Photon 服务器:
// Use this for initialization
void Start()
{
//This makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
PhotonNetwork.AutomaticallySyncScene = true;
if (!PhotonNetwork.IsConnected)
{
//Set the App version before connecting
PhotonNetwork.PhotonServerSettings.AppSettings.AppVersion = gameVersion;
// Connect to the photon master-server. We use the settings saved in PhotonServerSettings (a .asset file in this project)
PhotonNetwork.ConnectUsingSettings();
}
}
要知道与 Photon 的连接是否成功,我们需要实现以下回调: OnDisconnected(DisconnectCause Cause)、OnConnectedToMaster()、OnRoomListUpdate(List<RoomInfo> roomList)
public override void OnDisconnected(DisconnectCause cause)
{
Debug.Log("OnFailedToConnectToPhoton. StatusCode: " + cause.ToString() + " ServerAddress: " + PhotonNetwork.ServerAddress);
}
public override void OnConnectedToMaster()
{
Debug.Log("OnConnectedToMaster");
//After we connected to Master server, join the Lobby
PhotonNetwork.JoinLobby(TypedLobby.Default);
}
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
Debug.Log("We have received the Room list");
//After this callback, update the room list
createdRooms = roomList;
}
接下来是UI部分,在这里完成Room浏览和Room创建:
void OnGUI()
{
GUI.Window(0, new Rect(Screen.width / 2 - 450, Screen.height / 2 - 200, 900, 400), LobbyWindow, "Lobby");
}
void LobbyWindow(int index)
{
//Connection Status and Room creation Button
GUILayout.BeginHorizontal();
GUILayout.Label("Status: " + PhotonNetwork.NetworkClientState);
if (joiningRoom || !PhotonNetwork.IsConnected || PhotonNetwork.NetworkClientState != ClientState.JoinedLobby)
{
GUI.enabled = false;
}
GUILayout.FlexibleSpace();
//Room name text field
roomName = GUILayout.TextField(roomName, GUILayout.Width(250));
if (GUILayout.Button("Create Room", GUILayout.Width(125)))
{
if (roomName != "")
{
joiningRoom = true;
RoomOptions roomOptions = new RoomOptions();
roomOptions.IsOpen = true;
roomOptions.IsVisible = true;
roomOptions.MaxPlayers = (byte)10; //Set any number
PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
}
}
GUILayout.EndHorizontal();
//Scroll through available rooms
roomListScroll = GUILayout.BeginScrollView(roomListScroll, true, true);
if (createdRooms.Count == 0)
{
GUILayout.Label("No Rooms were created yet...");
}
else
{
for (int i = 0; i < createdRooms.Count; i++)
{
GUILayout.BeginHorizontal("box");
GUILayout.Label(createdRooms[i].Name, GUILayout.Width(400));
GUILayout.Label(createdRooms[i].PlayerCount + "/" + createdRooms[i].MaxPlayers);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Join Room"))
{
joiningRoom = true;
//Set our Player name
PhotonNetwork.NickName = playerName;
//Join the Room
PhotonNetwork.JoinRoom(createdRooms[i].Name);
}
GUILayout.EndHorizontal();
}
}
GUILayout.EndScrollView();
//Set player name and Refresh Room button
GUILayout.BeginHorizontal();
GUILayout.Label("Player Name: ", GUILayout.Width(85));
//Player name text field
playerName = GUILayout.TextField(playerName, GUILayout.Width(250));
GUILayout.FlexibleSpace();
GUI.enabled = (PhotonNetwork.NetworkClientState == ClientState.JoinedLobby || PhotonNetwork.NetworkClientState == ClientState.Disconnected) && !joiningRoom;
if (GUILayout.Button("Refresh", GUILayout.Width(100)))
{
if (PhotonNetwork.IsConnected)
{
//Re-join Lobby to get the latest Room list
PhotonNetwork.JoinLobby(TypedLobby.Default);
}
else
{
//We are not connected, estabilish a new connection
PhotonNetwork.ConnectUsingSettings();
}
}
GUILayout.EndHorizontal();
if (joiningRoom)
{
GUI.enabled = true;
GUI.Label(new Rect(900 / 2 - 50, 400 / 2 - 10, 100, 20), "Connecting...");
}
}
最后,我们实现另外 4 个回调:OnCreateRoomFailed(short returnCode, string message)、OnJoinRoomFailed(short returnCode, string message)、OnCreatedRoom() 和OnJoinedRoom()。
这些回调用于确定我们是否加入/创建了房间或者连接过程中是否存在任何问题。
这是最终的 PUN2_GameLobby.cs 脚本:
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
public class PUN2_GameLobby : MonoBehaviourPunCallbacks
{
//Our player name
string playerName = "Player 1";
//Users are separated from each other by gameversion (which allows you to make breaking changes).
string gameVersion = "0.9";
//The list of created rooms
List<RoomInfo> createdRooms = new List<RoomInfo>();
//Use this name when creating a Room
string roomName = "Room 1";
Vector2 roomListScroll = Vector2.zero;
bool joiningRoom = false;
// Use this for initialization
void Start()
{
//This makes sure we can use PhotonNetwork.LoadLevel() on the master client and all clients in the same room sync their level automatically
PhotonNetwork.AutomaticallySyncScene = true;
if (!PhotonNetwork.IsConnected)
{
//Set the App version before connecting
PhotonNetwork.PhotonServerSettings.AppSettings.AppVersion = gameVersion;
// Connect to the photon master-server. We use the settings saved in PhotonServerSettings (a .asset file in this project)
PhotonNetwork.ConnectUsingSettings();
}
}
public override void OnDisconnected(DisconnectCause cause)
{
Debug.Log("OnFailedToConnectToPhoton. StatusCode: " + cause.ToString() + " ServerAddress: " + PhotonNetwork.ServerAddress);
}
public override void OnConnectedToMaster()
{
Debug.Log("OnConnectedToMaster");
//After we connected to Master server, join the Lobby
PhotonNetwork.JoinLobby(TypedLobby.Default);
}
public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
Debug.Log("We have received the Room list");
//After this callback, update the room list
createdRooms = roomList;
}
void OnGUI()
{
GUI.Window(0, new Rect(Screen.width / 2 - 450, Screen.height / 2 - 200, 900, 400), LobbyWindow, "Lobby");
}
void LobbyWindow(int index)
{
//Connection Status and Room creation Button
GUILayout.BeginHorizontal();
GUILayout.Label("Status: " + PhotonNetwork.NetworkClientState);
if (joiningRoom || !PhotonNetwork.IsConnected || PhotonNetwork.NetworkClientState != ClientState.JoinedLobby)
{
GUI.enabled = false;
}
GUILayout.FlexibleSpace();
//Room name text field
roomName = GUILayout.TextField(roomName, GUILayout.Width(250));
if (GUILayout.Button("Create Room", GUILayout.Width(125)))
{
if (roomName != "")
{
joiningRoom = true;
RoomOptions roomOptions = new RoomOptions();
roomOptions.IsOpen = true;
roomOptions.IsVisible = true;
roomOptions.MaxPlayers = (byte)10; //Set any number
PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
}
}
GUILayout.EndHorizontal();
//Scroll through available rooms
roomListScroll = GUILayout.BeginScrollView(roomListScroll, true, true);
if (createdRooms.Count == 0)
{
GUILayout.Label("No Rooms were created yet...");
}
else
{
for (int i = 0; i < createdRooms.Count; i++)
{
GUILayout.BeginHorizontal("box");
GUILayout.Label(createdRooms[i].Name, GUILayout.Width(400));
GUILayout.Label(createdRooms[i].PlayerCount + "/" + createdRooms[i].MaxPlayers);
GUILayout.FlexibleSpace();
if (GUILayout.Button("Join Room"))
{
joiningRoom = true;
//Set our Player name
PhotonNetwork.NickName = playerName;
//Join the Room
PhotonNetwork.JoinRoom(createdRooms[i].Name);
}
GUILayout.EndHorizontal();
}
}
GUILayout.EndScrollView();
//Set player name and Refresh Room button
GUILayout.BeginHorizontal();
GUILayout.Label("Player Name: ", GUILayout.Width(85));
//Player name text field
playerName = GUILayout.TextField(playerName, GUILayout.Width(250));
GUILayout.FlexibleSpace();
GUI.enabled = (PhotonNetwork.NetworkClientState == ClientState.JoinedLobby || PhotonNetwork.NetworkClientState == ClientState.Disconnected) && !joiningRoom;
if (GUILayout.Button("Refresh", GUILayout.Width(100)))
{
if (PhotonNetwork.IsConnected)
{
//Re-join Lobby to get the latest Room list
PhotonNetwork.JoinLobby(TypedLobby.Default);
}
else
{
//We are not connected, estabilish a new connection
PhotonNetwork.ConnectUsingSettings();
}
}
GUILayout.EndHorizontal();
if (joiningRoom)
{
GUI.enabled = true;
GUI.Label(new Rect(900 / 2 - 50, 400 / 2 - 10, 100, 20), "Connecting...");
}
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
Debug.Log("OnCreateRoomFailed got called. This can happen if the room exists (even if not visible). Try another room name.");
joiningRoom = false;
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
Debug.Log("OnJoinRoomFailed got called. This can happen if the room is not existing or full or closed.");
joiningRoom = false;
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("OnJoinRandomFailed got called. This can happen if the room is not existing or full or closed.");
joiningRoom = false;
}
public override void OnCreatedRoom()
{
Debug.Log("OnCreatedRoom");
//Set our player name
PhotonNetwork.NickName = playerName;
//Load the Scene called GameLevel (Make sure it's added to build settings)
PhotonNetwork.LoadLevel("GameLevel");
}
public override void OnJoinedRoom()
{
Debug.Log("OnJoinedRoom");
}
}
2. 创建播放器预制件
在多人游戏中,Player 实例有 2 个侧面:本地和远程
本地实例由本地(由我们)控制。
另一方面,远程实例是其他玩家正在执行的操作的本地表示。它应该不受我们输入的影响。
为了确定实例是本地实例还是远程实例,我们使用 PhotonView 组件。
PhotonView 充当接收和发送需要同步的值的信使,例如位置和旋转。
因此,让我们从创建播放器实例开始(如果您已经准备好播放器实例,则可以跳过此步骤)。
在我的例子中,Player 实例将是一个简单的立方体,使用 W 和 S 键移动并使用 A 和 D 键旋转。
这是一个简单的控制器脚本:
SimplePlayerController.cs
using UnityEngine;
public class SimplePlayerController : MonoBehaviour
{
// Update is called once per frame
void Update()
{
//Move Front/Back
if (Input.GetKey(KeyCode.W))
{
transform.Translate(transform.forward * Time.deltaTime * 2.45f, Space.World);
}
else if (Input.GetKey(KeyCode.S))
{
transform.Translate(-transform.forward * Time.deltaTime * 2.45f, Space.World);
}
//Rotate Left/Right
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(new Vector3(0, -14, 0) * Time.deltaTime * 4.5f, Space.Self);
}
else if (Input.GetKey(KeyCode.D))
{
transform.Rotate(new Vector3(0, 14, 0) * Time.deltaTime * 4.5f, Space.Self);
}
}
}
下一步是添加 PhotonView 组件。
- 将 PhotonView 组件添加到 Player 实例。
- 创建一个新的 C# 脚本,并将其命名为 PUN2_PlayerSync(该脚本将用于通过 PhotonView 进行通信)。
打开 PUN2_PlayerSync 脚本:
在 PUN2_PlayerSync 中,我们需要做的第一件事是添加 Photon.Pun 命名空间并将 MonoBehaviour 替换为 MonoBehaviourPun 并添加 IPunObservable 接口。
MonoBehaviourPun 必须能够使用缓存的 photonView 变量,而不是使用 GetComponent<PhotonView>()。
using UnityEngine;
using Photon.Pun;
public class PUN2_PlayerSync : MonoBehaviourPun, IPunObservable
之后,我们可以创建所有必要的变量:
//List of the scripts that should only be active for the local player (ex. PlayerController, MouseLook etc.)
public MonoBehaviour[] localScripts;
//List of the GameObjects that should only be active for the local player (ex. Camera, AudioListener etc.)
public GameObject[] localObjects;
//Values that will be synced over network
Vector3 latestPos;
Quaternion latestRot;
然后在 void Start() 中,我们使用 photonView.IsMine 检查玩家是本地还是远程:
// Use this for initialization
void Start()
{
if (photonView.IsMine)
{
//Player is local
}
else
{
//Player is Remote, deactivate the scripts and object that should only be enabled for the local player
for (int i = 0; i < localScripts.Length; i++)
{
localScripts[i].enabled = false;
}
for (int i = 0; i < localObjects.Length; i++)
{
localObjects[i].SetActive(false);
}
}
}
实际的同步是通过 PhotonView 的回调完成的: OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info):
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
//We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
//Network player, receive data
latestPos = (Vector3)stream.ReceiveNext();
latestRot = (Quaternion)stream.ReceiveNext();
}
}
在本例中,我们仅发送玩家位置和旋转,但您可以使用上面的示例以高频率发送需要通过网络同步的任何值。
然后将接收到的值应用到 void Update() 中:
// Update is called once per frame
void Update()
{
if (!photonView.IsMine)
{
//Update remote player (smooth this, this looks good, at the cost of some accuracy)
transform.position = Vector3.Lerp(transform.position, latestPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, latestRot, Time.deltaTime * 5);
}
}
}
这是最终的 PUN2_PlayerSync.cs 脚本:
using UnityEngine;
using Photon.Pun;
public class PUN2_PlayerSync : MonoBehaviourPun, IPunObservable
{
//List of the scripts that should only be active for the local player (ex. PlayerController, MouseLook etc.)
public MonoBehaviour[] localScripts;
//List of the GameObjects that should only be active for the local player (ex. Camera, AudioListener etc.)
public GameObject[] localObjects;
//Values that will be synced over network
Vector3 latestPos;
Quaternion latestRot;
// Use this for initialization
void Start()
{
if (photonView.IsMine)
{
//Player is local
}
else
{
//Player is Remote, deactivate the scripts and object that should only be enabled for the local player
for (int i = 0; i < localScripts.Length; i++)
{
localScripts[i].enabled = false;
}
for (int i = 0; i < localObjects.Length; i++)
{
localObjects[i].SetActive(false);
}
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
//We own this player: send the others our data
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
//Network player, receive data
latestPos = (Vector3)stream.ReceiveNext();
latestRot = (Quaternion)stream.ReceiveNext();
}
}
// Update is called once per frame
void Update()
{
if (!photonView.IsMine)
{
//Update remote player (smooth this, this looks good, at the cost of some accuracy)
transform.position = Vector3.Lerp(transform.position, latestPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, latestRot, Time.deltaTime * 5);
}
}
}
现在让我们分配一个新创建的脚本:
- 将 PUN2_PlayerSync 脚本附加到 PlayerInstance。
- 将 PUN2_PlayerSync 拖放到 PhotonView 观察组件中。
- 将 SimplePlayerController 分配给 "Local Scripts" 并将 GameObjects(您想要为远程玩家停用的游戏对象)分配给 "Local Objects"
- 将 PlayerInstance 保存到 Prefab 并将其移动到名为 Resources 的文件夹(如果没有这样的文件夹,请创建一个)。为了能够通过网络生成多人游戏对象,此步骤是必需的。
3. 创建游戏关卡
GameLevel 是加入房间后加载的场景,它是所有动作发生的地方。
- 创建一个新场景并将其命名为 "GameLevel" (或者,如果您想保留不同的名称,请确保更改 PUN2_GameLobby.cs 中 PhotonNetwork.LoadLevel("GameLevel"); 行中的名称)。
就我而言,我将使用一个带有平面的简单场景:
- 现在创建一个新脚本并将其命名为 PUN2_RoomController (该脚本将处理房间内的逻辑,例如生成玩家、显示玩家列表等)。
打开 PUN2_RoomController 脚本:
与 PUN2_GameLobby 相同,我们首先添加 Photon 命名空间并将 MonoBehaviour 替换为 MonoBehaviourPunCallbacks:
using UnityEngine;
using Photon.Pun;
public class PUN2_RoomController : MonoBehaviourPunCallbacks
现在让我们添加必要的变量:
//Player instance prefab, must be located in the Resources folder
public GameObject playerPrefab;
//Player spawn point
public Transform spawnPoint;
为了实例化 Player 预制件,我们使用 PhotonNetwork.Instantiate:
// Use this for initialization
void Start()
{
//In case we started this demo with the wrong scene being active, simply load the menu scene
if (PhotonNetwork.CurrentRoom == null)
{
Debug.Log("Is not in the room, returning back to Lobby");
UnityEngine.SceneManagement.SceneManager.LoadScene("GameLobby");
return;
}
//We're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
PhotonNetwork.Instantiate(playerPrefab.name, spawnPoint.position, Quaternion.identity, 0);
}
以及一个带有 "Leave Room" 按钮和一些附加元素(例如房间名称和已连接玩家列表)的简单 UI:
void OnGUI()
{
if (PhotonNetwork.CurrentRoom == null)
return;
//Leave this Room
if (GUI.Button(new Rect(5, 5, 125, 25), "Leave Room"))
{
PhotonNetwork.LeaveRoom();
}
//Show the Room name
GUI.Label(new Rect(135, 5, 200, 25), PhotonNetwork.CurrentRoom.Name);
//Show the list of the players connected to this Room
for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++)
{
//Show if this player is a Master Client. There can only be one Master Client per Room so use this to define the authoritative logic etc.)
string isMasterClient = (PhotonNetwork.PlayerList[i].IsMasterClient ? ": MasterClient" : "");
GUI.Label(new Rect(5, 35 + 30 * i, 200, 25), PhotonNetwork.PlayerList[i].NickName + isMasterClient);
}
}
最后,我们实现另一个名为 OnLeftRoom() 的 PhotonNetwork 回调,当我们离开房间时会调用它:
public override void OnLeftRoom()
{
//We have left the Room, return back to the GameLobby
UnityEngine.SceneManagement.SceneManager.LoadScene("GameLobby");
}
以下是最终的 PUN2_RoomController.cs 脚本:
using UnityEngine;
using Photon.Pun;
public class PUN2_RoomController : MonoBehaviourPunCallbacks
{
//Player instance prefab, must be located in the Resources folder
public GameObject playerPrefab;
//Player spawn point
public Transform spawnPoint;
// Use this for initialization
void Start()
{
//In case we started this demo with the wrong scene being active, simply load the menu scene
if (PhotonNetwork.CurrentRoom == null)
{
Debug.Log("Is not in the room, returning back to Lobby");
UnityEngine.SceneManagement.SceneManager.LoadScene("GameLobby");
return;
}
//We're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
PhotonNetwork.Instantiate(playerPrefab.name, spawnPoint.position, Quaternion.identity, 0);
}
void OnGUI()
{
if (PhotonNetwork.CurrentRoom == null)
return;
//Leave this Room
if (GUI.Button(new Rect(5, 5, 125, 25), "Leave Room"))
{
PhotonNetwork.LeaveRoom();
}
//Show the Room name
GUI.Label(new Rect(135, 5, 200, 25), PhotonNetwork.CurrentRoom.Name);
//Show the list of the players connected to this Room
for (int i = 0; i < PhotonNetwork.PlayerList.Length; i++)
{
//Show if this player is a Master Client. There can only be one Master Client per Room so use this to define the authoritative logic etc.)
string isMasterClient = (PhotonNetwork.PlayerList[i].IsMasterClient ? ": MasterClient" : "");
GUI.Label(new Rect(5, 35 + 30 * i, 200, 25), PhotonNetwork.PlayerList[i].NickName + isMasterClient);
}
}
public override void OnLeftRoom()
{
//We have left the Room, return back to the GameLobby
UnityEngine.SceneManagement.SceneManager.LoadScene("GameLobby");
}
}
- 在 'GameLevel' 场景中创建一个新的 GameObject 并调用它 "_RoomController"
- 将 PUN2_RoomController 脚本附加到 _RoomController 对象
- 为其分配 PlayerInstance 预制件和 SpawnPoint Transform,然后保存场景
- 将 MainMenu 和 GameLevel 添加到构建设置中。
4. 进行测试构建
现在是时候进行构建并测试它了:
一切都按预期进行!
奖金
远程过程调用
在 PUN 2 中,RPC 代表远程过程调用,它用于调用位于同一房间的远程客户端上的函数(您可以阅读更多相关信息这里)。
RPC 有很多用途,例如,假设您需要向房间中的所有玩家发送聊天消息。使用 RPC,可以轻松做到这一点:
[PunRPC]
void ChatMessage(string senderName, string messageText)
{
Debug.Log(string.Format("{0}: {1}", senderName, messageText));
}
请注意该函数之前的 [PunRPC]。如果您计划通过 RPC 调用该函数,则此属性是必需的。
要调用标记为 RPC 的函数,您需要一个 PhotonView。调用示例:
PhotonView photonView = PhotonView.Get(this);
photonView.RPC("ChatMessage", RpcTarget.All, PhotonNetwork.playerName, "Some message");
专业提示:如果您将脚本中的 MonoBehaviour 替换为 MonoBehaviourPun 或 MonoBehaviourPunCallbacks 您可以跳过 PhotonView.Get() 并直接使用 photonView.RPC() 。
自定义属性
在 PUN 2 中,自定义属性是一个可以分配给玩家或房间的哈希表。
当您需要设置不需要经常更改的持久数据(例如玩家团队名称、房间游戏模式等)时,这非常有用。
首先,您必须定义一个哈希表,这是通过在脚本开头添加以下行来完成的:
//Replace default Hashtables with Photon hashtables
using Hashtable = ExitGames.Client.Photon.Hashtable;
下面的示例设置名为 "GameMode" 和 "AnotherProperty" 的 Room 属性:
//Set Room properties (Only Master Client is allowed to set Room properties)
if (PhotonNetwork.IsMasterClient)
{
Hashtable setRoomProperties = new Hashtable();
setRoomProperties.Add("GameMode", "FFA");
setRoomProperties.Add("AnotherProperty", "Test");
PhotonNetwork.CurrentRoom.SetCustomProperties(setRoomProperties);
}
//Will print "FFA"
print((string)PhotonNetwork.CurrentRoom.CustomProperties["GameMode"]);
//Will print "Test"
print((string)PhotonNetwork.CurrentRoom.CustomProperties["AnotherProperty"]);
玩家属性的设置类似:
Hashtable setPlayerProperties = new Hashtable();
setPlayerProperties.Add("PlayerHP", (float)100);
PhotonNetwork.LocalPlayer.SetCustomProperties(setPlayerProperties);
print((float)PhotonNetwork.LocalPlayer.CustomProperties["PlayerHP"]);
要删除特定属性,只需将其值设置为 null。
Hashtable setPlayerProperties = new Hashtable();
setPlayerProperties.Add("PlayerHP", null);
PhotonNetwork.LocalPlayer.SetCustomProperties(setPlayerProperties);
附加教程: