Skip to content

Instantly share code, notes, and snippets.

@MrBeerBrewer
Created March 16, 2017 11:41
Show Gist options
  • Save MrBeerBrewer/f70bdeaa2714e7a23d4557cedc4f35f2 to your computer and use it in GitHub Desktop.
Save MrBeerBrewer/f70bdeaa2714e7a23d4557cedc4f35f2 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Facebook.Unity;
public class FBManager : MonoBehaviour {
public Text txtStatus;
public GameObject btnLi, btnLo;
//public GameObject ProfilePicture;
public Image ProfilePicture;
void Awake ()
{
if (!FB.IsInitialized) {
FB.Init(InitCallback, OnHideUnity);
} else {
FB.ActivateApp();
}
}
private void InitCallback ()
{
if (FB.IsInitialized) {
FB.ActivateApp ();
} else {
txtStatus.text = "Failed to Initialize the Facebook SDK";
}
if (FB.IsLoggedIn) {
FB.API ("/me?fields=name", HttpMethod.GET, DispName);
FB.API("me/picture?type=square&height=128&width=128", HttpMethod.GET, GetPicture);
btnLi.SetActive (false); btnLo.SetActive (true);
} else {
txtStatus.text = "Please login to continue.";
btnLi.SetActive (true); btnLo.SetActive (false);
}
}
private void OnHideUnity (bool isGameShown)
{
if (!isGameShown) {
Time.timeScale = 0; //pause
} else {
Time.timeScale = 1; //resume
}
}
public void LoginWithFB(){
var perms = new List<string>(){"public_profile"};
FB.LogInWithReadPermissions(perms, AuthCallback);
}
public void LogoutFromFB(){
FB.LogOut (); btnLi.SetActive (true); btnLo.SetActive (false);
}
private void AuthCallback (ILoginResult result) {
if (result.Error != null) {
txtStatus.text = result.Error;
}
}
void DispName(IResult result){
if (result.Error != null) {
txtStatus.text = result.Error;
} else {
txtStatus.text = "Hi there: " + result.ResultDictionary ["name"];
}
}
private void GetPicture(IGraphResult result)
{
if (result.Error == null && result.Texture != null)
{
//http://stackoverflow.com/questions/19756453/how-to-get-users-profile-picture-with-facebooks-unity-sdk
/*if (result.Texture != null) {
Image img = ProfilePicture.GetComponent<Image> ();
img.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());
}*/
ProfilePicture.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());
}
}
}
@toseefhaiderkhansugguu
Copy link

Hi i tried your code for fetching my profile pic it worked on PC but when i try to build my project on android my profile pic get scaled or zoomed not fitting into my UI image can u help?

public void fetchProfilePic()
{

    FB.API("me/picture?type=square&height=70&width=70", HttpMethod.GET, GetPicture);
}

private void GetPicture(IGraphResult result)
{
if (result.Error == null && result.Texture != null)
{
Texture2D image = result.Texture;
Image img = navbarProfilePic.GetComponent ();
img.sprite = Sprite.Create (image, new Rect (0, 0, 70, 70), new Vector2 (0.5f,0.5f));

    }
}

navbarProfilePic is actual image where i am trying to put my fb profile picture

@arehmanargon
Copy link

Just replace

ProfilePicture.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());

Line with

ProfilePicture.sprite = Sprite.Create(result.Texture, new Rect(0, 0, result.Texture.width, result.Texture.height), new Vector2());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment