This document is about: PUN 2
SWITCH TO

PUN 2 is in maintenance / LTS mode. Aside from some fixes no further feature updates are planned. Of course, existing PUN 2 projects will continue to run. New projects should consider Fusion or Quantum.

Photon Oculus Authentication

客戶端代碼

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