Unity接入ShareSDK实现QQ登录和QQ分享、微信分享(Android端)

由于微信登录需要企业审核,我这里就不说明了,有需要的可以去官网看一下文档,和QQ登录比多了一个打包的步骤。

补充(做测试的时候最好先确定包名)

第一步:到官网申请开发者账号:MOB 

第二步:到腾讯QQ开放平台申请开发者账号并创建应用:腾讯开放平台

点击创建应用。然后会有一个APP ID,和APPKey: 这个要保留下来

第三步:在mob官网创建自己的应用:http://dashboard.mob.com/#!/index

保留下应用的APPID和KEY

第四步:在Mob官网下载Unity专用SDK包进行导入到项目中去:点击下载

第五步:  先打开Android 目录下找到AndroidManifest     Mob中你创建的应用的Key等信息填写进去

<meta-data android:name="Mob-AppKey" android:value="您的AppKey"/>
<meta-data android:name="Mob-AppSecret" android:value="您的AppSecret"/>

  在Unity工程中新建空物体挂载上ShareSDK.cs脚本 Mob中你创建的应用的Key等信息填写进去

第六步:在ShareSDK脚本中找到DevInfo定义的地方找到QQ类:修改其中的ID和KEY。 

或者修改

就修改成腾讯开放平台中你创建的应用的ID和KEY

第七步:创建一个新的脚本挂载到相同的物体上,然后这样...

using System;
using System.Collections;
using System.Collections.Generic;
using cn.sharesdk.unity3d;
using LitJson;
using UnityEngine;
using UnityEngine.UI;

public class ShareSDKManager : MonoBehaviour
{
    public Text myConsole;
    private QQUser user;

    private static ShareSDKManager _instance;
    public static ShareSDKManager Instance
    {
        get
        {
            return _instance;
        }
    }
    private ShareSDK shareSdk;
    
    void Awake()
    {
        _instance = this;
        DontDestroyOnLoad(gameObject);
        shareSdk = gameObject.GetComponent<ShareSDK>();

        shareSdk.authHandler = AuthHandler;

        EventCenter.AddListener(EventTypes.RequestAuth, OnRequestAuth);
        EventCenter.AddListener(EventTypes.CancelAuth, OnCancelAuth);
    }
    /// <summary>
    /// 请求授权
    /// </summary>
    /// <param name="data"></param>
    private void OnRequestAuth(object data)
    {
        shareSdk.Authorize(PlatformType.QQ);
    }
    /// <summary>
    /// 请求取消授权
    /// </summary>
    private void OnCancelAuth(object data)
    {
        shareSdk.CancelAuthorize(PlatformType.QQ);
    }

    /// <summary>
    /// 授权回调
    /// </summary>
    /// <param name="reqID"></param>
    /// <param name="state"></param>
    /// <param name="type"></param>
    /// <param name="data"></param>
    private void AuthHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
    {
        switch (state)
        {
            case ResponseState.Begin:// 开始授权
                break;
            case ResponseState.Success:// 授权成功
                AuthSucess();
                break;
            case ResponseState.Fail:// 授权失败
                break;
            case ResponseState.Cancel:// 取消授权
                break;
            case ResponseState.BeginUPLoad:// 开始加载
                break;
            default:
                break;
        }
    }
    /// <summary>
    /// 授权成功
    /// </summary>
    private void AuthSucess()
    {
        // 获取到授权信息
        Hashtable info = shareSdk.GetAuthInfo(PlatformType.QQ);
        // 解析json
        JsonData data = JsonMapper.ToObject(MiniJSON.jsonEncode(info));
        string userID = data["userID"].ToString();
        string username = data["userName"].ToString();
        string userIconUrl = data["userIcon"].ToString();
        string token = data["token"].ToString();
        user = new QQUser(userID, username, userIconUrl, token);
        EventCenter.Broadcast(EventTypes.AuthSuccess, user);
    }
    public void fenxiang__QQ() //QQ分享
    {
        ShareContent content = new ShareContent();
        content.SetTitle("测试");
        content.SetText("测试文本");
        content.SetTitleUrl(
            "https://www.gamersky.com/showimage/id_gamersky.shtml?http://img1.gamersky.com/image2019/07/20190725_ls_red_141_2/gamersky_042origin_083_2019725182972C.jpg");
        content.SetImageUrl(
            "https://www.gamersky.com/showimage/id_gamersky.shtml?http://img1.gamersky.com/image2019/07/20190725_ls_red_141_2/gamersky_042origin_083_2019725182972C.jpg");
        content.SetShareType(ContentType.Image);
        shareSdk.ShareContent(PlatformType.QQ, content);
    }
    public void fenxiang__ALL() //所有界面
    {
        ShareContent content = new ShareContent();
        content.SetTitle("测试");
        content.SetText("测试文本");
        content.SetTitleUrl(
            "https://www.gamersky.com/showimage/id_gamersky.shtml?http://img1.gamersky.com/image2019/07/20190725_ls_red_141_2/gamersky_042origin_083_2019725182972C.jpg");
        content.SetImageUrl(
            "https://www.gamersky.com/showimage/id_gamersky.shtml?http://img1.gamersky.com/image2019/07/20190725_ls_red_141_2/gamersky_042origin_083_2019725182972C.jpg");
        content.SetShareType(ContentType.Image);
        shareSdk.ShowPlatformList(null, content, 100, 100);
    }
    public void fenxiang_WX()
    {
        ShareContent content = new ShareContent();
        content.SetTitle("标题");
        content.SetText("内容");
        content.SetImageUrl("https://www.gamersky.com/showimage/id_gamersky.shtml?http://img1.gamersky.com/image2019/07/20190725_ls_red_141_2/gamersky_042origin_083_2019725182972C.jpg");
        content.SetUrl("");
        content.SetShareType(ContentType.Image);
        shareSdk.ShareContent(PlatformType.WeChat, content);
    }

    private void OnDestroy()
    {
        // 移除事件监听
        EventCenter.RemoveListener(EventTypes.RequestAuth, OnRequestAuth);
    }
}
/// <summary>
/// 定义一个QQUser类,用来存储登录返回的用户信息
/// </summary>
public class QQUser
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public string UserID { get; private set; }
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; private set; }
    /// <summary>
    /// 用户头像的url
    /// </summary>
    public string UserIconUrl { get; private set; }
    /// <summary>
    /// 用户记号
    /// </summary>
    public string Token { get; private set; }

    public QQUser(string userID, string username, string userIconUrl, string token)
    {
        this.UserID = userID;
        this.Username = username;
        this.UserIconUrl = userIconUrl;
        this.Token = token;
    }
}
/// <summary>
/// 事件中心
/// </summary>
public class EventCenter
{
    private static Dictionary<EventTypes, Action<object>> m_EventDict = new Dictionary<EventTypes, Action<object>>();

    /// <summary>
    /// 添加事件监听
    /// </summary>
    public static void AddListener(EventTypes eventType, Action<object> callback)
    {
        if (!m_EventDict.ContainsKey(eventType))
        {
            m_EventDict.Add(eventType, null);
        }
        m_EventDict[eventType] += callback;
    }

    /// <summary>
    /// 移除事件监听
    /// </summary>
    public static void RemoveListener(EventTypes eventType, Action<object> callback)
    {
        if (!m_EventDict.ContainsKey(eventType))
            return;

        if (m_EventDict[eventType] == null)
            return;

        m_EventDict[eventType] -= callback;
    }

    /// <summary>
    /// 广播
    /// </summary>
    public static void Broadcast(EventTypes eventType, object data)
    {
        if (!m_EventDict.ContainsKey(eventType))
            return;

        Action<object> callback = m_EventDict[eventType];

        if (callback != null)
        {
            callback(data);
        }
    }

}

/// <summary>
/// 事件类型枚举
/// </summary>
public enum EventTypes
{
    RequestAuth,    // 请求授权
    AuthSuccess,    // 授权成功
    AuthFail,       // 授权失败
    CancelAuth,     // 取消授权
}

第八步:设置分享(可以显示全平台分享和指定平台分享)

分享的时候需要传递很多的属性进去:

详解可以参考Demo.cs脚本(SDK里自带的)里面有常用的属性写法

还有一些不常用的属性请看:ShareContent.cs脚本

下面给一个示例代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
/// <summary>
/// UI面板
/// </summary>
public class UIPanel : MonoBehaviour
{
    public Text msgText;
    public Image userIconImg;
    public Text usernameText;
    public Button loginBtn;
    public Button logoutBtn;
 
    private void Awake()
    {
        // 获取UI组件
        userIconImg = transform.Find("Img_UserIcon").GetComponent<Image>();
        msgText = transform.Find("Txt_Msg").GetComponent<Text>();
        usernameText = transform.Find("Txt_Username").GetComponent<Text>();
        loginBtn = transform.Find("Btn_Login").GetComponent<Button>();
        logoutBtn = transform.Find("Btn_Logout").GetComponent<Button>();
 
        // 添加按钮点击事件
        loginBtn.onClick.AddListener(OnLoginBtnClicked);
        logoutBtn.onClick.AddListener(OnLogoutBtnClicked);
 
        logoutBtn.gameObject.SetActive(false);
 
        EventCenter.AddListener(EventTypes.AuthSuccess, OnAuthSuccess);
    }
 
 
    /// <summary>
    /// 登录成功
    /// </summary>
    /// <param name="data">QQUser对象</param>
    private void OnAuthSuccess(object data)
    {
        QQUser user = data as QQUser;
        usernameText.text = "用户名:" + user.Username;
        StartCoroutine(LoadUserIcon(user.UserIconUrl));
        loginBtn.gameObject.SetActive(false);
        logoutBtn.gameObject.SetActive(true);
        ShowMsg("授权成功");
    }
 
    /// <summary>
    /// 加载头像协程
    /// </summary>
    /// <param name="url">头像url</param>
    /// <returns></returns>
    private IEnumerator LoadUserIcon(string url)
    {
        WWW www = new WWW(url);
        yield return www;
        Texture2D texture = www.texture;
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        userIconImg.sprite = sprite;
    }
 
    /// <summary>
    /// 点击登录按钮
    /// </summary>
    private void OnLoginBtnClicked()
    {
        EventCenter.Broadcast(EventTypes.RequestAuth, null);
    }
 
    /// <summary>
    /// 显示提示消息
    /// </summary>
    /// <param name="msg"></param>
    private void ShowMsg(string msg)
    {
        StartCoroutine(ShowMsgCor(msg));
    }
 
    /// <summary>
    /// 显示提示消息协程
    /// </summary>
    /// <param name="msg"></param>
    /// <returns></returns>
    private IEnumerator ShowMsgCor(string msg)
    {
        msgText.text = msg;
        yield return new WaitForSeconds(3f);
        msgText.text = "";
    }
 
    /// <summary>
    /// 点击取消授权按钮
    /// </summary>
    private void OnLogoutBtnClicked()
    {
        EventCenter.Broadcast(EventTypes.CancelAuth, null);
        loginBtn.gameObject.SetActive(true);
        logoutBtn.gameObject.SetActive(false);
        usernameText.text = "";
        userIconImg.sprite = null;
    }
 
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventTypes.AuthSuccess, OnAuthSuccess);
    }
}

上面的这种写法是指定平台分享:

如果将

 shareSdk.ShareContent(PlatformType.QQ, content);

替换成:

shareSdk.ShowPlatformList(null, content, 100, 100);

即可以显示全平台分享

其中分享传递的参数大家需要根据分享的平台来进行设定!

可以参考Demo.cs或者关注Mob的技术论坛:http://bbs.mob.com/forum.php

猜你喜欢

转载自blog.csdn.net/qq_36848370/article/details/103125352