Skip to content

Instantly share code, notes, and snippets.

@cdhanna
Last active February 10, 2021 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdhanna/2bafcd6b230fe2279003e3f4b63b8353 to your computer and use it in GitHub Desktop.
Save cdhanna/2bafcd6b230fe2279003e3f4b63b8353 to your computer and use it in GitHub Desktop.
The facebook wrapper for Beamable 0.9.0
using System.Collections.Generic;
using Beamable.AccountManagement;
using Beamable.Api;
using Beamable.Common.Api.Auth;
using Facebook.Unity;
using UnityEditor;
using UnityEngine;
public class FacebookWrapper : MonoBehaviour
{
private AccountManagementSignals _signals;
private bool _hasAttachedListener;
private void Update()
{
if (!_hasAttachedListener && _signals.ThirdPartyLoginAttempted != null)
{
_signals.ThirdPartyLoginAttempted.AddListener(StartFacebookLogin);
_hasAttachedListener = true;
}
}
// Awake function from Unity's MonoBehavior
void Awake ()
{
_signals = gameObject.AddComponent<AccountManagementSignals>();
if (!FB.IsInitialized) {
// Initialize the Facebook SDK
FB.Init(InitCallback, OnHideUnity);
} else {
// Already initialized, signal an app activation App Event
FB.ActivateApp();
}
}
private void InitCallback ()
{
if (FB.IsInitialized) {
// Signal an app activation App Event
FB.ActivateApp();
// Continue with Facebook SDK
// ...
Debug.Log("FB Didnt die");
} else {
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
private void OnHideUnity (bool isGameShown)
{
if (!isGameShown) {
// Pause the game - we will need to hide
Time.timeScale = 0;
} else {
// Resume the game - we're getting focus again
Time.timeScale = 1;
}
}
private void AuthCallback (ThirdPartyLoginPromise promise, ILoginResult result) {
if (!string.IsNullOrEmpty(result.Error))
{
promise.CompleteError(new ErrorCode(0, GameSystem.GAME_CLIENT, "Facebook Error", result.Error));
return;
}
if (FB.IsLoggedIn) {
// AccessToken class will have session details
var aToken = PlayerSettings.Facebook.Unity.AccessToken.CurrentAccessToken;
// Print current access token's User ID
Debug.Log(aToken.UserId);
// Print current access token's granted permissions
foreach (string perm in aToken.Permissions) {
Debug.Log(perm);
}
promise.CompleteSuccess(new ThirdPartyLoginResponse()
{
AuthToken = aToken.TokenString
});
} else {
Debug.Log("User cancelled login");
promise.CompleteSuccess(ThirdPartyLoginResponse.CANCELLED);
}
}
public void StartFacebookLogin(ThirdPartyLoginPromise promise)
{
if (promise.ThirdParty != AuthThirdParty.Facebook) return;
var perms = new List<string>(){"public_profile", "email"};
FB.LogInWithReadPermissions(perms, result => AuthCallback(promise, result));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment