Unity使用ShareSDK 分享、并发布Android及ios
在使用Unity发布Android及ios时应用程序时,往往会用到分享功能,即是分享到某一平台如:QQ、微信、微博,这里为大家详细介绍一下如何成功使用ShareSDK分享插件发布分享信息:
1. 首先:
在ShareSDK 官网下载http://mob.com/“ShareSDK社会化”分享插件,选择——>Unity 的图标 (ShareSDK For Unity3D )
2. 根据ShareSDK 官网中有关Unity3D快速集成教程:
将项目中的Unity3dDemo/Assets/Plugins目录拷贝到您的项目的Assets目录中。 注意该操作可能会覆盖您原来已经存在的文件(如:AndroidManifest.xml,重命名不能覆盖,也不能不加入,后面还有重要用途)!
重要步奏:
1.初始化你的ShareSDK、平台信息如下
void Start()
{
ShareSDK.setCallbackObjectName("Main Camera");
//**********************************在shareSDK官网注册
ShareSDK.open("23bf46c00544");
//**********************************app_Key及app_secret及redirect_uri均在相应开发者平台注册得到
//Sina Weibo
Hashtable sinaWeiboConf = new Hashtable();
sinaWeiboConf.Add("app_key", "4059252106");
sinaWeiboConf.Add("app_secret", "0bc591308999f4c7f4694714ba665d36");
sinaWeiboConf.Add("redirect_uri", "https://api.weibo.com/oauth2/default.html");
//sinaWeiboConf.Add("redirect_uri","http://www.baidu.com");
sinaWeiboConf.Add("bypassapproval","true");
ShareSDK.setPlatformConfig(PlatformType.SinaWeibo, sinaWeiboConf);
//Tencent Weibo
Hashtable tcConf = new Hashtable();
tcConf.Add("app_key", "801307650");
tcConf.Add("app_secret", "ae36f4ee3946e1cbb98d6965b0b2ff5c");
tcConf.Add("redirect_uri", "http://www.sharesdk.cn");
ShareSDK.setPlatformConfig(PlatformType.TencentWeibo, tcConf);
//QZone
Hashtable qzConf = new Hashtable();
qzConf.Add("app_id", "100371282");
qzConf.Add("app_key", "aed9b0303e3ed1e27bae87c33761161d");
ShareSDK.setPlatformConfig(PlatformType.QZone, qzConf);
//WeChat
Hashtable wcConf = new Hashtable();
wcConf.Add("app_id", "wx1927ee4fa06136d2");
wcConf.Add("app_secret", "03781b71830487581967e91985db31f1");
ShareSDK.setPlatformConfig(PlatformType.WeChatSession, wcConf);
ShareSDK.setPlatformConfig(PlatformType.WeChatTimeline, wcConf);
ShareSDK.setPlatformConfig(PlatformType.WeChatFav, wcConf);
//QQ
Hashtable qqConf = new Hashtable();
qqConf.Add("app_id", "100371282");
ShareSDK.setPlatformConfig(PlatformType.QQ, qqConf);
//Facebook
Hashtable fbConf = new Hashtable();
fbConf.Add("api_key", "107704292745179");
fbConf.Add("app_secret", "38053202e1a5fe26c80c753071f0b573");
ShareSDK.setPlatformConfig(PlatformType.Facebook, fbConf);
//Twitter
Hashtable twConf = new Hashtable();
twConf.Add("consumer_key", "mnTGqtXk0TYMXYTN7qUxg");
twConf.Add("consumer_secret", "ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc");
twConf.Add("redirect_uri", "http://www.sharesdk.cn");
ShareSDK.setPlatformConfig(PlatformType.Twitter, twConf);
}’
2.使用应用:
‘ using cn.sharesdk.unity3d;’
3.在分享处添加分享内容:
‘if (GUI.Button(0,0,100,100))
{
string imagePath = Application.persistentDataPath + "/screen.png";
Hashtable content = new Hashtable();
content["content"] = "分享内容";
if (System.IO.File.Exists(imagePath))
{
content["image"] = imagePath;
}
content["title"] = "分享标题";
content["url"] = "http://www.baidu.com";
content["type"] = Convert.ToString((int)ContentType.Image);
ShareResultEvent evt = new ShareResultEvent(ShareResultHandler);
ShareSDK.showShareMenu(null, content, 100, 100, MenuArrowDirection.Up, evt);
buttonDown = 4;
}'
4.其中shareResultHandler为回调方法,其定义如下:
‘void ShareResultHandler(ResponseState state, PlatformType type, Hashtable shareInfo, Hashtable error, bool end)
{
if (state == ResponseState.Success)
{
print(“share result :”);
print(MiniJSON.jsonEncode(shareInfo));
}
else if (state == ResponseState.Fail)
{
print(“fail! error code = ” + error[“error_code”] + “; error msg = ” + error[“error_msg”]);
}
else if (state == ResponseState.Cancel)
{
print(“cancel !”);
}
}’
5.重要功能(不易发现):如何修改分享图标个数以及分享图标先后顺序
安卓:
在工程中找到引入的ShareSDK.xml,打开:
在这里面注意几个参数所代表信息:
SortID:分享平台顺序
ShareByAppClient:是否客服端打开
Enable:是否可用
ByPassApproval:是否绕过授权
(PS:为避免问题,最后将这里的app_Key及app_secret及redirect_uri修改为均在相应开发者平台注册得到)
6.修改AndroidManifest.xm.l中信息:
当拷贝下载得到的文件Plugins目录拷贝到您的项目的Assets目录中时,原本工程中也有一个AndroidManifest.xm.l文件,这样就得将两个.xml文件融合到同一个文件中;
1.添加权限:
2.添加相应Activity:
[code=plain]
注:
如果出现错误信息:
AndroidJavaException : java.lang.NullPointerException:Please call ShareSDK.initSDK(Context)befor any action.
请在AndroidMainFest.xml中的
IOS分享如下:
1.首先将工程转换为ios工程
2.再在Mac中发布为XCode: