PUN Classic (v1)、PUN 2 和 Bolt 處於維護模式。 PUN 2 將支援 Unity 2019 至 2022,但不會添加新功能。 當然,您所有的 PUN & Bolt 專案可以用已知性能繼續運行使用。
對於任何即將開始或新的專案:請切換到 Photon Fusion 或 Quantum。
Facebook Authentication
Overview
In this tutorial we will create a Photon Unity Networking (PUN) project with Facebook custom authentication.
Facebook Application Setup
First, we need to create a Facebook application.
- 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 Dashboard Setup
- Open Photon Dashboard.
- Select the App Id that will be used and click See Details.
- Navigate to Authentication tab and uncheck Allow anonymous clients to connect, independently of configured providers.
- Select Facebook authentication provider and paste App ID and Secret.
- Check Reject all clients if not available.
PUN Setup
- Open Unity.
- Import.
- Setup PUN.
- Import Facebook SDK for Unity.
- In Unity's main menu go to Facebook -> Edit Settings, enter the App Name and App Id for your Facebook Application.
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);
}
}
Conclusion
In this tutorial we covered the basic steps required for integration of Facebook API with PUN.
Still have some questions? You are extremely welcome to ask it on forum.
Happy coding!