这里是以Android为例,IOS没试过,但应该差不多
1、进入Mob官网下载ShareSDK
官网网址:http://www.mob.com/
ShareSDK GitHub下载地址:https://github.com/MobClub/New-Unity-For-ShareSDK
2.将ShareSDK导入Unity
将下载好的ShareSDK压缩包解压,将里面的 Share_SDK.unitypackage 导入Unity中
导入Unity之后的目录结构如下:
一定要将工程的平台改为Android平台
3.挂载ShareSDK脚本
1、创建一个空物体(这里我将空物体命名为ShareSDK),将ShareSDK脚本挂载上去
注意:如果工程平台没有改为Android平台,挂载ShareSDK脚本后不会显示AppKey和AppSecret
4.到Mob官网注册一个账号
注册账号这个过程就不用多说了,注册完成后进入开发者平台,这里说一下开发者台怎么进入(因为官网和以前的不同了)
5.在开发者平台创建应用
添加应用的过程按照上面的提示填写就行了,完成之后会得到AppKey和App Secret,这两个很重要,在工程需要用到
5.接入SDK
将AppKey和AppSecret添加到上面挂载的ShareSDK脚本上,DevInfo中可以开启要接入的平台
接下来就是代码示例:
/// <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;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using cn.sharesdk.unity3d;
using LitJson;
using System;
// 挂载到ShareSDK物体上
public class QQTest : MonoBehaviour
{
private ShareSDK shareSDK;
private QQUser user;
private void Awake()
{
// 获取ShareSDK
shareSDK = 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);
}
private void OnDestroy()
{
// 移除事件监听
EventCenter.RemoveListener(EventTypes.RequestAuth, OnRequestAuth);
}
}
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);
}
}
using System;
using System.Collections.Generic;
/// <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, // 取消授权
}
显示效果
这个示例中只处理了授权成功情况,其他情况可以参照实现
示例工程下载:https://download.csdn.net/download/qq_30473517/11154355