- Home /
first steps with facebook sdk - print name in GUI
hello everyone, I'm trying to understand the use of the SDK in facebook unity. What I want to do is retrieve the name of a user and print them using a gui label. The process works well but I do not know how I can print it in the GUI
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
public class init : MonoBehaviour
{
bool isInit = false;
string hasPerm = "***";
private void CallFBInit()
{
FB.Init(OnInitComplete, OnHideUnity);
}
void OnGUI()
{
if (GUI.Button (new Rect (10, 10, 300, 100), "InitInit: " + isInit))
CallFBInit ();
if (GUI.Button (new Rect (10, 110, 300, 100), "LogIn - Logged: " + FB.IsLoggedIn)) {
CallFBLogin ();
print ("NOME"+FB.UserId +"/first_name");
}
if (GUI.Button (new Rect (10, 210, 300, 100), "Permission - friends: " + hasPerm)) {
CallPermissionCheck ();
}
}
private void OnInitComplete()
{
Debug.Log("FB.Init completed: Is user logged in? " + FB.IsLoggedIn);
isInit = true;
}
private void OnHideUnity(bool isGameShown)
{
Debug.Log("Is game showing? " + isGameShown);
}
private void CallFBLogin()
{
FB.Login("publish_actions, user_friends", LoginCallback);
}
void LoginCallback(FBResult result)
{
string lastResponse;
if (result.Error != null)
lastResponse = "Error Response:\n" + result.Error;
else if (!FB.IsLoggedIn)
{
lastResponse = "Login cancelled by Player";
}
else
{
lastResponse = "Login was successful!";
}
Debug.Log("lastResponse: "+lastResponse);
}
void CallPermissionCheck()
{
FB.API("/me/permissions", Facebook.HttpMethod.GET, PermissionCallback);
}
void PermissionCallback(FBResult result)
{
if (!String.IsNullOrEmpty(result.Error))
{
Debug.Log("Error Response:\n" + result.Error);
}
else
{
Debug.Log("Get user's permissions was successful!");
Debug.Log ("Result: "+result.Text);
var permissionDict = Facebook.MiniJSON.Json.Deserialize(result.Text) as Dictionary<string, object>;
IDictionary persmissionData = permissionDict;
foreach(IDictionary perm in (List<object>)persmissionData["data"])
{
if((string)perm["permission"] == "user_friends")
{
hasPerm = (string)perm["status"];
// This will deauthorise the permissions for the user and then log them out
/*
if(hasPerm != "granted")
{
FB.API ("me/permissions", Facebook.HttpMethod.DELETE, RemovePermissionCallback);
FB.Logout();
}
*/
FB.API("me?fields=name", Facebook.HttpMethod.GET, NameCallBack);
}
}
}
}
void RemovePermissionCallback(FBResult result)
{
if (!String.IsNullOrEmpty(result.Error))
{
Debug.Log("Error Response:\n" + result.Error);
}
else
{
Debug.Log("RemovePermissionCallback was successful!");
Debug.Log ("Result: "+result.Text);
}
}
void NameCallBack(FBResult result)
{
IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.Text) as IDictionary;
string fbname = dict["name"].ToString();
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613170418im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gui label variable as a string.. 1 Answer
GUI LABEL behind viewport rect of mini map? 1 Answer
GuiMatrix ? 1 Answer
Labels aren't colored properly 1 Answer