Access ShareSDK and package DemoCallback.jar

1. Download the project view of ShareSDK from the MOB official website as follows
write picture description here
2. Import the resources of the corresponding platform or open the demo project of Unity and import the Plugins directory into your own project
3. Hang the script ShareSDK on the objects that will not be destroyed in the scene, such as MainCamera
4. Register an account on the ShareSDK official website and add an application, then you can get the App Key and App Secret, and change the appKey and appSecret in the ShareSDK to the App Key and App Secret of your application
5. Modify different applications AppID and App Secret such as WeChat example
1. Unity converts Android or Apple platform
2. Modify on ShareSDK mount object
write picture description here

3. Modify in the ShareSDKDevInfo script
write picture description here
4. Finally write the ShareSdkOperate script to implement WeChat authorization, login, share button events and callback events for ShareSDK login and sharing

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. Type DemoCallback.jar to replace DemoCallback.jar in Plugins/Android/ShareSDK/libs
write picture description here
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325543507&siteId=291194637