Unity接入ShareSDK的微信登陆和分享功能

一:安卓端

——到MobTech官网和微信开发者平台获取shareSDK的AppKey、AppSecret和微信的AppID、AppSecret

——在MobTech官网上下载Unity的ShareSDK并导入unitypackage文件:http://www.mob.com/download

——File—BuildSettings—PlayerSettings—Publishing Settings—勾选上Custom Gradle Template(集成的核心文件)和User Proguard File

——File—BuildSettings—PlayerSettings—Publishing Settings—创建并选择签名

——打开Plugins—Android—ShareSDK—AndroidManifest文件,修改Activity的name,就是在.wxapi.WXEntryActivity的activity前加上包名
Unity接入ShareSDK的微信登陆和分享功能_第1张图片

——删除这两块不必要的内容
Unity接入ShareSDK的微信登陆和分享功能_第2张图片

——微信规定WXEntryActivity.java类的位置必须放到 “项目包包名.wxapi” 路径下,需要生成新的jar包,熟悉Android Studio可以自己生成,不熟悉的跟客服说帮忙生成一个新的DemoCallback.jar,然后报上package的名字,等几分钟就可以收到新的jar
进入到目录Plugins—Android—ShareSDK—libs下,将新的jar包替换原有的DemoCallback.jar包

 

——微信登录实现
首先shareSDK.Authorize(PlatformType.WeChat)拉取微信(授权)
拉取成功后(shareSDK.authHandler = AuthResultHandler)执行AuthResultHandler回调
在AuthResultHandler回调返回成功后去获得用户信息shareSDK.GetUserInfo(PlatformType.WeChat)
获得用户信息成功后(shareSDK.showUserHandler = GetUserInfoResultHandler)执行GetUserInfoResultHandler回调GetUserInfoResultHandler回调返回成功表示登录成功

using System;
using System.Collections;
using UnityEngine;
using cn.sharesdk.unity3d;
using UnityEngine.SceneManagement;

public class Login : MonoBehaviour
{
    private ShareSDK shareSDK; //ShareSDK脚本

    private void Awake()
    {
        shareSDK = FindObjectOfType();

        shareSDK.authHandler = AuthResultHandler;
        shareSDK.showUserHandler = GetUserInfoResultHandler;
    }

    /// 
    /// 按下登陆按钮
    /// 
    public void OnLoginButtonDown()
    {
        //拉起微信
        shareSDK.Authorize(PlatformType.WeChat);
    }

    //拉取微信成功的回调
    private void AuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            shareSDK.GetUserInfo(PlatformType.WeChat);

            Debug.Log("authorize success !");
        }
        else if (state == ResponseState.Fail)
        {
            Debug.Log("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
        }
        else if (state == ResponseState.Cancel)
        {
            Debug.Log("cancel !");
        }
    }

    //得到用户信息成功的回调
    private void GetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            Debug.Log("get user info result :");
            Debug.Log(MiniJSON.jsonEncode(result));

            //保存用户信息
            PlayerPrefs.SetString("userInfo", MiniJSON.jsonEncode(result));

            //切换场景
            SceneManager.LoadScene("Main");
        }
        else if (state == ResponseState.Fail)
        {
            Debug.Log("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
        }
        else if (state == ResponseState.Cancel)
        {
            Debug.Log("cancel !");
        }
    }
}

/// 
/// 用户信息类
/// 
[Serializable]
public class Root
{
    public string nickname;

    public string headimgurl;
}

 

——微信分享实现
——勾选掉ShareSDK的Bypass Approval,否则分享不能显示
Unity接入ShareSDK的微信登陆和分享功能_第3张图片


——微信分享代码
设置shareSDK.shareHandler = ShareResultHandler回调,分享成功后执行ShareResultHandler回调

using UnityEngine;
using cn.sharesdk.unity3d;
using System.Collections;

public class Share : MonoBehaviour
{
    private ShareSDK shareSDK; //ShareSDK脚本

    private void Awake()
    {
        shareSDK = FindObjectOfType();

        shareSDK.shareHandler = ShareResultHandler;
    }

    /// 
    /// 分享的回调
    /// 
    private void ShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            Debug.Log("share result :");
            Debug.Log(MiniJSON.jsonEncode(result));
        }
        else if (state == ResponseState.Fail)
        {
            Debug.Log("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
        }
        else if (state == ResponseState.Cancel)
        {
            Debug.Log("cancel !");
        }
    }

    /// 
    /// 按下分享按钮
    /// 
    public void OnShareButtonDown()
    {
        ShareContent content = new ShareContent();
        content.SetText("邀请的文字");
        content.SetShareType(ContentType.Text);

        //WeChat:微信朋友 WeChatMoments:朋友圈
        shareSDK.ShareContent(PlatformType.WeChat, content);
    }
}

二:IOS端

 


三:从服务器下载头像

    /// 
    /// 下载头像
    /// 
    /// 头像的url
    public IEnumerator DownloadHeadicon(string url)
    {
        if (url == null)
        {
            yield return null;
        }

        UnityWebRequest uwr = UnityWebRequest.Get(url);
        DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
        uwr.downloadHandler = downloadTexture;

        yield return uwr.SendWebRequest();

        Texture2D _texture = null;
        if (!(uwr.isNetworkError || uwr.isHttpError))
        {
            _texture = downloadTexture.texture;
            headicon_img.texture = _texture;
        }
        else
        {
            Debug.Log("download Texture Error");
        }
    }

 

你可能感兴趣的:(Unity开发实战)