https://developers.facebook.com/docs/unity/ 下载用户SDK导入工程,常用内容代码如下:

using Facebook.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FBUserMgr
{
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="onInitComplete">完成回调</param>
    /// <param name="onHideUnity">失败回调</param>
    /// <param name="authResponse"></param>
    public static void FBInit(Action onInitComplete = null, Action onHideUnity = null, string authResponse = null)
    {
        FB.Init(() =>
        {
            Debug.Log("IsInitialized=" + FB.IsInitialized);
            Debug.Log("IsLoggedIn=" + FB.IsLoggedIn);
            Debug.Log("AppId=" + FB.AppId);
            Debug.Log("GraphApiVersion=" + FB.GraphApiVersion);
            onInitComplete?.Invoke();
        }, (isUnityShutDown) =>
        {
            Debug.LogError("OnHideUnity=" + isUnityShutDown);
            onHideUnity?.Invoke();
        }, authResponse);
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="onFBLoginSucced">成功回调</param>
    /// <param name="onFBLoginFaild">失败回调</param>
    public static void FBLogin(Action<AccessToken> onFBLoginSucced = null, Action<bool, string> onFBLoginFaild = null)
    {
        //权限列表                       默认公开资料字段,  邮箱,     好友名单,       曾住地/家乡,         所在地
        //var perms = new List<string>() { "public_profile", "email", "user_friends", "user_hometown", "user_location" };
        //默认权限
        var perms = new List<string>() { "public_profile", "user_friends" };
        FB.LogInWithReadPermissions(perms, (result) =>
        {
            if (FB.IsLoggedIn)
            {
                //登录成功
                Debug.Log("FaceBook Login Succed");
                Debug.Log("AccessToken.CurrentAccessToken=" + AccessToken.CurrentAccessToken);
                onFBLoginSucced?.Invoke(AccessToken.CurrentAccessToken);
            }
            else
            {
                //登录失败
                Debug.Log("FaceBook Login Faild");
                Debug.Log("result.Cancelled=" + result.Cancelled + " result.Error=" + result.Error);
                onFBLoginFaild?.Invoke(result.Cancelled, result.Error);
            }
        });
    }

    /// <summary>
    /// 一对多分享框
    /// 在没有登录时调用,会自己调起登录授权
    /// </summary>
    /// <param name="contentURL">跳转链接</param>
    /// <param name="contentTitle">标题</param>
    /// <param name="contentDescription">内容</param>
    /// <param name="photoURL">图片链接</param>
    /// <param name="onFBShareSucced">完成回调</param>
    /// <param name="onFBShareFaild">失败回调</param>
    public static void FBShareLink(Uri contentURL = null, string contentTitle = "", string contentDescription = "", Uri photoURL = null, Action<string> onFBShareSucced = null, Action<bool, string> onFBShareFaild = null)
    {
        //contentTitle contentDescription photoURL 可能不会生效,如果不生效可去除
        FB.ShareLink(contentURL, contentTitle, contentDescription, photoURL, (result) =>
        {
            if (!result.Cancelled && String.IsNullOrEmpty(result.Error))
            {
                Debug.Log("FaceBook ShareLink Success");
                Debug.Log("result.PostId=" + result.PostId);
                onFBShareSucced?.Invoke(String.IsNullOrEmpty(result.PostId) ? "" : result.PostId);
            }
            else
            {
                Debug.Log("FaceBook ShareLink Faild");
                Debug.Log("result.Cancelled=" + result.Cancelled + " result.Error=" + result.Error);
                onFBShareFaild?.Invoke(result.Cancelled, result.Error);
            }
        });
    }

    public static void FBShareLink(string uri, string contentTitle = null, string contentDesc = null, string picUri = null, Action<string> onFBShareSucced = null, Action<bool, string> onFBShareFaild = null)
    {
        FBShareLink(new Uri(uri), contentTitle, contentDesc, new Uri(picUri), onFBShareSucced, onFBShareFaild);
    }

    /// <summary>
    /// 获取自己的信息
    /// </summary>
    /// <param name="onFBGetMyInfo">完成回调</param>
    public static void FBGetMyInfo(Action<string> onFBGetMyInfo = null)
    {
        if (FB.IsLoggedIn == false)
        {
            Debug.Log("FaceBook Not Login");
            return;
        }

        //id,name,picture为需要的字段
        FB.API("me?fields=id,name,picture", HttpMethod.GET, (result) => {
            Debug.Log("result.RawResult=" + result.RawResult);
            onFBGetMyInfo?.Invoke(result.RawResult);
        });
    }

    /// <summary>
    /// 获取游戏好友
    /// </summary>
    /// <param name="onFBGetFriendInGame">完成回调</param>
    public static void FBGetFriendInGame(Action<string> onFBGetFriendInGame = null)
    {
        Debug.Log("FaceBook Get Friend");
        if (FB.IsLoggedIn == false)
        {
            Debug.Log("FaceBook Not Login");
            return;
        }

        //id,name,picture为需要的字段
        FB.API("me/friends?fields=id,name,picture", HttpMethod.GET, (result) => {

            Debug.Log("result.RawResult=" + result.RawResult);
            onFBGetFriendInGame?.Invoke(result.RawResult);
        });
    }
}


https://developers.facebook.com/docs/app-events/unity 下载广告SDK导入工程,激励广告代码如下:

using AudienceNetwork;
using Facebook.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FBAdMgr
{
    private RewardedVideoAd _rewardedVideoAd;

    // 激励广告奖励领取回调
    private Action _rewardCall;

    private bool _isLoaded;

    public void FBInit()
    {
        //#后面带广告ID
        _rewardedVideoAd = new RewardedVideoAd("VID_HD_9_16_39S_APP_INSTALL#YOUR_PLACEMENT_ID");
        var go = new GameObject("[AdManager]");
        go.transform.SetParent(GameGod.Instance.transform);
        _rewardedVideoAd.Register(go);

        _rewardedVideoAd.RewardedVideoAdDidLoad = () => {
            Debug.Log("激励广告加载完毕");
            _isLoaded = true;
        };
        _rewardedVideoAd.RewardedVideoAdDidClose = () => {
            Debug.Log("激励广告关闭");
            _rewardedVideoAd?.Dispose();
        };
        _rewardedVideoAd.RewardedVideoAdDidFailWithError = (error) => {
            Debug.Log("激励广告加载失败 错误=" + error);
        };
        _rewardedVideoAd.RewardedVideoAdWillLogImpression = () => {
            Debug.Log("激励广告记录");
        };
        _rewardedVideoAd.RewardedVideoAdDidClick = () => {
            Debug.Log("激励广告被点击");
        };

        //预加载广告
        _rewardedVideoAd.LoadAd();
    }

    public void FBShowRewardedVideo(Action rewardCall)
    {
        if (_isLoaded)
        {
            _rewardCall = rewardCall;
            _rewardedVideoAd.Show();
            _isLoaded = false;
        }
        else
        {
            Debug.Log("广告未加载");
        }
    }
}

最后修改:2023 年 06 月 08 日
如果觉得我的文章对你有用,请随意赞赏