This document is about: PUN 2
SWITCH TO

PUN Classic (v1)、PUN 2 和 Bolt 處於維護模式。 PUN 2 將支援 Unity 2019 至 2022,但不會添加新功能。 當然,您所有的 PUN & Bolt 專案可以用已知性能繼續運行使用。 對於任何即將開始或新的專案:請切換到 Photon Fusion 或 Quantum。

Photon Steam Authentication

應用程序設置

添加Steam作為認証提供者很簡單,可以在幾秒鐘內從您的Photon應用程序介面完成。
進入應用程序的 "管理 "頁面,向下滑動到 "認証"部分。

如果您為Steam添加一個新的認証提供者或編輯一個現有的認証提供者,此為強制性的設置:

  • apiKeySecret:Steam發行商網絡API密鑰。不要把它與Steam用戶密鑰混淆。請閱讀更多關於如何獲取點此
  • appid:Steam遊戲的ID。您可以在通過Steam Direct程序(以前稱為Steam Greenlight)後得到一個。
  • verifyOwnership:可以是truefalse:是否在認証過程中啟用所有權驗証。
    這允許您驗証用戶是否真的擁有(購買了遊戲並在他的資料庫中)該遊戲。
    這個步驟,如果啟用,將在驗証用戶的會話票後執行。
    啟用這個功能可能會在驗証中增加額外的延遲,所以只有在您真的需要時才啟用它。
  • 驗証VacBan:可以是truefalse。在認証過程中是否檢查用戶是否被Valve的反作弊(VAC)禁止。
    閱讀更多 點此
    啟用這個功能可能會增加認証的延遲,所以只有在真正需要時才啟用。
  • 驗証PubBan:可以是truefalse。在認証過程中是否檢查用戶是否被禁止使用發布者禁令。
    閱讀更多 點此
    啟用這個功能可能會在認証中增加額外的延遲,所以只有在真正需要的時候才啟用它。

客戶端代碼 (Unity)

客戶端必須使用Valve的Steamworks API來獲得一個會話票。
這個票據可以証明客戶端是一個有效的Steam用戶。

Steamworks.NET

Steamworks.NET是一個流行的免費開源的Steamworks API包裝器。
按照本頁面列出的說明導入Unity版本的Steamworks.NET。

獲取票據

使用下面的代碼,使用Steamworks API獲得一個會話票,並將其轉換為十六進制編碼的UTF-8字符串:

C#

// hAuthTicket should be saved so you can use it to cancel the ticket as soon as you are done with it
public string GetSteamAuthTicket(out HAuthTicket hAuthTicket)
{
    byte[] ticketByteArray = new byte[1024];
    uint ticketSize;
    hAuthTicket = SteamUser.GetAuthSessionTicket(ticketByteArray, ticketByteArray.Length, out ticketSize);
    System.Array.Resize(ref ticketByteArray, (int)ticketSize);
    StringBuilder sb = new StringBuilder();
    for(int i=0; i < ticketSize; i++)
    {
        sb.AppendFormat("{0:x2}", ticketByteArray[i]);
    }
    return sb.ToString();
}

發送票據

客戶端必須將用戶的會話票(在轉換為十六進制編碼的UTF-8字符串後)作為查詢字符串鍵 "ticket "的值發送。

C#

PhotonNetwork.AuthValues = new AuthenticationValues();
PhotonNetwork.AuthValues.UserId = SteamUser.GetSteamID().ToString();
PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Steam;
PhotonNetwork.AuthValues.AddAuthParameter("ticket", SteamAuthSessionTicket);
// connect

取消票據

建議在認証完成後取消或撤銷票據。

在PUN 2中,第一個被觸發的回調是IConnectionCallbacks.OnConnected,您可以在此取消票據:

C#

private void OnConnected()
{
    SteamUser.CancelAuthTicket(hAuthTicket);
}

您也可以在其他回調中取消票據,如IConnectionCallbacks.OnConnectedToMasterIConnectionCallbacks.OnCustomAuthenticationFail或甚至IConnectionCallbacks.OnDisconnected

完整片段

這是一個最小的、自成一體的例子,並不意味著可以完整地復制/粘貼,並按原樣使用。

C#

using Photon.Pun;
using Photon.Realtime;
using Steamworks;

public class MinimalSteamworksPunAuth : MonoBehaviourPunCallbacks
{
    private HAuthTicket hAuthTicket;

    private void Start()
    {
        if (SteamManager.Initialized)
        {
            Connect();
        }
    }

    public void Connect()
    {
        string SteamAuthSessionTicket = GetSteamAuthTicket(out hAuthTicket);
        PhotonNetwork.AuthValues = new AuthenticationValues();
        PhotonNetwork.AuthValues.UserId = SteamUser.GetSteamID().ToString();
        PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Steam;
        PhotonNetwork.AuthValues.AddAuthParameter("ticket", SteamAuthSessionTicket);
        PhotonNetwork.ConnectUsingSettings();
    }

    public override void OnConnectedToMaster()
    {
        CancelAuthTicket(hAuthTicket);
    }

    public override void OnCustomAuthenticationFailed(string errorMessage)
    {
        CancelAuthTicket(hAuthTicket);
    }

    // ticket should be saved so you can use it to cancel the ticket as soon as you are done with it
    private string GetSteamAuthTicket(out HAuthTicket ticket)
    {
        byte[] ticketByteArray = new byte[1024];
        uint ticketSize;
        ticket = SteamUser.GetAuthSessionTicket(ticketByteArray, ticketByteArray.Length, out ticketSize);
        System.Array.Resize(ref ticketByteArray, (int)ticketSize);
        StringBuilder sb = new StringBuilder();
        for(int i=0; i < ticketSize; i++)
        {
            sb.AppendFormat("{0:x2}", ticketByteArray[i]);
        }
        return sb.ToString();
    }

  private void CancelAuthTicket(HAuthTicket ticket)
  {
      if (ticket != null)
      {
          SteamUser.CancelAuthTicket(ticket);
      }
  }
}

Facepunch.Steamworks

Facepunch.Steamworks是Steamworks API的另一個免費和開源的實現。
按照本頁所列的說明來導入Facepunch.Steamworks。

獲取票據

使用下面的代碼來獲取一個會話票據,並將其轉換為十六進制編碼的UTF-8字符串:

C#

// authTicket should be saved so you can use it to cancel the ticket as soon as you are done with it
public string GetSteamAuthTicket(out AuthTicket authTicket)
{
    authTicket = SteamUser.GetAuthSessionTicket();
    StringBuilder ticketString = new StringBuilder();
    for (int i = 0; i < authTicket.Data.Length; i++)
    {
        ticketString.AppendFormat("{0:x2}", authTicket.Data[i]);
    }
    return ticketString.ToString();
}

發送票據

客戶端必須將用戶的會話票(在轉換為十六進制編碼的UTF-8字符串後)作為查詢字符串鍵 "ticket "的值發送。

C#

PhotonNetwork.AuthValues = new AuthenticationValues();
PhotonNetwork.AuthValues.UserId = SteamClient.SteamId.ToString();
PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Steam;
PhotonNetwork.AuthValues.AddAuthParameter("ticket", steamAuthSessionTicket);
// connect

取消票據

建議在認証完成後取消或撤銷票據。

在PUN 2中,第一個被觸發的回調是IConnectionCallbacks.OnConnected,您可以在此取消票據:

C#

private void OnConnected()
{
    ticket.Cancel();
}

您也可以在其他回調中取消票據,如IConnectionCallbacks.OnConnectedToMasterIConnectionCallbacks.OnCustomAuthenticationFail或甚至IConnectionCallbacks.OnDisconnected

完整片段

這是一個最小的、自成一體的例子,並不意味著可以完整地復制/粘貼,並按原樣使用。

C#

using Photon.Pun;
using Photon.Realtime;
using Steamworks;
using UnityEngine;

public class MinimalSteamworksPunAuth : MonoBehaviourPunCallbacks
{
    [SerializeField]
    private int steamAppId;
    private AuthTicket hAuthTicket;

    private void Awake()
    {
        try
        {
            SteamClient.Init(steamAppId);
        }
        catch (System.Exception e)
        {
            // Couldn't init for some reason - it's one of these:
            //     Steam is closed?
            //     Can't find steam_api dll?
            //     Don't have permission to play app?
        }
    }

    private void Start()
    {
        Connect();
    }

    public void Connect()
    {
        string steamAuthSessionTicket = GetSteamAuthTicket(out hAuthTicket);
        PhotonNetwork.AuthValues = new AuthenticationValues();
        PhotonNetwork.AuthValues.UserId = SteamClient.SteamId.ToString();
        PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Steam;
        PhotonNetwork.AuthValues.AddAuthParameter("ticket", steamAuthSessionTicket);
        PhotonNetwork.ConnectUsingSettings();
    }

    public override void OnConnectedToMaster()
    {
        CancelAuthTicket(hAuthTicket);
    }

    public override void OnCustomAuthenticationFailed(string errorMessage)
    {
        CancelAuthTicket(hAuthTicket);
    }

    // ticket should be saved so you can use it to cancel the ticket as soon as you are done with it
    private string GetSteamAuthTicket(out AuthTicket ticket)
    {
        ticket = SteamUser.GetAuthSessionTicket();
        StringBuilder ticketString = new StringBuilder();
        for (int i = 0; i < ticket.Data.Length; i++)
        {
            ticketString.AppendFormat("{0:x2}", ticket.Data[i]);
        }
        return ticketString.ToString();
    }

    private void CancelAuthTicket(AuthTicket ticket)
    {
        if (ticket != null)
        {
            ticket.Cancel();
        }
    }

    private void Update()
    {
        SteamClient.RunCallbacks();
    }

    private void OnApplicationQuit()
    {
        SteamClient.Shutdown();
    }
}
Back to top