Skip to content

Instantly share code, notes, and snippets.

@pescode
Last active January 8, 2018 13:51
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 pescode/670e2263cbad828f54cef52093796cc0 to your computer and use it in GitHub Desktop.
Save pescode/670e2263cbad828f54cef52093796cc0 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Facebook.Unity;
using Facebook.MiniJSON;
using System;
public class FBManager : MonoBehaviour {
public static List<object> scoresList = new List<object>();
void Awake()
{
FB.Init(OnInitCompleted,OnHideUnity);
}
void OnInitCompleted()
{
if(FB.IsLoggedIn){
// El usuario ya esta conectado
}else{
// El usuario no esta logueado
}
}
void OnHideUnity(bool isGameShown)
{
if(!isGameShown)
{
}else{
}
}
public static void FBlogin()
{
List<string> permissions = new List<string>();
permissions.Add("public_profile");
permissions.Add("user_friends");
FB.LogInWithReadPermissions (permissions, AuthCallBack);
}
static void AuthCallBack(IResult result)
{
if(result.Error != null){
Debug.Log(result.Error);
}else{
if(FB.IsLoggedIn){
// El usuario se logueo correctamente
// En caso de que se utilice PUBLISH_ACTIONS hay que pedir autorizacion al user
List<string> permissions = new List<string>();
permissions.Add("public_profile");
permissions.Add("user_friends");
permissions.Add("publish_actions");
FB.LogInWithPublishPermissions(permissions,AuthCallBackPublish);
}else{
// El usuario no se logueo por alguna razon
}
}
}
void AuthCallBackPublish(IResult result)
{
if(result.Error != null){
Debug.Log(result.Error);
}else{
if(FB.IsLoggedIn){
}else{
}
}
}
public static String FBUserName = "";
public static String GetUserName()
{
if(FB.IsLoggedIn){
FB.API("/me?fields=first_name", HttpMethod.GET, ResultGetUserName);
FB.API("/me/picture?type=square&height=128&width=128", HttpMethod.GET, GetProfilePic);
}
}
static void ResultGetUserName(IResult result)
{
if(result.Error == null){
FBUserName = result.ResultDictionary["first_name"].ToString();
}else{
Debug.Log(result.Error);
FBUserName = "";
}
}
public static Sprite FBProfilePic128;
public static Sprite GetUserPic()
{
if(FB.IsLoggedIn){
FB.API("/me/picture?type=square&height=128&width=128", HttpMethod.GET, ResultGetUserPic);
}
}
static void ResultGetUserPic(IGraphResult result)
{
if(result.Texture != null){
FBProfilePic128 = Sprite.Create(result.Texture, new Rect(0,0,128,128), new Vector2());
}
}
public static void AppRequest()
{
FB.AppRequest(
message:"Can you beat me in this game? Lets play now!",
title:"Invite to play YOURGAME"
);
}
public static void InviteFriends()
{
FB.Mobile.AppInvite(
new Uri("https://fb.me/666666666666"),
null,
AppInviteCallback
);
}
void AppInviteCallback(IAppInviteResult result)
{
}
public static void ShowTop10()
{
FB.API("/app/scores?fields=score,user.limit(10)",HttpMethod.GET,ScoresCallBack);
}
void ScoresCallBack(IGraphResult result)
{
scoresList = result.ResultDictionary["data"] as List<object>;
foreach(object score in scoresList)
{
var entry = (Dictionary<string,object>) score;
var user = (Dictionary<string,object>) entry["user"];
// Obtener datos
Debug.Log(user["name"].ToString());
Debug.Log(entry["score"].ToString());
// Obtener imagen de perfil
Image UserAvatar = FriendPic.GetComponent<Image>();
FB.API("/"+user["id"].ToString()+"/picture?type=square&height=128&width=128", HttpMethod.GET, delegate(IGraphResult resultpic) {
if(resultpic.Texture == null) // error
{
}else{
// Ejemplo de uso
// UserAvatar.sprite = Sprite.Create(resultpic.Texture, new Rect(0,0,128,128), new Vector2());
}
});
}
}
public static void SendScore(string score)
{
if(FB.IsLoggedIn){
var scoreData = new Dictionary<string,string>();
scoreData["score"] = score;
FB.API("/me/scores",HttpMethod.POST, delegate(IGraphResult result) {
Debug.Log("submit result:"+result.ToString());
}, scoreData);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment