This document is about: PUN 1
SWITCH TO

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

Photon Oculus Authentication

應用程序設置

添加Oculus作為認証提供者很容易,可以在幾秒鐘內從您的Photon應用程序介面完成。 進入一個應用程序的 "管理"頁面,向下滑動到 "認証"部分。 如果您為Oculus添加一個新的認証供應商或編輯一個現有的認証供應商,此為必須的設置:

  • appid:您的Oculus應用程序的ID。
  • appsecret:您的Oculus應用程序機密。

客戶端代碼

Oculus根據用戶的Oculus ID和客戶端提供的nonce對用戶進行驗証。 在代碼中,nonce是一個任意的數字,只能使用一次。 閱讀更多點此

獲取憑證

客戶端需要登錄到Oculus,然后生成一個nonce。 這個nonce是証明客戶端是一個有效的Oculus用戶。 您可以從他們的網站獲得Oculus SDKs。

Unity說明

下載Oculus Platform SDK for Unity並將其導入到您的項目中。 從編輯器的選單欄,進入 "Oculus Platform"->"編輯設置",輸入您的Oculus AppId。 使用下面的代碼來獲取登錄用戶的Oculus ID並生成一個nonce:

C#

using UnityEngine;
using Oculus.Platform;
using Oculus.Platform.Models;

public class OculusAuth : MonoBehaviour
{
    private string oculusId;

    private void Start()
    {
        Core.AsyncInitialize().OnComplete(OnInitializationCallback);
    }
    
    private void OnInitializationCallback(Message<PlatformInitialize> msg)
    {
        if (msg.IsError)
        {
            Debug.LogErrorFormat("Oculus: Error during initialization. Error Message: {0}",
                msg.GetError().Message);
        }
        else
        {
            Entitlements.IsUserEntitledToApplication().OnComplete(OnIsEntitledCallback);
        }
    }

    private void OnIsEntitledCallback(Message msg)
    {
        if (msg.IsError)
        {
            Debug.LogErrorFormat("Oculus: Error verifying the user is entitled to the application. Error Message: {0}",
                msg.GetError().Message);
        }
        else
        {
            GetLoggedInUser();
        }
    }

    private void GetLoggedInUser()
    {
        Users.GetLoggedInUser().OnComplete(OnLoggedInUserCallback);
    }

    private void OnLoggedInUserCallback(Message<User> msg)
    {
        if (msg.IsError)
        {
            Debug.LogErrorFormat("Oculus: Error getting logged in user. Error Message: {0}",
                msg.GetError().Message);
        }
        else
        {
            oculusId = msg.Data.ID.ToString(); // do not use msg.Data.OculusID;
            GetUserProof();
        }
    }

    private void GetUserProof()
    {
        Users.GetUserProof().OnComplete(OnUserProofCallback);
    }

    private void OnUserProofCallback(Message<UserProof> msg)
    {
        if (msg.IsError)
        {
            Debug.LogErrorFormat("Oculus: Error getting user proof. Error Message: {0}",
                msg.GetError().Message);
        }
        else
        {
            string oculusNonce = msg.Data.Value;
            // Photon Authentication can be done here
        }
    }
}

憑證

客戶端需要發送Oculus ID和生成的nonce作為查詢字符串參數,並分別使用關鍵字 "userid "和 "nonce ":

C#

PhotonNetwork.AuthValues = new AuthenticationValues();
PhotonNetwork.AuthValues.UserId = oculusId;
PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Oculus;
PhotonNetwork.AuthValues.AddAuthParameter("userid", oculusId);
PhotonNetwork.AuthValues.AddAuthParameter("nonce", oculusNonce);
// do not set AuthValues.Token or authentication will fail
// connect
Back to top