unity接入QQ登录

参考: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);
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值