参考:https://www.cnblogs.com/mufei/p/10084323.html
资源:https://github.com/MobClub/New-Unity-For-ShareSDK(下载导入unity)
using cn.sharesdk.unity3d;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
/// <summary>
/// QQ按钮
/// </summary>
private Button QQBtn;
/// <summary>
/// 分享按钮
/// </summary>
private Button ShareBtn;
/// <summary>
/// SDK
/// </summary>
public ShareSDK ssdk;
/// <summary>
/// 控制台
/// </summary>
public Text text;
/// <summary>
/// 获取到的用户信息保存本地的文件名
/// </summary>
private string fileName;
/// <summary>
/// 获取到用户的头像保存到本地的文件名
/// </summary>
private string iconName;
/// <summary>
/// 可以分享的平台
/// </summary>
private PlatformType[] platforms;
public void Start()
{
Debug.Log(ssdk);
platforms = new PlatformType[] { PlatformType.WeChat,PlatformType.WeChatFavorites,PlatformType.WeChatMoments,PlatformType.WechatPlatform,
PlatformType.QQ,PlatformType.QZone,PlatformType.SinaWeibo};
// userName = transform.Find("UserName").GetComponentInChildren<InputField>();
// passWord = transform.Find("PassWord").GetComponentInChildren<InputField>();
//exitBtn = transform.Find("ExitBtn").GetComponent<Button>();
QQBtn = transform.Find("QQBtn").GetComponent<Button>();
ShareBtn = transform.Find("QQShareBtn").GetComponent<Button>();
//exitBtn.onClick.AddListener(ExitButtonHandle);
QQBtn.onClick.AddListener(QQButtonHandle);
ShareBtn.onClick.AddListener(ShareButtonHandle);
}
/// <summary>
/// QQ登录
/// </summary>
private void QQButtonHandle()
{
Debug.Log("点击了QQ登录");
fileName = "/qq.json";
iconName = "/qqIcon.jpg";
if (File.Exists(Application.persistentDataPath + fileName))
return;
//注册登录回调事件
ssdk.authHandler = AuthHandler;
//确定向哪个平台进行第三方验证
ssdk.Authorize(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)
{
Debug.Log("回调函数");
if (state == ResponseState.Success)
{
SaveUserInfo(data.ToString());
string icon = data["icon"].ToString();
StartCoroutine(DownUserIcon(icon));
text.text += "\n userid : " + data["userID"] + "\n username : " + data["nickname"] + "\n icon : " + data["icon"];
text.text += "\n授权成功!!!";
}
else if (state == ResponseState.Fail)
{
text.text += "\n授权失败!!!";
}
}
/// <summary>
/// 分享
/// </summary>
private void ShareButtonHandle()
{
//分享的内容
ShareContent content = new ShareContent();
if (iconName != null)
content.SetImagePath(Application.persistentDataPath + iconName);
content.SetTitle(" 分享 ");
content.SetTitleUrl("https://www.baidu.com/");
content.SetText(" wecome ");
content.SetSite("Mob-ShareSDK");
content.SetSiteUrl("https://www.baidu.com/");
content.SetUrl("https://www.baidu.com/");
content.SetComment("test description");
content.SetMusicUrl("http://fjdx.sc.chinaz.com/Files/DownLoad/sound1/201807/10300.mp3");
content.SetShareType(ContentType.Webpage);
Debug.Log(" ******* 001 ");
//注册分享回调事件
ssdk.shareHandler = ShareHandler;
//传递需要分享的平台及分享内容
ssdk.ShowPlatformList(platforms, content, 0, 0);
}
/// <summary>
/// 分享回调
/// </summary>
/// <param name="reqID"></param>
/// <param name="state"></param>
/// <param name="type"></param>
/// <param name="data"></param>
private void ShareHandler(int reqID, ResponseState state, PlatformType type, Hashtable data)
{
if (state == ResponseState.Success)
{
Debug.Log(" share is success ");
Debug.Log(data);
}
else if (state == ResponseState.Fail)
{
Debug.Log(" share is fail ");
}
}
/// <summary>
/// 将用户的头像下载
/// </summary>
/// <param name="icon"></param>
/// <returns></returns>
private IEnumerator DownUserIcon(string icon)
{
Debug.Log("开启协程进行资源下载");
WWW www = new WWW(icon);
yield return www;
FileStream stream = File.Create(Application.persistentDataPath + iconName);
Texture2D texture = new Texture2D(www.texture.width, www.texture.height);
www.LoadImageIntoTexture(texture);
byte[] bytes = texture.EncodeToJPG();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
}
/// <summary>
/// 将得到的用户信息保存
/// </summary>
/// <param name="jsonFile"></param>
private void SaveUserInfo(string jsonFile)
{
if (File.Exists(Application.persistentDataPath + "/" + fileName))
File.Delete(Application.persistentDataPath + "/" + fileName);
File.WriteAllText(Application.persistentDataPath + "/" + fileName, jsonFile);
}
}