Last active
December 3, 2016 19:41
-
-
Save bluenex/149653668674fa80986a0710c89bfe69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using Facebook.Unity; | |
using UnityEngine.UI; | |
public class fb_api : MonoBehaviour { | |
public Button showProfilePic; | |
public Image profilePic; | |
void Awake () | |
{ | |
if (!FB.IsInitialized) { | |
// Initialize the Facebook SDK | |
FB.Init(InitCallback, OnHideUnity); | |
} else { | |
// Already initialized, signal an app activation App Event | |
FB.ActivateApp(); | |
} | |
} | |
void Start() | |
{ | |
StartCoroutine(loginAfter()); | |
showProfilePic.onClick.AddListener(getProfilePic); | |
} | |
// methods to login and retrieve profile pic | |
IEnumerator loginAfter() | |
{ | |
yield return new WaitForSeconds(0.5f); | |
LoginFB(); | |
} | |
private void LoginFB() | |
{ | |
var perms = new List<string>(){"public_profile", "email", "user_friends"}; | |
FB.LogInWithReadPermissions(perms, AuthCallback); | |
} | |
void getProfilePic() | |
{ | |
FB.API("/me/picture", HttpMethod.GET, ProfilePhotoCallback); | |
} | |
// Facebook class methods | |
private void InitCallback () | |
{ | |
if (FB.IsInitialized) { | |
// Signal an app activation App Event | |
FB.ActivateApp(); | |
// Continue with Facebook SDK | |
// ... | |
} 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 (ILoginResult result) { | |
if (FB.IsLoggedIn) { | |
// AccessToken class will have session details | |
var aToken = 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); | |
} | |
} else { | |
Debug.Log("User cancelled login"); | |
} | |
} | |
private void ProfilePhotoCallback(IGraphResult result) | |
{ | |
if (string.IsNullOrEmpty(result.Error) && result.Texture != null) | |
{ | |
Sprite p = Sprite.Create(result.Texture, new Rect(0,0,result.Texture.width, result.Texture.height), new Vector2(0.5f, 0.5f)); | |
profilePic.sprite = p; | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using Facebook.Unity; | |
using UnityEngine.UI; | |
using Facebook.MiniJSON; | |
public class profile : MonoBehaviour | |
{ | |
public GameObject fbname; | |
public GameObject fbava; | |
private Dictionary<string, string> aaa = null; | |
private void DealWithProfilePicture(IGraphResult result) | |
{ | |
if (result.Error != null) | |
{ | |
Debug.Log ("problem with getting profile"); | |
FB.API(Util.GetPictureURL("me", 128, 128), HttpMethod.GET, DealWithProfilePicture); | |
return; | |
} | |
Image UserAvatar = fbava.GetComponent<Image> (); | |
UserAvatar.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 (0, 0)); | |
} | |
private void DealWithUsername(IGraphResult result) | |
{ | |
if (result.Error != null) | |
{ | |
Debug.Log ("problem with getting username"); | |
FB.API ("/me?filed=id,first_name", HttpMethod.GET, DealWithUsername); | |
return; | |
} | |
aaa = Util.DeserializeJSONProfile(result.RawResult); | |
Text Username = fbname.GetComponent<Text> (); | |
Username.text = "Hello, " + aaa["first_name"]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment