这两天研究了一下ShareSDK,说实话挺好用的,但是还是有点坑的地方。那么雨松MOMO写下博文记录一下来我遇到的坑,嘿嘿。
大部分内容它的文档上已经说的很清楚了。
http://wiki.sharesdk.cn/Unity3D%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E6%8C%87%E5%8D%97
这里我直说文档上没说的部分。
分享图片的时候 它的例子写的是 网络图片。
content[“image”] = “http://img.baidu.com/img/image/zhenrenmeinv0207.jpg”;
但是实际游戏里一般的需求是 分享一张本地图片,或者分享一张截屏后的图片。
先说我遇到的坑,我把图片放在 StreamingAssets文件夹下面,写入下面的代码。
content[“image”] = Application.streamingAssetsPath+”/Share.png”;
打包在IOS上,分享微博,微信朋友圈都没问题。但是我在切换到android平台,这样的方法死活就会报错。理论上StreamingAssets 是一个应用程序只读的文件夹,技术上完全可以做到的,不知道为什么sharesdk做不到。后来我咨询了一个他们的技术。他给我的答案是:
既然原因知道了,那么就好办了,我需要在分享前把u3d里的图片拷贝到sdcard卡里面。
在Resources文件夹下放一个图片记住一定要PNG,在U3D里面把图片的格式修改成RGBA。
//读、写的路径
string imagePath = Application.persistentDataPath + "/0.png";
//如果文件不存在,把它拷贝进去。
if(!System.IO.File.Exists(imagePath))
{
Texture2D o = Resources.Load("0") as Texture2D;
System.IO.File.WriteAllBytes(imagePath, o.EncodeToPNG());
}
这样在分享图片的时候
//image的路径就可以直接写了。
string imagePath = Application.persistentDataPath + "/0.png";
if(System.IO.File.Exists(imagePath))
{
content["image"] = imagePath;
}
这里再说一下,如果你想分享的是一张截屏图片,路径这样来写。
Application.CaptureScreenshot("screen.png");
content["image"] = Application.persistentDataPath + "/screen.png";
为了让安卓和iPhone的通用性,所以我只能把Resources一张需要分享的图片拷贝到Application.persistentDataPath 路径下面。
如下图所示, 微博 朋友圈都分享成功了,朋友圈的图我就不截了。
下面上完整代码 另外 appid和 appkey我都用了sharesdk自带的,最好自己申请一下。
using UnityEngine;
using System.Collections;
using cn.sharesdk.unity3d;
using System;
public class NewBehaviourScript : MonoBehaviour
{
void Awake ()
{
ShareSDK.setCallbackObjectName("SDK");
ShareSDK.open ("api20");
//新浪微博
Hashtable sinaWeiboConf = new Hashtable();
sinaWeiboConf.Add("app_key", "568898243");
sinaWeiboConf.Add("app_secret", "38a4f8204cc784f81f9f0daaf31e02e3");
sinaWeiboConf.Add("redirect_uri", "http://www.sharesdk.cn");
ShareSDK.setPlatformConfig (PlatformType.SinaWeibo, sinaWeiboConf);
//微信朋友圈
Hashtable weixinConf = new Hashtable();
weixinConf.Add("app_id", "wx4868b35061f87885");
ShareSDK.setPlatformConfig (PlatformType.WeChatTimeline, weixinConf);
//拷贝图片
string imagePath = Application.persistentDataPath + "/0.png";
if(!System.IO.File.Exists(imagePath))
{
Texture2D o = Resources.Load("0") as Texture2D;
System.IO.File.WriteAllBytes(imagePath, o.EncodeToPNG());
}
}
void OnGUI()
{
//分享微信
if(GUILayout.Button("weixin",GUILayout.Width(100), GUILayout.Height(100)))
{
NewBehaviourScript.TryShareMessage(PlatformType.WeChatTimeline,"Unity3D接入shardSdk 看看能不能成功。",ShareResultHandler);
}
//分享微博
if(GUILayout.Button("weibo",GUILayout.Width(100), GUILayout.Height(100)))
{
NewBehaviourScript.TryShareMessage(PlatformType.SinaWeibo,"Unity3D接入shardSdk 看看能不能成功。",ShareResultHandler);
}
}
void ShareResultHandler (ResponseState state, PlatformType type, Hashtable shareInfo, Hashtable error, bool end)
{
if (state == ResponseState.Success)
{
print ("分享成功");
print (MiniJSON.jsonEncode(shareInfo));
}
else if (state == ResponseState.Fail)
{
print ("分享失败");
print ("fail! error code = " + error["error_code"] + "; error msg = " + error["error_msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
public static void TryShareMessage(PlatformType type,string text,ShareResultEvent ShareResultHandler)
{
string imagePath = Application.persistentDataPath + "/0.png";
Hashtable content = new Hashtable();
content["content"] = text;
if(System.IO.File.Exists(imagePath))
{
content["image"] = imagePath;
}
content["title"] = "雨松MOMO程序研究院";
content["url"] = "http://xuanyusong.com";
content["type"] = Convert.ToString((int)ContentType.News);
ShareResultEvent evt = new ShareResultEvent(ShareResultHandler);
ShareSDK.shareContent (type, content, evt);
}
}
最后我在上u3d工程的包 ,已经在IOS和Android上测试通过。
http://vdisk.weibo.com/s/qDm4IY-bo7eu
6月6日补充
这个例子前段时间做完了,因为我策划还没有确定完需求,所以一直没再项目上加这个东西。 今天加到我游戏里面才发现,真的是问题多多呀。。
主要是微信,安卓到没什么问题。
你必须要在ShareSDK上注册一个号 http://sharesdk.cn/ 然后创建你的游戏,分成安卓和IOS。
#if UNITY_IPHONE
cn.sharesdk.unity3d.ShareSDK.open ("xxxxxx");
#else
cn.sharesdk.unity3d.ShareSDK.open ("xxxxxx");
#endif
xxxxxx就是你申请的ID,如果你不申请ID的话就只能用它的默认ID,即时你配了你自己申请的微信的APPID 那么还是会报错 :warning: 尚未配置微信URL Scheme:wx4868b35061f87885, 无法进行分享。 所以一定要在ShareSdk的官网上注册你自己的游戏。
还有就是我用微信分享,无论我点击分享或者点击取消分享。ShardSdk永远都给我返回一个 Cancel的状态,那么我就没办法获取用户是否分享成功的事件。。 我咨询了一下它们的客服。。他告诉我:
打开AppController.mm文件,添加ShareSDK.h头文件:
#import <ShareSDK/ShareSDK.h>新增handleOpenURL的处理方法,代码如下:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [ShareSDK handleOpenURL:url wxDelegate:nil];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];
}
这样问题就来了,因为 AppController.mm文件 是Unity自动生成的,我不能随意修改因为他会打断我写的自动化批量打包工具。。
建议大家使用XUPorter 这样的话可以打完包以后自己用代码上面的代码写在AppController.mm里面。
- 本文固定链接: https://www.xuanyusong.com/archives/2620
- 转载请注明: 雨松MOMO于 雨松MOMO程序研究院 发表



想问,解决了吗?logo怎么修改,可否告知兄弟
需要将logo图片放到drawablex哪个文件夹下,同时修改XML中的icon
是安装后直接点打开后,程序按home后台,回去应用图标打开么?
是的,不知道这个是什么原因
我是一打开应用就黑屏,我把AndroidManifest.xml 删掉了出现的。不删掉的话应用名称和图标什么的就不对了
Application.CaptureScreenshot(“screen.png”); content[“image”] = Application.persistentDataPath “/screen.png”;我直接使用的这句,在pad上预览图都没有显示出来= =我打算是分享到facebook上
Application.CaptureScreenshot(“screen.png”); content[“image”] = Application.persistentDataPath + “/screen.png”;我直接使用的这句,在pad上预览图都没有显示出来= =我打算是分享到facebook上
– (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:nil];}和unity打包出来的一个方法冲突了= =要怎么处理呀?
您好,现在Unity如何在WinPhone上实现分享功能呢?希望得到回复 万分感激
松哥,我使用ShareSDK时发现我已经用了芒果聚合的android unity3d插件,我google了下说是需要将两个插件合并,但是本人android苦手啊,能分享一下你处理这个问题的过程和经验么?
额,我试了,还是不行。。momo,你知道它这个sharesdk点分享后会弹出个字符串加缩略图,而我的问题就出现在了缩略图上,即截屏的图片与缩略图不一致,点击那个缩略图查看是当前截屏图,没错,而缩略图却是上一次的截屏图片,分享后在新浪后查看又是正确的,分享的也是我截的那张图。哎,我在网上找了各种截屏方法都试了,还是没有让缩略图显示正确。。(我是运行在安卓平台上的,momo若有时间可以测试下,在此多谢了),这样写也不行,效果一样Application.CaptureScreenshot(“screen.png”); Hashtable content = new Hashtable(); content[“content”] = “this is a test string.”; content[“image”] = Application.persistentDataPath “/screen.png”;
Application.CaptureScreenshot(“screen.png”); 就已经把图片拷贝在 persistentDataPath 路径下面了。。不需要重新拷贝。。直接用这个路径 Application.persistentDataPath “/screen.png”; 就可以了。
分享前做?可它是在点击分享按钮瞬间截屏写入的呀,我能想到的就是在new之前写入,我是这样写的,不知哪里出问题了。if (GUI.Button(new Rect((Screen.width – btnWidth) / 2, btnTop, btnWidth, btnHeight), “分享”)) { Application.CaptureScreenshot(“screen.png”); string imagetPath = Application.persistentDataPath “/screen.png”; if (Application.platform == RuntimePlatform.Android) { Texture2D o = Resources.Load(“screen”) as Texture2D; System.IO.File.WriteAllBytes(imagetPath, o.EncodeToPNG()); } string imagetPathSD = Application.persistentDataPath “/screen.png”; Hashtable content = new Hashtable(); content[“content”] = “this is a test string.”; if (System.IO.File.Exists(imagetPathSD)) { content[“image”] = imagetPathSD; }…….
图片改变了?是通过什么方法判断它改变了的,bool开关,还是截屏时间,为什么像我下面这样写分享没反映,还请大神指点下,因为对这个File还不熟,小菜了。。 Application.CaptureScreenshot(“screen.png”); string imagetPath = Application.persistentDataPath “/screen.png”; if(Application.platform == RuntimePlatform.Android) { Texture2D o = Resources.Load(“screen”) as Texture2D; System.IO.File.WriteAllBytes(imagetPath, o.EncodeToPNG()); } string imagetPathSD = Application.persistentDataPath “/screen.png”; if (System.IO.File.Exists(imagetPathSD)) { content[“image”] = imagetPathSD; }
松哥,我想问你个问题,是有关你这篇文章的,如你所说,如果像我下面这么写的话,我测试了多次,在分享UI中的缩略图第一张是正确的,可接下来的每次都是上一次的截屏缩略图,而不是当前截屏的缩略图。请问这个问题该怎么解决?(这是在安卓上的问题)我想主要是这句话 if (!System.IO.File.Exists(imagetPath))限制了手机sd卡中的图片不能得到及时更新,可是不要它直接写入却连分享窗口都不能弹出了。。if (GUI.Button(new Rect(200,200,100,50), “分享”)){ Application.CaptureScreenshot(“screen.png”); string imagetPath = Application.persistentDataPath “/screen.png”; if (!System.IO.File.Exists(imagetPath)) { Texture2D o = Resources.Load(“screen”) as Texture2D; System.IO.File.WriteAllBytes(imagetPath, o.EncodeToPNG()); } string imagetPathSD = Application.persistentDataPath “/screen.png”; if (System.IO.File.Exists(imagetPathSD)) { content[“image”] = imagetPathSD; }}
松哥,我想问你个问题,是有关你这篇文章的,如你所说,如果像我下面这么写的话,我测试了多次,在分享UI中的缩略图第一张是正确的,可接下来的每次都是上一次的截屏缩略图,而不是当前截屏的缩略图。请问这个问题该怎么解决?(这是在安卓上的问题)我想主要是这句话 if (!System.IO.File.Exists(imagetPath))限制了手机sd卡中的图片不能得到及时更新,可是不要它直接写入却连分享窗口都不能弹出了。。if (GUI.Button(new Rect(200,200,100,50), “分享”)){ Application.CaptureScreenshot(“screen.png”); string imagetPath = Application.persistentDataPath + “/screen.png”; if (!System.IO.File.Exists(imagetPath)) { Texture2D o = Resources.Load(“screen”) as Texture2D; System.IO.File.WriteAllBytes(imagetPath, o.EncodeToPNG()); } string imagetPathSD = Application.persistentDataPath + “/screen.png”; if (System.IO.File.Exists(imagetPathSD)) { content[“image”] = imagetPathSD; }}
还有个坑就是,导入Sharesdk包后,在unity打包安卓包并安装后,包的图标和名字不随unity,而是AndroidManifest.xml文件里设置的名字。到了AndroidManifest.xml里android:label=” ” >字段可以修改app名字,但没找到哪个是修改图标。把AndroidManifest.xml这个文件删除,打包安卓app图标和名字都正常,但不显示GUI。
自己加了个android:icon=”@drawable/1.png”字段来改ICON,打包时候出错
android平台 同时加入广告什么的 就打不开了我用了sharesdk 同时用腾讯的广告包名改为com.xxx.xxx之后 u3d中sharesdk onekeyshare就打不开了 AndroidManifest.xml 也改过了 始终搞不定广告什么的可以正常显示不知道站长有没有试过
在U3D 3.x中不能够使用ShareSDK吗?
发布apk到手机实验了一下,貌似微信,点击了过后没有反应!!!
狂顶
才在微信朋友圈看到的分享,跑来就只剩下小小板凳了
最近在研究unity,想请教一下关于材质的问题,一般来说材质重复平铺都是一张贴图的,但是我发现网上有些人可以做到用两张图重复平铺,也就是差不多变成黑白棋盘格那样,而且黑白两处的贴图还可以点击切换,但是我死活研究不出是怎么做的。