Unity实现微信登录使用ShareSDK

首先说明,本人纯新手,不动Android也不懂iOS,作为一个这样的新手,在Unity里面实现微信登录真是费了不少劲,好在ShareSDK给集成了很多东西,就这样还是走了很多很多的弯路,为了避免后面的同学再次走弯路,我决定写下这篇博客记录下详细过程!

 

第一步:去微信开放平台申请你的应用

地址:http://open.weixin.qq.com/

创建移动应用等待审核,然后注册成为开发者,此步骤我不在细讲,这个很简单申请成功之后是这样的:

会提示你已通过,记下AppID和AppSecret这两个,以后会用到,当然你想要实现分享和登录功能需要再次申请开通接口

下面会有包名和签名

记下包名,以后也会用到。

 

 

第二步:接入ShareSDK

先去ShareSDK注册一下获取你的ShareSDK中的AppID

官网:http://www.mob.com/

 

注册成功之后点击进入后台

 

然后点击这个你就会看见这个ShareSDK点进去然后概况下面有App Key

 

记下这个App Key后面也会用到

 

然后下载Demo体验包,包我会在下面提供下载地址,避免大家走弯路

 

下载完之后是一个压缩文件:

 

文件结构是一个Unity工程和一个apk安装包

 

解压之后用Unity打开这个Unity工程

 

选择你刚才解压的路径打开这个工程

 

如果版本不符的话点下Continue就可以了,但不建议使用5.3.5以下的版本,因为有可能会出现意想不到的bug

 

打开之后是QQ登录和QQ分享,不要急往下看

 

接下来该修改你的东西了首先点下Main Camera看脚本

 

有个ShareSDK的脚本,着重看四个地方

1:AppKey

2:WeChat AppID

3:wechat AppSecret

4:BypassApproval

 

上面说的4个地方都是需要改的

第一处:改成你的SheraSDK的App Key刚才记下的

第二处:改成刚才记下的微信AppID

第三处:改成刚才记下的微信AppSecret

第四处:BypassApproval这个按钮点一下 处于不激活状态

 

上面四个地方改过之后就要修改脚本了

 

打开SheraDemo脚本修改由于不能贴太多图片我直接上代码,可直接替换!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using cn.sharesdk.unity3d; //导入ShareSdk
 
public class ShareDemo : MonoBehaviour {
 
    private ShareSDK shareSdk;
    public  Text message;
    void Start () {
        shareSdk = GetComponent<ShareSDK>();
        //分享回调事件
        shareSdk.shareHandler += ShareResultHandler;
        //授权回调事件
        shareSdk.authHandler += AuthResultHandler;
        //用户信息事件
        shareSdk.showUserHandler += GetUserInfoResultHandler;
    }
    //分享
    public void OnShareClick()
    {
        ShareContent content = new ShareContent();
        //这个地方要参考不同平台需要的参数    可以看ShareSDK提供的   分享内容参数表.docx
        content.SetText("快来和我一起玩这个游戏吧!");                            //分享文字
        content.SetImageUrl("https://f1.webshare.mob.com/code/demo/img/4.jpg");   //分享图片
        content.SetTitle("标题title");                                            //分享标题
        content.SetTitleUrl("http://www.qq.com");
        content.SetSite("Mob-ShareSDK");
        content.SetSiteUrl("http://www.mob.com");
        content.SetUrl("http://www.sina.com");                                    //分享网址
        content.SetComment("描述");
        content.SetMusicUrl("http://up.mcyt.net/md5/53/OTg1NjA5OQ_Qq4329912.mp3");//分享类型为音乐时用
        content.SetShareType(ContentType.Webpage);
        //shareSdk.ShowPlatformList(null, content, 100, 100);                      //弹出分享菜单选择列表
        shareSdk.ShowShareContentEditor(PlatformType.WeChat, content);                 //指定平台直接分享
    }
    // 分享结果回调
    void ShareResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
    {   //成功
        if (state == ResponseState.Success)
        {
            message.text =("share result :");
            message.text = (MiniJSON.jsonEncode(result)); 
        }
        //失败
        else if (state == ResponseState.Fail)
        {
            message.text = ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
        }
        //关闭
        else if (state == ResponseState.Cancel) 
        {
            message.text = ("cancel !");
        }
    }
    //授权
    public void OnAuthClick()
    {
        //请求微信授权//请求这个授权是为了获取用户信息来第三方登录
        shareSdk.Authorize(PlatformType.WeChat);
    }
    //授权结果回调
    void AuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {   
        if (state == ResponseState.Success)
        {
            message.text = ("authorize success !");
 
            //授权成功的话,获取用户信息
            shareSdk.GetUserInfo(type);  
        }
        else if (state == ResponseState.Fail)
        {
            message.text = ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
        }
        else if (state == ResponseState.Cancel)
        {
            message.text = ("cancel !");
        }
    }
    //获取用户信息
    void GetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        if (state == ResponseState.Success)
        {
            //获取成功的话 可以写一个类放不同平台的结构体,用PlatformType来判断,用户的Json转化成结构体,来做第三方登录。
            switch (type)
            {
                case PlatformType.WeChat:
                     message.text = (MiniJSON.jsonEncode(result));  //Json
 
                    break;
            }    
        }
        else if (state == ResponseState.Fail)
        {
            message.text = ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
        }
        else if (state == ResponseState.Cancel)
        {
            message.text = ("cancel !");
        }
    }
}
 
/*
//QQ用户信息结构体
 struct QQUser 
{
     public string yellow_vip_level;
     public string msg;
     public string province;
     public string gender;
     public string is_yellow_year_vip;
     public int is_lost;
     public string nickname;
     public int ret;
     public string level;
     public string city;
     public string figureurl;
     public string figureurl_1;
     public string figureurl_2;
     public string figureurl_qq_1;
     public string figureurl_qq_2;
     public string vip;
     public string is_yellow_vip;
}
*/

改完之后保存这个脚本

 

 

然后打开你的Unity工程下面的Plugins\Android\ShareSDK\AndroidManifest 这个文件修改成如下样式:吧com.xxxx.xxxx换成你的包名即可

 

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx.xxxx"
android:versionCode="2"
android:versionName="2.0" >

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<!-- 蓝牙分享所需的权限 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application>
<activity
android:name="com.mob.tools.MobUIShell"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize" >

<!--
如果集成QQ分享,或者使用QQ客户端来进行QQ空间的分享,须要在此处添加一个对ACTION_VIEW
事件的过滤器,其中的scheme是“tencent”前缀再开发者应用的加上appId。如果此过滤器不设置,
则分享结束以后不能得到正确的回调
-->
<intent-filter android:priority="1000" >
<data android:scheme="tencent100371282" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<!-- 新浪回调 -->
<intent-filter>
<action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<!-- 微信分享回调 -->
<activity
android:name="com.xxxx.xxxx.wxapi.WXEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true" />
<!-- 易信分享回调 -->
<activity
android:name=".yxapi.YXEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:excludeFromRecents="true"
android:exported="true"
android:launchMode="singleTop" />
<!-- 支付宝分享回调 -->
<activity
android:name=".apshare.ShareEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"/>

</application>

</manifest>

 

 


这样其实已经可以了 但是保险起见打开SheraSDKDevInfo脚本修所有用中文标注的 "你的微信AppID"  和  "你的微信AppSecret"  的地方AppID和AppSecret修改后保存

using UnityEngine;
using System.Collections;
using System;
 
namespace cn.sharesdk.unity3d 
{
    [Serializable]
    public class DevInfoSet
 
    { 
        public WeChat wechat;  
    }
 
    public class DevInfo 
    {    
        public bool Enable = true;
    }
 
    [Serializable]
    public class WeChat : DevInfo 
    {    
        #if UNITY_ANDROID
        public string SortId = "5";
        public const int type = (int) PlatformType.WeChat;
        public string AppId = "你的微信AppID";
        public string AppSecret = "你的微信AppSecret";
        public bool BypassApproval = true;
#elif UNITY_IPHONE
        public const int type = (int) PlatformType.WeChat;
        public string app_id = "你的微信AppID";
        public string app_secret = "你的微信AppSecret";
#endif
    }
 
    [Serializable]
    public class WeChatMoments : DevInfo 
    {
        #if UNITY_ANDROID
        public string SortId = "6";
        public const int type = (int) PlatformType.WeChatMoments;
        public string AppId = "你的微信AppID";
        public string AppSecret = "你的微信AppSecret";
        public bool BypassApproval = false;
#elif UNITY_IPHONE
        public const int type = (int) PlatformType.WeChatMoments;
        public string app_id = "你的微信AppID";
        public string app_secret = "你的微信AppSecret";
#endif
    }
 
    [Serializable]
    public class WeChatFavorites : DevInfo 
    {
        #if UNITY_ANDROID
        public string SortId = "7";
        public const int type = (int) PlatformType.WeChatFavorites;
        public string AppId = "你的微信AppID";
        public string AppSecret = "你的微信AppSecret";
#elif UNITY_IPHONE
        public const int type = (int) PlatformType.WeChatFavorites;
        public string app_id = "你的微信AppID";
        public string app_secret = "你的微信AppSecret";
#endif
    }
 
    [Serializable]
    public class WechatSeries : DevInfo 
    {
#if UNITY_ANDROID
        //for android,please set the configuraion in class "Wechat" ,class "WechatMoments" or class "WechatFavorite"
        //对于安卓端,请在类Wechat,WechatMoments或WechatFavorite中配置相关信息↑    
#elif UNITY_IPHONE
        public const int type = (int) PlatformType.WechatPlatform;
        public string app_id = "你的微信AppID";
        public string app_secret = "你的微信AppSecret";
#endif
    }
}

这一步完成就差不多大工告成了,接下来打包打成安卓包,注意修改你的包名

打包不在详细的说打出来安装到你的安卓手机上就行了点运行 分享和登录就这样完成了 GoodLuck!

 

posted on 2018-11-28 18:54  &大飞  阅读(479)  评论(0编辑  收藏  举报

导航