这两天研究了一下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。

1
2
3
4
5
6
7
8
//读、写的路径
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());
}

 这样在分享图片的时候

1
2
3
4
5
6
//image的路径就可以直接写了。
string imagePath = Application.persistentDataPath + "/0.png";
if(System.IO.File.Exists(imagePath))
{
     content["image"] = imagePath;
}

 这里再说一下,如果你想分享的是一张截屏图片,路径这样来写。

1
2
Application.CaptureScreenshot("screen.png");
content["image"] = Application.persistentDataPath + "/screen.png";

 为了让安卓和iPhone的通用性,所以我只能把Resources一张需要分享的图片拷贝到Application.persistentDataPath 路径下面。

 如下图所示, 微博 朋友圈都分享成功了,朋友圈的图我就不截了。

下面上完整代码  另外 appid和 appkey我都用了sharesdk自带的,最好自己申请一下。

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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。

 
1
2
3
4
5
#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的状态,那么我就没办法获取用户是否分享成功的事件。。 我咨询了一下它们的客服。。他告诉我:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
打开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里面。

最新文章

  1. java程序员快速掌握python系列——概述
  2. 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串
  3. php对二维数组进行相关操作(排序、转换、去空白等)
  4. sublime text3 编译less
  5. C语言 百炼成钢13
  6. Spring Bean配置默认为单实例 pring Bean生命周期
  7. 利用glassfish4任意文件读取拿权限的一些思路
  8. Android二手交易平台,dagger2+mvp+Bmob后台云搭建
  9. [js综合问题汇总]js窗口关闭事件,表单名称,父窗口子窗口,var变量名
  10. queue与topic的技术特点对比
  11. ios9中 UIStackView的使用
  12. PHP - 目录、文件操作
  13. DFS 练习 (这篇真的是随笔)
  14. FileSystemXmlApplicationContext方法的绝对路径问题
  15. React+ES6+Webpack环境配置
  16. APACHE服务器出现No input file specified.解决方案
  17. 四、XML语言学习(1)
  18. BZOJ2002[Hnoi2010]弹飞绵羊——LCT
  19. Omnibus test
  20. 【转】Java迭代:Iterator和Iterable接口

热门文章

  1. Top K问题-BFPRT算法、Parition算法
  2. 用dynamic的方式来转换Json对象
  3. python_2_变量的使用2
  4. P2878 [USACO07JAN]保护花朵Protecting the Flowers
  5. 【洛谷P1063】NOIP2006能量项链
  6. 激活SQLPrompt7.4及以上版本
  7. C# break语句
  8. vue 城市搜索组件
  9. 【Java】Maven安装、Eclipse配置以及相关错误解决集合
  10. C++ 类型转换(conv.)