UserIDs and Friends
UserIDs
在Photon中,玩家的身份是用一個唯一的UserID來識別的。
獨特的用戶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。
- 外部認証提供者在認証成功後返回UserID:見自定義認証。它將覆蓋客戶端發送的任何值。
- Photon伺服器將為那些沒有使用1或2獲得UserIDs的用戶分配GUIDs作為ID。因此,即使是匿名用戶也會有UserIDs。
朋友
- Friends' UserIDs are case sensitive. Example: "mybestfriend" and "MyBestFriend" are two different UserIDs for two different friends. ::: include condition="Chat PUN Realtime"
- 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. ::: ::: include condition="Server"
- Only friends connected to the same Master Server 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.
C#
using System.Collections.Generic;
using Photon.Realtime;
public class FindFriendsExample : IMatchmakingCallbacks
{
private LoadBalancingClient loadBalancingClient;
public bool FindFriends(string[] friendsUserIds)
{
return loadBalancingClient.OpFindFriends(friendsUserIds);
}
// do not forget to register callbacks via loadBalancingClient.AddCallbackTarget
// also deregister via loadBalancingClient.RemoveCallbackTarget
#region IMatchmakingCallbacks
public override void IMatchmakingCallbacks.OnFriendListUpdate(List<FriendInfo> friendsInfo)
{
for(int i=0; i < friendsInfo.Count; i++)
{
FriendInfo friend = friendsInfo[i];
Debug.LogFormat("{0}", friend);
}
}
// [..] Other callbacks implementations are stripped out for brevity, they are empty in this case as not used.
#endif
}
Photon不會持久保存好友列表。
您可能需要一個外部服務來執行。
由於Photon不跟蹤您的用戶群,任何在您遊戲中不存在的用戶都會被認為是離線的。
只有當一個朋友在進行FindFriends查詢時連接到Photon時,他/她才被認為是在線。
如果用戶在線並且加入了同名的房間,那麼房間名稱將被返回給每個用戶。