接入ShareSDK和打包DemoCallback.jar

1.从MOB官网下载ShareSDK的工程视图如下
这里写图片描述
2.导入对应平台的资源或者打开Unity的demo 项目将Plugins目录导入自己的工程zhong
3.将脚本ShareSDK挂在场景中的不会被销毁物体上,如MainCamera
4.在ShareSDK官网上注册账号,并添加一个应用,这时就可以拿到App Key和App Secret,并将ShareSDK中的appKey和appSecret修改为你应用的App Key和App Secret
5.修改不同应用的AppID和App Secret 譬如那微信举例
1.unity转换安卓或苹果平台
2. 在ShareSDK挂载对象上修改
这里写图片描述

3.在ShareSDKDevInfo脚本中修改
这里写图片描述
4.最后编写ShareSdkOperate脚本, 用来实现微信授权,登录,分享按钮事件以及ShareSDK 登录和分享的回调事件

using UnityEngine;
using System.Collections;
using cn.sharesdk.unity3d;
public class ShareSdkOperate : MonoBehaviour 
{
    //先授权 在授权回调中请求用户信息,在回调中得到用户信息
    public static ShareSDK shareSdk;
    void Start () 
    {
        GameObject go = GameObject.Find("LuaTools");
        if (go != null)
        {
            shareSdk = transform.GetComponent<ShareSDK>();
        }
        else
        {
            Debug.Log("LuaTools没有找到");
        }
        shareSdk.showUserHandler = GetUserInforCallback;//定义登录回调权限
        shareSdk.shareHandler = ShareCallBack;//分享权限
        shareSdk.authHandler = AuthResultHandler;
    }
    //授权
    public static void Auth()
    {
        shareSdk.Authorize(PlatformType.WeChat);
    }
    //登录
    public static void WxLogin()
    {
        shareSdk.GetUserInfo(PlatformType.WeChat); //获取用户信息
    }

    /// <summary>
    /// 分享图片
    /// </summary>
    /// <param name="title">标题</param>
    /// <param name="contents">内容</param>
    /// <param name="url">图片地址</param>
    public static void ShareImage(string title,string contents,string url,string type)
    {
        ShareContent content = new ShareContent();
        content.SetText(contents);//分享内容
        content.SetImageUrl(url);//图片地址
        content.SetTitle(title);//标题
        content.SetComment(contents);
        content.SetShareType(ContentType.Image);
        switch (type)
        {
            case "WeChat":
                shareSdk.ShareContent(PlatformType.WeChat, content);//分享到微信朋友
                break;
            case "WeChatMoments":
                shareSdk.ShareContent(PlatformType.WeChatMoments, content);//分享到微信朋友
                break;
            default: break;
        }
        //shareSdk.ShowPlatformList(new PlatformType[] { PlatformType.WeChat }, content, 100, 100);
    }

    public static void ShareUrl(string title,string contents,string imageUrl,string webUrl,string type)
    {
        ShareContent content = new ShareContent();
        content.SetText(contents);//分享内容
        content.SetImageUrl(imageUrl);//网址图片icon
        content.SetUrl(webUrl);//网址
        content.SetTitle(title);//标题
        content.SetShareType(ContentType.Webpage);
        switch (type)
        {
            case "WeChat":
                shareSdk.ShareContent(PlatformType.WeChat, content);//分享到微信朋友
                break;
            case "WeChatMoments":
                shareSdk.ShareContent(PlatformType.WeChatMoments, content);//分享到微信朋友
                break;
            default: break;
        }
    }


    //授权回调
    public static void AuthResultHandler(int reqId, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            Debug.Log("授权成功");
        }
        else if (state == ResponseState.Fail)
        {
            Debug.Log("授权失败" + "error msg==" + result["msg"]);
        }
        else if (state == ResponseState.Cancel)
        {
            Debug.Log("授权取消");
        }   
    }

    //登录回调
    public static void GetUserInforCallback(int reqID,ResponseState state,PlatformType type,Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            Debug.Log("微信的个人信息回调成功");
            Debug.Log(cn.sharesdk.unity3d.MiniJSON.jsonEncode(result));
            Auth();
        }
        else if (state == ResponseState.Fail)
        {
            Debug.Log("微信的个人信息回调失败");

        }
        else if (state == ResponseState.Cancel)
        {
            Debug.Log("微信的个人信息回调取消");

        }
    }

    //分享回调
    public static void ShareCallBack(int reqId,ResponseState state,PlatformType type,Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            Debug.Log("分享成功");
            Debug.Log(cn.sharesdk.unity3d.MiniJSON.jsonEncode(result));
        }
        else if (state == ResponseState.Fail)
        {
            Debug.Log("分享失败"+"error msg=="+result["msg"]);
        }
        else if (state == ResponseState.Cancel)
        {
            Debug.Log("分享取消");
        }   

    }
}

5.打出DemoCallback.jar替换Plugins/Android/ShareSDK/libs中的DemoCallback.jar
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/baidu_39447417/article/details/79105281