PUN Classic (v1), PUN 2 and Bolt are in maintenance mode. PUN 2 will support Unity 2019 to 2022, but no new features will be added. Of course all your PUN & Bolt projects will continue to work and run with the known performance in the future. For any upcoming or new projects: please switch to Photon Fusion or Quantum.

UserIDs and Friends

UserIDs

在Photon中,玩家的身份是用一個唯一的UserID來識別的。

這個用戶ID在房間內和房間外都很有用。 這個用戶ID在房間內和房間外都很有用。 具有相同UserID的Photon客戶端可以連接到同一個伺服器,但是您不能從兩個不同的客戶端使用相同的UserID加入同一個Photon房間。 房間裡的每個actor都應該有一個唯一的UserID。 在舊的C#客戶端SDK中,這可以通過RoomOptions.CheckUserOnJoin來執行。

Back To Top
 

獨特的用戶ID

一般來說,UserIDs不打算被顯示。 與用戶名、顯示名或綽號不同。 用戶ID不一定是人類可讀的,也不一定是非常人性化的。 因此,您可以,例如:使用一個GUID作為UserID。

每個玩家保持一個唯一的UserID的好處是。

  • 您可以在不同的遊戲環節和多個設備之間保存您的數據。 您可以重新加入房間,在您停止的地方繼續遊戲。

  • 您可以被您遇到的所有玩家所認識,並且容易被大家所識別。 您可以與您的朋友一起玩,向他們發送邀請和挑戰,進行在線聚會,組建團隊和公會,等等。 您可以添加用戶檔案(如經驗、統計、成就、等級等),使遊戲更具挑戰性(也可以使用錦標賽和排行榜)。

  • 您可以利用另一個服務將Photon UserID綁定到一個外部的唯一標識。 例如,Photon UserID可以被設置為Facebook ID、Google ID、Steam ID、PlayFab ID等。

  • 您可以通過保留一個用戶ID的黑名單並使用以下方法來禁止惡意用戶連接到您的應用程序。

    自定義認証

Back To Top
 

設置用戶ID

Once authenticated, a Photon client will keep the same UserID until disconnected. The UserID for a client can be set in three ways:

  1. 客戶端在連接前通過設置AuthenticationValues.UserId來發送其UserID。 當您不使用

自定義認証 並希望設置一個用戶ID。 2. 外部認証提供者在認証成功後返回UserID:

自定義認証. 。它將覆蓋客戶端發送的任何值。 3. Photon伺服器將為那些沒有使用1或2獲得UserIDs的用戶分配GUIDs作為ID。因此,即使是匿名用戶也會有UserIDs。

Back To Top
 

發布用戶ID

玩家可以在房間內互相分享他們的UserIDs。 要啟用這個功能並使用戶ID對所有人可見,當您創建一個房間時,將RoomOptions.PublishUserId設置為true。 然後伺服器將在每個新加入的玩家中廣播這一信息,您可以使用PhotonPlayer.UserId訪問每個玩家的UserID。

Back To Top
 

朋友

  • 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伺服器上使用:

using System.Collections.Generic;
using UnityEngine;
using Photon.Realtime;
using Photon.Pun;

public class FindFriendsExample : MonoBehaviourPunCallbacks
{
    public bool FindFriends(string[] friendsUserIds)
    {
        return PhotonNetwork.FindFriends(friendsUserIds);
    }

    public override void OnFriendListUpdate(List<FriendInfo> friendsInfo)
    {
        for(int i=0; i < friendsInfo.Count; i++)
        {
            FriendInfo friend = friendsInfo[i];
            Debug.LogFormat("{0}", friend);
        }
    }
}

Photon不會持久保存好友列表。 您可能需要一個外部服務來執行。

由於Photon不跟蹤您的用戶群,任何在您遊戲中不存在的用戶都會被認為是離線的。 只有當一個朋友在進行FindFriends查詢時連接到Photon時,他/她才被認為是在線。 如果用戶在線並且加入了同名的房間,那麼房間名稱將被返回給每個用戶。


To Document Top