unity使用Shar SDK登录微信和分享到微信好友朋友圈(Android)

编程入门 行业动态 更新时间:2024-10-26 07:40:11

unity使用Shar SDK登录微信和分享到微信好友<a href=https://www.elefans.com/category/jswz/34/1768857.html style=朋友圈(Android)"/>

unity使用Shar SDK登录微信和分享到微信好友朋友圈(Android)

       由于最近工作需要,需要微信的第三方登录和分享功能。自己在网上看了一些分享资料,然后选择了Share sdk来制作,中途也踩了不少的坑。在这里总结下来,让自己加强记忆,也避免遗忘。

一、配置环境修改SDK脚本

1、去官网下载Share SDk,注册一个账号,然后申请一个应用,记录下你的Appkey和AppSecret备用。

2、在微信开放平台申请一个移动应用(需要注册年审)。在申请的时候要注意包名和应用签名,包名就是player setting里面的Bundle Identifier:com.XXX.XXX;应用签名就是你的keystore的MD5,你可以选择用签名工具去获取或者自己重新导出一个keystore,要注意是MD5那一排序列号去掉中间的:,然后还要全部小写.然后等待腾讯审核通过你就能得到你的App ID和Secret。然后在Mob开发后台你中你刚刚申请的应用中的社会化平台设置中,选中微信填上你的微信应用的ID和Secret。

3、打开Unity工程,将下载下来的ShareSDK.unitypackage导入工程,寻找MOb的客服,提供你的包名,请他帮你打包一个Democallback.jar的包用于替换掉你工程里面的那个包。把Share SDK脚本放到你的任意物体上,然后需要把前2排的AppKey和AppSecret改成你在Mob平台申请的应用的AppKey和AppSecret,把WeChat和WeChatMoments中的App Id和App Secret更改为你的微信应用的ID和Secret。打开Plugins/Android/ShareSDK/AndroidManifest.xml文件将

package改成你的包名,然后将

name中的wxapi前加上你的包名。

然后打开ShareSDkDevInfo脚本,可以将你不需要的平台全部注释掉,然后把WeChat和WeChatMoments中的AppId和AppSecret改成你的微信的ID和Secret。在这里如果你分享的时候要绕过审核就不用管BypassApproval,如果不绕过就把这个bool值改成false,同样在unity的Inspect面板中要把Share SDK的BypassApproval的√去掉。

打开Share SDk脚本,将Appkey和AppSecret更改成你的Mob平台申请的应用的key和Secret。

到此,配置已经全部配完,需要修改的东西也已经改完,接下来就是开始写我们的微信登录和分享脚本。

二、微信登录与分享

1、使用ShareSDK登录微信

直接上代码,在代码注释中标注了一些注意事项,登录代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using cn.sharesdk.unity3d;public class WeChatLogin : MonoBehaviour
{ShareSDK sharesdk;
//这里我登录之后需要获取到头像、昵称和OpenIDprivate  Text UserNameText;private  Image UserIconImage;private static string UserName;private static string UserIcon;private static string UserOpenID;void Start(){sharesdk = GameObject.Find("Main Camera").GetComponent<ShareSDK>();UserNameText = GameObject.Find("Name").GetComponent<Text>();UserIconImage = GameObject.Find("Icon").GetComponent<Image>();sharesdk.authHandler += OnAuthResultHandler;//授权回调sharesdk.showUserHandler += OnGetUserInfoResultHandler;//获取用户信息GameObject.Find("WeChatLogin").GetComponent<Button>().onClick.AddListener(delegate{//print("登录");sharesdk.GetUserInfo(PlatformType.WeChat);//授权只有第一次是手动授权,之后都是自动授权,更改微信账号后会重新授权});GameObject.Find("WeChatCancelLogin").GetComponent<Button>().onClick.AddListener(delegate{// print("取消登录");sharesdk.CancelAuthorize(PlatformType.WeChat);CancelLogin();});GameObject.Find("text").GetComponent<Text>().text = sharesdk.IsAuthorized(PlatformType.WeChat)+"";if (sharesdk.IsAuthorized(PlatformType.WeChat))sharesdk.GetUserInfo(PlatformType.WeChat);}public void CancelLogin()//取消授权{if(!sharesdk.IsAuthorized(PlatformType.WeChat))//IsAuthorized可以判断授权是否有效{UserIcon = null;UserName = null;UserIconImage.sprite = null;UserNameText.text = null;}}void OnAuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result){if (state == ResponseState.Success){sharesdk.GetUserInfo(type);}else if (state == ResponseState.Fail){
#if UNITY_ANDROIDprint("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONEprint ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif}else if (state == ResponseState.Cancel){print("cancel !");}}void OnGetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result){if (state == ResponseState.Success){switch (type){case PlatformType.WeChat:result = sharesdk.GetAuthInfo(PlatformType.WeChat);//获取到用户信息,可以在里面去到头像地址、用户名、OpenID等UserIcon = (string)result["userIcon"];UserName = (string)result["userName"];UserOpenID = (string)result["openID"];//GameObject.Find("text").GetComponent<Text>().text = MiniJSON.jsonEncode(result);StartCoroutine(GetUserIcon(UserIcon));//获取到的头像是一个地址,所以使用www加载break;}}else if (state == ResponseState.Fail){print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);}else if (state == ResponseState.Cancel){print("cancel !");}}IEnumerator GetUserIcon(string url){WWW www = new WWW(url);yield return www;if (www != null && string.IsNullOrEmpty(www.error)){Texture2D texture = www.texture;Sprite iconSprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));UserIconImage.sprite = iconSprite;UserNameText.text = UserName;}}
}

2、微信好友与朋友圈的分享

也是直接上代码,注意点见注释。代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using cn.sharesdk.unity3d;
using UnityEngine.UI;
using UnityEngine.EventSystems;public class WeChatShare : MonoBehaviour
{ShareSDK sharesdk;private GameObject SharePanel;void Start (){sharesdk = GameObject.Find("Main Camera").GetComponent<ShareSDK>();sharesdk.shareHandler = OnShareResultHandler;//分享回调SharePanel = GameObject.Find("SharePanel").gameObject;GameObject.Find("WeChatShare").GetComponent<Button>().onClick.AddListener(delegate{if (sharesdk.IsAuthorized(PlatformType.WeChat))//判断是否授权,没授权则不能分享{//print("分享");ScreenShot();}});GameObject.Find("Friends").GetComponent<Button>().onClick.AddListener(delegate{//print("好友");ShareImage();});GameObject.Find("Moments").GetComponent<Button>().onClick.AddListener(delegate{// print("朋友圈");ShareImage();});SharePanel.SetActive(false);}public void ScreenShot(){Application.CaptureScreenshot("ShareImage.png");//屏幕截图SharePanel.SetActive(true);}public void ShareImage(){ShareContent content = new ShareContent();content.SetText("测试一下share sdk 分享功能");content.SetImagePath(Application.persistentDataPath+ "/ShareImage.png");//手机路径使用的是SetImagePathcontent.SetShareType(ContentType.Image);//分享的类型,image就是只分享图片content.SetTitle("测试");string BtnName = EventSystem.current.currentSelectedGameObject.name;switch (BtnName){case "Friends":sharesdk.ShareContent(PlatformType.WeChat, content);//分享到微信好友break;case "Moments":sharesdk.ShareContent(PlatformType.WeChatMoments, content);//分享到微信朋友圈break;default:break;}}void OnShareResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result){if (state == ResponseState.Success){print("share successfully - share result :");print(MiniJSON.jsonEncode(result));}else if (state == ResponseState.Fail){#if UNITY_ANDROIDprint("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);#elif UNITY_IPHONEprint ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);#endif}else if (state == ResponseState.Cancel){print("cancel !");}}
}

至此,unity使用Shar SDK登录微信和分享到微信好友朋友圈的Android版本全部结束,iOS的等需要再试。

更多推荐

unity使用Shar SDK登录微信和分享到微信好友朋友圈(Android)

本文发布于:2024-02-10 13:24:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1675698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:朋友圈   好友   Shar   unity   SDK

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!