UserIDs and Friends
UserIDs
在Photon中,玩家的身份是用一個唯一的UserID來識別的。
這個用戶ID在房間內和房間外都很有用。
這個用戶ID在房間內和房間外都很有用。
具有相同UserID的Photon客戶端可以連接到同一個伺服器,但是您不能從兩個不同的客戶端使用相同的UserID加入同一個Photon房間。
房間裡的每個actor都應該有一個唯一的UserID。
在舊的C#客戶端SDK中,這可以通過RoomOptions.CheckUserOnJoin
來執行。
獨特的用戶ID
一般來說,UserIDs不打算被顯示。
與用戶名、顯示名或綽號不同。
用戶ID不一定是人類可讀的,也不一定是非常人性化的。
因此,您可以,例如:使用一個GUID作為UserID。
每個玩家保持一個唯一的UserID的好處是。
您可以在不同的遊戲環節和多個設備之間保存您的數據。
您可以重新加入房間,在您停止的地方繼續遊戲。您可以被您遇到的所有玩家所認識,並且容易被大家所識別。
您可以與您的朋友一起玩,向他們發送邀請和挑戰,進行在線聚會,組建團隊和公會,等等。
您可以添加用戶檔案(如經驗、統計、成就、等級等),使遊戲更具挑戰性(也可以使用錦標賽和排行榜)。您可以利用另一個服務將Photon UserID綁定到一個外部的唯一標識。
例如,Photon UserID可以被設置為Facebook ID、Google ID、Steam ID、PlayFab ID等。您可以通過保留一個用戶ID的黑名單並使用以下方法來禁止惡意用戶連接到您的應用程序。
設置用戶ID
Once authenticated, a Photon client will keep the same UserID until disconnected.
The UserID for a client can be set in three ways:
- 客戶端在連接前通過設置
AuthenticationValues.UserId
來發送其UserID。
當您不使用
自定義認証
並希望設置一個用戶ID。
2. 外部認証提供者在認証成功後返回UserID:
見自定義認証.
。它將覆蓋客戶端發送的任何值。
3. Photon伺服器將為那些沒有使用1或2獲得UserIDs的用戶分配GUIDs作為ID。因此,即使是匿名用戶也會有UserIDs。
發布用戶ID
玩家可以在房間內互相分享他們的UserIDs。
要啟用這個功能並使用戶ID對所有人可見,當您創建一個房間時,將RoomOptions.PublishUserId
設置為true
。
然後伺服器將在每個新加入的玩家中廣播這一信息,您可以使用PhotonPlayer.UserId
訪問每個玩家的UserID。
Matchmaking Slot Reservation
Sometimes, a player joins a room, knowing that a friend should join as well.
With Slot Reservation, Photon can block a slot for specific users and take that into account for matchmaking.
To reserve slots there is an expectedUsers
parameter (exact parameter or argument name may vary depending on your client SDK) in the methods that get you in a room (JoinRoom
, JoinOrCreateRoom
, JoinRandomRoom
and CreateRoom
. Exact functions or methods names may vary depending on your client SDK).
C#
// create room example
PhotonNetwork.CreateRoom(roomName, roomOptions, typedLobby, expectedUsers);
// join room example
PhotonNetwork.JoinRoom(roomName, expectedUsers);
// join or create room example
PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, typedLobby, expectedUsers);
// join random room example
PhotonNetwork.JoinRandomRoom(expectedProperties, maxPlayers, expectedUsers, matchmakingType, typedLobby, sqlLobbyFilter, expectedUsers);
When you know someone should join, pass an array of UserIDs.
For JoinRandomRoom
, the server will attempt to find a room with enough slots for you and your expected players (plus all active and expected players already in the room).
The server will update clients in a room with the current expectedUsers
, should they change.
You can update the list of expected users inside a room (add or remove one or more users), this is done via a well known room property.
(In C# SDKs, you can get and set Room.ExpectedUsers
).
To support Slot Reservation, you need to enable publishing UserIDs inside rooms.
Example Use Case: Teams Matchmaking
You can use this to support teams in matchmaking.
The leader of a team does the actual matchmaking.
He/She can join a room and reserve slots for all members:
Try to find a random room:
C#
PhotonNetwork.JoinRandomRoom(expectedProperties, maxPlayers, expectedUsers, matchmakingType, typedLobby, sqlLobbyFilter, expectedUsers);
Create a new one if none found:
C#
PhotonNetwork.CreateRoom(roomName, roomOptions, typedLobby, teamMembersUserIds);
The others don't have to do any matchmaking but instead repeatedly call ('periodic poll', every few frames/(milli)seconds):
C#
PhotonNetwork.FindFriends(new string[1]{ leaderUserId });
When the leader arrives in a room, the FindFriends
operation will reveal that room's name and everyone can join it:
C#
PhotonNetwork.JoinRoom(roomNameWhereTheLeaderIs);
朋友
- Friends' UserIDs are case sensitive. Example: "mybestfriend" and "MyBestFriend" are two different UserIDs for two different friends.
- Only friends connected to the same AppID, the same Photon Cloud region and play the same Photon AppVersion can find each other no matter what device or platform they're using.
- FindFriends works only when connected to the Master Server, it does not work when the client is joined to a room.
您可以確認跟您玩同一個遊戲的朋友是否在線,甚至他們加入了哪個房間。
像用戶一樣,朋友也是用他們的UserID來識別的。
所以FriendID和UserID是一樣的,為了找到一些朋友,您需要先知道他們的UserID。
然後您就可以把UserID的列表發送到Photon伺服器上使用:
C#
using UnityEngine;
public class FindFriendsExample : MonoBehaviour
{
public bool FindFriends(string[] friendsUserIds)
{
return PhotonNetwork.FindFriends(friendsUserIds);
}
private void OnUpdatedFriendList()
{
for(int i=0; i < PhotonNetwork.Friends.Count; i++)
{
FriendInfo friend = PhotonNetwork.Friends[i];
Debug.LogFormat("{0}", friend);
}
}
}
Photon不會持久保存好友列表。
您可能需要一個外部服務來執行。
由於Photon不跟蹤您的用戶群,任何在您遊戲中不存在的用戶都會被認為是離線的。
只有當一個朋友在進行FindFriends查詢時連接到Photon時,他/她才被認為是在線。
如果用戶在線並且加入了同名的房間,那麼房間名稱將被返回給每個用戶。