Unity FaceBook SDK - 1

SDK下载

前往 https://developers.facebook.com/docs/unity/downloads 下载SDK

ps目前我只用过 16.0.0 跑通

 

在 facebook 上搞出应用后,将应用的app相对于数据复制过来 如图下:

 安卓的话 将上面的三个内容复制到 facebook ,然后点击一下按钮,生成出 manifest

 

然后可以参考使用一下代码

复制代码
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using Facebook.Unity;
using Facebook.MiniJSON;
using LitJson;

public class FBMgr
{
    public static string FBLog = "MyFbLog:";
    public delegate void OnFBLoginSucced(Facebook.Unity.AccessToken token);
    public delegate void OnFBLoginFaild(bool isCancel, string errorInfo);
    public delegate void OnFBShareLinkSucced(string postId);
    public delegate void OnFBShareLinkFaild(bool isCancel, string errorInfo);
    public delegate void OnGotFBFriendInGame(string resultJsonStr);
    public delegate void OnGotFBMyInfo(string resultJsonStr);
    public delegate void OnFBInvitedSucceed(string resultJsonStr);
    private static string appLinkUrl;
    public String FBID;
    /// <summary>
    /// 初始化
    /// </summary>
    public static void Init()
    {
        FB.Init(() =>
        {
            string logMessage = string.Format("OnInitCompleteCalled IsLoggedIn='{0}' IsInitialized='{1}'", FB.IsLoggedIn, FB.IsInitialized);
            Debug.Log(FBLog+ logMessage);
            Debug.Log(FBLog+"FB.AppId: " + FB.AppId);
            Debug.Log(FBLog+"FB.GraphApiVersion: " + FB.GraphApiVersion);         

        },(isUnityShutDown) =>
        {
            Debug.Log(FBLog+" FB OnHideUnity: " + isUnityShutDown);
        });
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="onFBLoginSucced">回调,不需要可不填</param>
    /// <param name="onFBLoginFaild">回调,不需要可不填</param>
    public static void FBLogin(OnFBLoginSucced onFBLoginSucced = null, OnFBLoginFaild onFBLoginFaild = null)
    {
        var perms = new List<string>() { "public_profile", "email", "user_friends" };
        FB.LogInWithReadPermissions(perms, (result) =>
        {
            if (result == null)
            {              
                Debug.Log(FBLog+ "Null Response\n");
                return;
            }
            if (FB.IsLoggedIn)
            {
                Debug.Log(FBLog+"FBLoginSucceed");
                if (onFBLoginSucced != null)
                {
                    onFBLoginSucced(Facebook.Unity.AccessToken.CurrentAccessToken);
                }
            }
            else
            {
                Debug.Log(FBLog+"FBLoginFaild+"+result.Error);
                Debug.Log(FBLog + result.RawResult);
                if (onFBLoginFaild != null)
                {
                    onFBLoginFaild(result.Cancelled, result.Error);
                }
            }
        });
    }


    /// <summary>
    /// 分享。
    /// </summary>
    /// <param name="uri">"https://developers.facebook.com/"</param>
    /// <param name="contentTitle">"ShareLink"</param>
    /// <param name="contentDesc">"Look I'm sharing a link"</param>
    /// <param name="picUri">"http://i.imgur.com/j4M7vCO.jpg"</param>
    /// <param name="onFBShareLinkSucced">回调,不需要可不填</param>
    /// <param name="onFBShareLinkFaild">回调,不需要可不填</param>

    public static void FBShareLink(string uri, string contentTitle=null, string contentDesc=null, string picUri=null, OnFBShareLinkSucced onFBShareLinkSucced = null, OnFBShareLinkFaild onFBShareLinkFaild = null)
    {

        FBShareLink(new Uri(uri), contentTitle, contentDesc, new Uri(picUri), onFBShareLinkSucced, onFBShareLinkFaild);
    }

    private static void FBShareLink(Uri uri, string contentTitle, string contentDesc, Uri picUri, OnFBShareLinkSucced onFBShareLinkSucced = null, OnFBShareLinkFaild onFBShareLinkFaild = null)
    {
       
            FB.ShareLink(uri, contentTitle, contentDesc, picUri, (result) =>
            {
                if (result.Cancelled || !String.IsNullOrEmpty(result.Error))
                {
                    Debug.Log(FBLog + "ShareLink Faild");
                    if (onFBShareLinkFaild != null)
                    {
                        onFBShareLinkFaild(result.Cancelled, result.Error);
                    }
                }
                else
                {
                    Debug.Log(FBLog + "ShareLink success!");
                    if (onFBShareLinkSucced != null)
                    {
                        onFBShareLinkSucced(String.IsNullOrEmpty(result.PostId) ? "" : result.PostId);
                    }
                }
            });
     
    }



    /// <summary>
    /// 获取自己的信息
    /// </summary>
    /// <param name="onGotFBMyInfo">回调,不需要可不填</param>
    //返回示例
    //{
    //  "id": "581672012697623",
    //  "name": "Guangmao Zhao",
    //  "picture": {
    //    "data": {
    //      "height": 50,
    //      "is_silhouette": false,
    //      "url": "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=581672012697623&height=50&width=50&ext=1585856586&hash=AeR_BguJ28EVvd-r",
    //      "width": 50
    //    }
    //  }
    //}
    public static void GetMyInfo(OnGotFBMyInfo onGotFBMyInfo = null)
    {
        if (FB.IsLoggedIn == false)
        {
            Debug.Log(FBLog + "FBNotLogin");
            return;
        }
        FB.API("me?fields=id,name,picture", HttpMethod.GET, (result) => {
            Debug.Log(FBLog + result.RawResult);
            if (onGotFBMyInfo != null)
            {
                onGotFBMyInfo(result.RawResult);
            }
        });
    }

    /// <summary>
    /// 获取游戏好友
    /// </summary>
    /// <param name="onGotFBFriendInGame">回调,不需要可不填</param>

    //返回示例
    //{
    //  "data": [
    //  ],
    //  "summary": {
    //    "total_count": 3
    //  }
    //}
    public static void GetFBFriendInGame(OnGotFBFriendInGame onGotFBFriendInGame = null)
    {
        Debug.Log("GetFBFriendInGame");
        if (FB.IsLoggedIn == false)
        {
            Debug.Log(FBLog + "FBNotLogin");
            return;
        }

        FB.API("me/friends?fields=id,name,picture", HttpMethod.GET, (result) => {
           
            if (onGotFBFriendInGame != null)
            {
                Debug.Log(FBLog + result.RawResult);
                onGotFBFriendInGame(result.RawResult);
            }
        });
    }

    /// <summary>
    /// 邀请
    /// </summary>
    /// <param name="message">Come play this great game!</param>
    /// <param name="onFBInvitedSucceed">获取游戏好友</param>
    public static void FBInvite(string message, OnFBInvitedSucceed onFBInvitedSucceed = null)
    {

        FB.AppRequest(message, null, null, null, null, null, null, (result) => {
            Debug.Log(FBLog + result.RawResult);

            if (onFBInvitedSucceed!=null)
            {
                onFBInvitedSucceed(result.RawResult);
                InviteInfoToArry(result.RawResult);
            }
        });
    }


    /// <summary>
    /// 把邀请返回的数据转换成数组
    /// </summary>
    /// <param name="info"></param>
    /// <returns></returns>
    public static string[] InviteInfoToArry(string info)
    {
        JsonData jsonData = JsonMapper.ToObject(info);
        JsonData damageValue = jsonData["to"];
        string[] arrStr = damageValue.ToString().Split(',');//按逗号截取 
        Debug.Log(arrStr[0]);
        return arrStr;
    }

}
复制代码

 

在插入一个登录

复制代码
    private void DoLogin()
    {
        if(FB.IsLoggedIn)
        {
            Debug.Log("You have logined!"); 
            return;
        }
        if (null != AccessToken.CurrentAccessToken && AccessToken.CurrentAccessToken.ExpirationTime > System.DateTime.Now)
        {
            // 快速登录
            FB.Android.RetrieveLoginStatus((result) =>
            {
                if (!string.IsNullOrEmpty(result.Error))
                {
                    Debug.Log("Error: " + result.Error);
                }
                else if (result.Failed)
                {
                    Debug.Log("Failure: Access Token could not be retrieved");
                }
                else
                {
                    Debug.Log("Success: " + result.AccessToken.UserId);
                }
            });
        }
        else
        {
            // 登录
            var perms = new List<string>() { "public_profile", "email" };
            FB.LogInWithReadPermissions(perms, (result) =>
            {
                if (FB.IsLoggedIn)
                {
                    var aToken = AccessToken.CurrentAccessToken;
                    Debug.Log(aToken.UserId);
                    foreach (string perm in aToken.Permissions)
                    {
                        Debug.Log(perm);
                    }
                }
                else
                {
                    Debug.Log("User cancelled login");
                }
            });
        }
    }
复制代码

 

 
posted @   减肥的程序  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示