每个U3D程序都要面临着接入第三方SDK的问题,之前接入的原生微信登录那套,这两天公司因为某种原因放弃了,准备接入
Share SDK
的登录 和分享。
关于如何去Share官网注册,和去对应平台获取生产对应的秘钥。可以先阅读下这篇文章Unity 之MOB >ShareSDK流程详解 - 。(里面已经详细介绍了接入前的准备工作流程)
我写这篇文章是怕我以后一忙,自己忘记,毕竟这块技术含量很低,就是注意下配置的几个地方,如果是高手请忽略此文章。 不啰嗦,进入正题。
首先说下去官网下载画unity的继承 SDK,这里MOB 做的就是傻瓜式接入,这里贴上下载地址 免费SDK下载_第>三方SDK接口_Mob移动开发者平台
下载后的文件夹里找到
ShareSDk.unitypackage
,这个资源包含了所需要的安卓资源和ios资源,这样小白也就可以不用去关心资源摆放的问题。只需要关心接下来的几个配置的步骤,之后的版本虽有变化,但差别不大。
首先,在Scene中新建一个空物体,并挂上SDK提供的
ShareSDK
脚本,填入ShareSDk 的App Key
和App Secret
。打开折叠的Dev info
修改里面,所需要的平台秘钥,最后挂上ShareSDKManager
,这个是要我自己写的。
接着,是修改
AndrondManifest
配置表,将package=
后面的包名换成自己项目的
最下面填入shareSDK的秘钥,这个配置表就修改好了
再来是导出对应包名的jar包,这个可以联系客服获取,然后客服给我返回了一个工具 PackUnity
放到ta
导给我的jar包列表里,让我以后可以自己修改包名。导好的jar包放到../Android/ShareSDK/libs
文件夹下
我摘取了demo脚本里需要的代码部分放到ShareSDKManager
贴上代
ShareSDKManager
代码码
using System;
using System.Collections;
using cn.sharesdk.unity3d;
using UnityEngine;
using UnityEngine.UI;
public class ShareSDKManager : MonoBehaviour
{
public Button ShareMenuButton; //微信菜单分享按钮
public Button WeChatShareButton; //微信分享按钮
public Button LoginButton; //登录微信按钮
public Text TipsText; //提示文本
public Button CopyButton; //复制按钮
public ShareSDK Ssdk; //shareSDK
public Image Image;
private void Start ()
{
//注册shareSDK需要的回调函数
Ssdk = gameObject.GetComponent();
Ssdk.authHandler = OnAuthResultHandler;
Ssdk.shareHandler = OnShareResultHandler;
Ssdk.showUserHandler = OnGetUserInfoResultHandler;
Ssdk.getFriendsHandler = OnGetFriendsResultHandler;
Ssdk.followFriendHandler = OnFollowFriendResultHandler;
ShareMenuButton.onClick.AddListener(OnShareMeuaOnClick);
WeChatShareButton.onClick.AddListener(weChatShareButtonOnClick);
LoginButton.onClick.AddListener(LoginButtonClick);
CopyButton.onClick.AddListener(CopyButtonOnClick);
}
///
/// 复制按钮处理事件
///
private void CopyButtonOnClick()
{
GUIUtility.systemCopyBuffer = TipsText.text;
}
///
/// 微信菜单分享按钮
///
public void OnShareMeuaOnClick()
{
ShareContent content = new ShareContent();
//(Android only) 隐藏九宫格里面不需要用到的平台(仅仅是不显示平台)
//(Android only) 也可以把jar包删除或者把Enabl属性e改成false(对应平台的全部功能将用不了)
String[] platfsList = {((int)PlatformType.QQ).ToString(), ((int)PlatformType.Facebook).ToString(), ((int)PlatformType.TencentWeibo).ToString()};
content.SetHidePlatforms (platfsList);
content.SetText("这是微信菜单分享测试");
content.SetImageUrl("http://ww3.sinaimg.cn/mw690/be159dedgw1evgxdt9h3fj218g0xctod.jpg");
content.SetTitle("测试微信菜单分享");
//(Android only) 针对Android绕过审核的多图分享,传图片String数组
String[] imageArray = {"/sdcard/test.jpg", "http://f1.webshare.mob.com/dimgs/1c950a7b02087bf41bc56f07f7d3572c11dfcf36.jpg", "/sdcard/test.jpg"};
content.SetImageArray (imageArray);
content.SetTitleUrl("http://www.mob.com");
content.SetSite("Mob-ShareSDK");
content.SetSiteUrl("http://www.mob.com");
content.SetUrl("http://www.mob.com");
content.SetComment("test description");
content.SetMusicUrl("http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3");
content.SetShareType(ContentType.Image);
//不同平台分享不同内容
ShareContent customizeShareParams = new ShareContent();
customizeShareParams.SetText("Sina share content");
customizeShareParams.SetImageUrl("http://git.oschina.net/alexyu.yxj/MyTmpFiles/raw/master/kmk_pic_fld/small/107.JPG");
customizeShareParams.SetShareType(ContentType.Text);
customizeShareParams.SetObjectID("SinaID");
content.SetShareContentCustomize(PlatformType.SinaWeibo, customizeShareParams);
//优先客户端分享
// content.SetEnableClientShare(true);
//使用微博API接口应用内分享 iOS only
// content.SetEnableSinaWeiboAPIShare(true);
//通过分享菜单分享
Ssdk.ShowPlatformList (null, content, 100, 100);
}
///
/// 登录按钮
///
public void LoginButtonClick()
{
print("----------------------->>>>>>>>>"+Ssdk.IsAuthorized(PlatformType.WeChat));
TipsText.text = "点击授权返回的值:" + Ssdk.IsAuthorized(PlatformType.WeChat);
Ssdk.GetUserInfo(PlatformType.WeChat);
}
///
/// 授权回调
///
///
///
///
///
void OnAuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
if (result != null && result.Count > 0)
{
print ("authorize success !" + "Platform :" + type + "result:" + MiniJSON.jsonEncode(result));
} else
{
print ("authorize success !" + "Platform :" + type);
}
}
else if (state == ResponseState.Fail)
{
#if UNITY_ANDROID
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
print ("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)
{
print ("get user info result :");
print (MiniJSON.jsonEncode(result));
print ("AuthInfo:" + MiniJSON.jsonEncode (Ssdk.GetAuthInfo (PlatformType.WeChat)));
var infoJson = MiniJSON.jsonEncode(Ssdk.GetAuthInfo(PlatformType.WeChat));
TipsText.text = "开始解析json数据";
var weChatUserData = JsonUtility.FromJson(infoJson);
StartCoroutine(LoadUserIcon(weChatUserData.UserIcon));
// tipsText.text = "AuthInfo:" + MiniJSON.jsonEncode(ssdk.GetAuthInfo(PlatformType.WeChat));
//
// print ("Get userInfo success !Platform :" + type );
}
else if (state == ResponseState.Fail)
{
#if UNITY_ANDROID
TipsText.text = "fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"];
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
tipsText.text = "fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]];
print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
}
else if (state == ResponseState.Cancel)
{
TipsText.text = "cancel !";
print ("cancel !");
}
}
///
/// 分享回调
///
///
///
///
///
void OnShareResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
TipsText.text = "share successfully - share result :";
print ("share successfully - share result :");
print (MiniJSON.jsonEncode(result));
}
else if (state == ResponseState.Fail)
{
#if UNITY_ANDROID
TipsText.text = "fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"] ;
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
}
else if (state == ResponseState.Cancel)
{
TipsText.text = "cancel !";
print ("cancel !");
}
}
void OnGetFriendsResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
print ("get friend list result :");
print (MiniJSON.jsonEncode(result));
}
else if (state == ResponseState.Fail)
{
#if UNITY_ANDROID
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
void OnFollowFriendResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
print ("Follow friend successfully !");
}
else if (state == ResponseState.Fail)
{
#if UNITY_ANDROID
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
///
/// 下载头像
///
///
///
public IEnumerator LoadUserIcon(string url)
{
TipsText.text = url;
if (url == null)
{
TipsText.text = "输入的下载地址为空";
yield return null;
}
using (WWW www = new WWW(url))
{
yield return www;
if (string.IsNullOrEmpty(www.error))
{
var texture = www.texture;
Image.sprite=Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
else
{
TipsText.text = "头像获取为空";
}
}
}
}
[System.Serializable]
public class Root
{
///
///验证令牌
///
public string RefreshToken;
///
/// 打开id
///
public string OpenId;
///
/// ExpiresIn
///
public int ExpiresIn;
///
/// 用户性别
///
public string UserGender;
///
/// 令牌秘钥匙
///
public string TokenSecret;
///
/// 用户ID
///
public string UserId;
///
/// 工会ID
///
public string UnionId;
///
///延时
///
public int ExpiresTime;
///
/// 用户名
///
public string UserName;
///
///令牌
///
public string Token;
///
///头像地址
///
public string UserIcon;
}