This document is about: PUN 2
SWITCH TO

PUN Classic (v1), PUN 2, Bolt는 휴업 모드입니다. Unity2022에 대해서는 PUN 2에서 서포트하지만, 신기능의 추가는 없습니다. 현재 이용중인 고객님의 PUN 및 Bolt 프로젝트는 중단되지 않고, 퍼포먼스나 성능이 떨어지는 일도 없습니다. 앞으로의 새로운 프로젝트에는 Photon Fusion 또는 Quantum을 사용해 주십시오.

Facebook Authentication

Server Side

Facebook App Setup

First, we need to create a Facebook application if you don't have one already.

  • Go to Facebook Developers website.
  • Click Apps -> Create a New App , enter the name of your app and press Create App button.
  • Choose Apps -> [your_app] and copy App ID and App Secret.

Photon Configuration

Go to the Details page of your application via your Photon Cloud Dashboard.
Expand the Custom Authentication section.
The Authentication URL for Facebook authentication is set by Exit Games.

Set the values for these two parameters:

  • appid = your Facebook App ID
  • secret = your Facebook App Secret

Save changes.

Client Code

The client needs to set the correct authentication type (Facebook, 2) and send a valid Facebook token as a query string parameter named "token".

PUN

Implementation

Create a new MonoBehaviour, attach it to an object on scene and then open.
Use the following code for Facebook initialization and login:

C#

// Include Facebook namespace
using Facebook.Unity;

// [..]

private void Awake()
{
    if (!FB.IsInitialized)
    {
        // Initialize the Facebook SDK
        FB.Init(InitCallback);
    }
    else
    {
        FacebookLogin();
    }
}

private void InitCallback()
{
    if (FB.IsInitialized)
    {
        FacebookLogin();
    }
    else
    {
        Debug.Log("Failed to initialize the Facebook SDK");
    }
}

private void FacebookLogin()
{
    if (FB.IsLoggedIn)
    {
        OnFacebookLoggedIn();
    }
    else
    {
        var perms = new List<string>(){"public_profile", "email", "user_friends"};
        FB.LogInWithReadPermissions(perms, AuthCallback);
    }
}

private void AuthCallback(ILoginResult result)
{
    if (FB.IsLoggedIn)
    {
        OnFacebookLoggedIn();
    }
    else
    {
        Debug.LogErrorFormat("Error in Facebook login {0}", result.Error);
    }
}

To use Facebook Authentication in PUN, add:

C#

private void OnFacebookLoggedIn()
{
    // AccessToken class will have session details
    string aToken = AccessToken.CurrentAccessToken.TokenString;
    string facebookId = AccessToken.CurrentAccessToken.UserId;
    PhotonNetwork.AuthValues = new AuthenticationValues();
    PhotonNetwork.AuthValues.AuthType = CustomAuthenticationType.Facebook;
    PhotonNetwork.AuthValues.UserId = facebookId; // alternatively set by server
    PhotonNetwork.AuthValues.AddAuthParameter("token", aToken);
    PhotonNetwork.ConnectUsingSettings("1.0");
}

The PUN callbacks for success and error that you can implement are:

C#

public class FacebookAuthTest : MonoBehaviourPunCallbacks
{
    public override void OnConnectedToMaster()
    {
        Debug.Log("Successfully connected to Photon!");
    }

    // something went wrong
    public override void OnCustomAuthenticationFailed(string debugMessage)
    {
        Debug.LogErrorFormat("Error authenticating to Photon using facebook: {0}", debugMessage);
    }
}

Back to top