首先我们先要认识一下Mob这个网站,我做这个社会化的分享功能,就是用的它们提供的ShareSDK实现的,打开Mob后,主要需要看如图所示的2个目录栏:
这里是为了下载ShareSDK的Unity3D工具类,这里是下载ShareSDK的地址
在文档中心这里,主要是介绍怎么应用下载S的hareSDK和通用部分集成。
一、通用部分集成
1、下载ShareSDK的Unity3D工具类
打开Github下载New-Unity-For-ShareSDK项目。将项目中的Unity3dDemo/Assets/Plugins目录拷贝到您的项目的Assets目录中,或双击ShareSDKForU3DWithEditor.unitypackage导入相关文件。 注意该操作可能会覆盖您原来已经存在的文件!
2、.挂接ShareSDK脚本并配置平台信息
选择好需要挂接的GameObject(例如Main Camera),在右侧栏中点击Add Component,选择Share SDK 进行挂接。
挂接后会发现提供了当前支持的平台和及其配置信息。可以直接在此处修改你所需要的平台的配置信息。需要注意的是当前的编译环境是Android还是iOS,其字段名称是不同的哦!
第一个App Key指的是ShareSDK的appkey,可以到Mob官网注册应用获得。其他的就是各社交平台各自的配置信息。
除了可以上图处设定配置信息,也可以在ShareSDKDevInfo.cs文件中配置所需的平台信息,效果都是一样的,步骤如下:
i.配置您自己的ShareSDK的AppKey
public class AppKey
{
//配置ShareSDK AppKey
public string appKey = "a5d9150e8348";
}
ii.选择所需要的平台,不想要的可以直接注释或删掉
public class DevInfoSet
{
public SinaWeiboDevInfo sinaweibo;
public TencentWeiboDevInfo tencentweibo;
public QQ qq;
public QZone qzone;
}
iii.配置对应平台的信息(建议直接修改字符串值即可).
public class SinaWeiboDevInfo : DevInfo
{
#if UNITY_ANDROID
public const int type = (int) PlatformType.SinaWeibo;
public string SortId = "1";
public string AppKey = "568898243";
public string AppSecret = "38a4f8204cc784f81f9f0daaf31e02e3";
public string RedirectUrl = "http://www.sharesdk.cn";
public string ShareByAppClient = "false";
#elif UNITY_IPHONE
public const int type = (int) PlatformType.SinaWeibo;
public string app_key = "568898243";
public string app_secret ="38a4f8204cc784f81f9f0daaf31e02e3";
public string redirect_uri = "http://www.sharesdk.cn";
public string auth_type = "both"; //can pass "both","sso",or "web"
#endif
}
3、进行分享、授权,请先引入命名空间:使用ShareSDK:
using cn.sharesdk.unity3d;
private ShareSDK ssdk;
分享步骤:
i.定制分享信息
ShareContent content = new ShareContent();
content.SetText("this is a test string.");
content.SetImageUrl("https://f1.webshare.mob.com/code/demo/img/1.jpg");
content.SetTitle("test title");
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.Webpage);
ii.制定分享的回调
ssdk.shareHandler = ShareResultHandler;
以下为回调的定义:
void ShareResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
print ("share result :");
print (MiniJSON.jsonEncode(result));
}
else if (state == ResponseState.Fail)
{
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
iii.然后将内容传入分享接口
ssdk.ShowPlatformList(null, content, 100, 100);
授权步骤:
i.指定授权的回调
ssdk.authHandler = AuthResultHandler;
以下为回调的定义:
void AuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
print ("authorize success !");
}
else if (state == ResponseState.Fail)
{
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
ii.进行授权
ssdk.Authorize(PlatformType.SinaWeibo);
获取用户信息
i.指定获取用户信息的回调
sdk.showUserHandler = GetUserInfoResultHandler;
以下为回调的定义:
void GetUserInfoResultHandler (int reqID, ResponseState state, PlatformType type, Hashtable result)
{
if (state == ResponseState.Success)
{
print ("get user info result :");
print (MiniJSON.jsonEncode(result));
}
else if (state == ResponseState.Fail)
{
print ("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
ii.获取用户信息
ssdk.GetUserInfo(PlatformType.SinaWeibo);
完成上面的基础知识介绍后,我们就可以根据了解来实现自己想要的功能啦,在这里我想提一下,一个容易被忽视的问题,如图:
下面看一下我的成果吧,我实在电脑上的模拟器中实验的,效果如图所示:
我的源码在这里,欢迎下载,喜欢的老铁们,点个赞吧!!!
参考网址
[1]: http://blog.sina.com.cn/s/blog_7f0562030102w32c.html/
[2]: http://blog.csdn.net/hongyouwei/article/details/45533063e