> >

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

1、通用部分集成

1、下载ShareSDK的Unity3D工具类

打开Github下载New-Unity-For-ShareSDK项目。将项目中的Unity3dDemo/Assets/Plugins目录拷贝到您的项目的Assets目录中,或双击sharesdk-unity3d-plugin.unitypackage导入相关文件。 注意该操作可能会覆盖您原来已经存在的文件!

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、进行分享、授权

请先引入命名空间:
using cn.sharesdk.unity3d;
使用ShareSDK:
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);

2、Android部分集成步骤

1、下载New-Unity-For-ShareSDK项目,将Unity3D-ShareSDK/Assets/Plugins目录拷贝到您项目的Assets目录中,或双击sharesdk-unity3d-plugin.unitypackage导入相关文件时,已经将ShareSDK的快速集成到了您的项目中。

F8EBCCEA-A5F9-42A5-9129-FAFE7FD1324E

E2190F1F-5F64-47F4-A747-1B481B63025F

2、设置AndroidManifest.xml文件,添加相关的配置信息

04930835-7D1F-45C6-A898-DA9797175F82

3、完成以上配置后,就可调用相关接口实现分享、登录、获取用户信息等功能,

具体可以参考Unity3D-ShareSDK/Assets/Demo.cs文件。

需要先挂载Unity3D-ShareSDK/Assets/Plugins/ShareSDK/ShareSDK.cs文件,然后就可配置各个微博平台的key

CFB71BF8-2371-46F3-A88D-BC0F644C103D

然后就可调用代码,例如打开分享界面

123

4、运行Demo时,需要把Demo.cs 和 ShareSDK.cs挂载

5C02A01B-E641-45BE-9BB5-769BC72AE527

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