首页 > 其他分享 >Unity FaceBook SDK - 1

Unity FaceBook SDK - 1

时间:2024-01-16 16:13:07浏览次数:28  
标签:Log FB Unity FaceBook result Debug null string SDK

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");
                }
            });
        }
    }

 

           

标签:Log,FB,Unity,FaceBook,result,Debug,null,string,SDK
From: https://www.cnblogs.com/XieBoss-blogs1/p/17962293

相关文章

  • Unity报错记录->ArgumentNullException: Value cannot be null. Parameter name: _uni
    问题描述项目报错ArgumentNullException:Valuecannotbenull.Parametername:_unity_self不会影响项目正常运行,但是在DeBug模式下会一直卡住,非常恶心。解决方法删除项目中的Library文件夹,重新加载项目......
  • 在Linux下, 不使用包管理器安装Golang sdk
    尝试从targz安装go下面这个是go官网的,注意使用代理下载;wgethttps://go.dev/dl/go1.21.6.linux-amd64.tar.gztar-zxvfgo1.13.1.linux-amd64.tar.gzmvgo//usr/local/将go添加到环境变量:确实fishshell是这样设置环境变量的吗?vim/etc/profile加入以下内容:e......
  • SDK does not contain 'libarclite' at the path '/Applications/Xcode.app/Contents
    热烈欢迎,请直接点击!!!进入博主AppStore主页,下载使用各个作品!!!注:博主将坚持每月上线一个新app!!1、打开路径文件夹:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/2、新建文件夹:arc3、下载文件:https://github.com/kamyarelya......
  • Unity可拖动UI
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.EventSystems;publicclassDragPanel:MonoBehaviour,IDragHandler,IPointerDownHandler{privateRectTransformpanel;privateCanvascanvas;voidS......
  • Unity UnityWebRequest增加重试
    逻辑比较简单,直接上代码:IEnumeratorRequest(stringurl,stringmethod,Dictionary<string,string>headers,byte[]bodyRaw,intretryCount,System.Action<DownloadHandler>callback){for(inti=0;i<retryCount;i++)......
  • 微软企业库Unity学习笔记(一)
    微软企业库Unity学习笔记(一) 本文主要介绍:关于Unitycontainer配置,注册映射关系、类型,单实例、已存在对象和指出一些container的基本配置,这只是我关于Unity的学习心得和笔记,希望能够大家多交流相互学习到更多知识,谢谢大家的支持。我们可以通过以下两种方法给Unitycontain......
  • 微软企业库Unity学习笔记(二)
    微软企业库Unity学习笔记(二) 接下来介绍一下依赖注入的方式:构造函数注入属性注入方法注入一、构造函数注入我们将介绍单构造函数和多构造函数注入1)单构造函数使用自动注入单构造函数自动注入,这里我们使用一个简单的例子具体类MyObject依赖于具体类MyDependentC......
  • 如何让Visual Studio Tools for Unity插件用于调试你自己的Mono嵌入应用程序
       最近在测试将mono嵌入到C++应用程序中,苦于没有调试器,有时候还是不怎么方便。网上搜了一下,有VS插件MDebug、VSMonoDebugger,实际试用了一下,有点麻烦,而且似乎对Windows+VisualStudio2022支持不大好。因此想到了,Unity引擎是基于mono的,VisualStudio2022也内置了针对Unity的......
  • RK3568 学习笔记 : 解决 linux_sdk 编译 python 版本报错问题
    前言最近买了【正点原子】的RK3568开发板,下载了开发板的资料,包括LinuxSDK,这个LinuxSDK占用的空间比较大,扩展了一下VM虚拟机ubuntu20.04的硬盘空间,编译才正常通过。编译RK3568LinuxSDK时,遇到python版本的问题,这里做个记录【正点原子】rk3568开发板资料与Lin......
  • Unity 获取当前系统时间并在UI界面中显示
    在Unity3D中获取当前系统时间,并在UITEXT中显示 代码:usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingSystem;usingTMPro;usingUnityEngine.UI;usingUnityEngine.SceneManagement;publicclassTIME_show:MonoBehaviou......