帮助与文档

> >

重点注意:ShareSDK Android for Unity3D 有分新版本和旧版本,本教程是新版教程,旧版教程请戳:网页链接

一、获取AppKey

如何在我们的官网注册应用得到appkey,请点击链接看里面的操作步骤。

二、下载SDK

1、下载ShareSDK的Unity3D工具类

打开Github下载New-Unity-For-ShareSDK项目。双击或导入ShareSDK.unitypackage导入相关文件。 注意该操作可能会覆盖您原来已经存在的文件!

三、快速集成

1、双击或导入ShareSDK.unitypackage导入相关文件。 注意该操作可能会覆盖您原来已经存在的文件!

注意:集成微信分享或者登录的请先点击下这里查看,需要替换掉 jar包,如果没有环境可以联系4006852216来给您打包;

导入后如下图所示,ShareSDK目录下的AndroidManifest.xml中的包名(package)改成您自己的项目的包名;

2、.挂接ShareSDK脚本并配置平台信息

选择好需要挂接的GameObject(例如Main Camera),在右侧栏中点击Add Component,选择Share SDK 进行挂接。

step1

挂接后会发现提供了当前支持的平台和及其配置信息。可以直接在此处修改你所需要的平台的配置信息。需要注意的是当前的编译环境是Android还是iOS,其字段名称是不同的哦!

第一个App Key指的是ShareSDK的appkey,可以到Mob官网注册应用获得。其他的就是各社交平台各自的配置信息。

step2

除了可以上图处设定配置信息,也可以在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);

具体的调用方法可以查看Demo中的Demo.cs来查看;

如遇到任何问题,可跟我们的技术支持同事联系,QQ: 4006852216