Unity3d 4.6.8 ShareSDK 分享新浪,微信,qq,经验总结

1.导入ShareSDK,注意不要导入AndroidManifest文件,不然会覆盖源文件。

2.合并AndroidManifest文件:

第一,添加Android:name,:如图

第二,添加四个activity,第一个activity要去掉包含 LAUNCHER  的intent-filter :

<activity
            android:name="cn.sharesdk.unity3d.demo.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="adjustPan|stateHidden" >
<!--
               KakaoTalk , 分享应用的时候,可以设置打开应用的参数 excuteUrl, 用户点击分享的内容时,就会打开相应的应用,
                如果应用不存在,可以设置 installURL参数, 打开下载应用的相关网址  (executeUrl)
               设置后intent-filter 后,分享时,需要设置参数 setExecuteUrl("kakaoTalkTest://starActivity");
            -->
            <intent-filter>
                <data android:scheme="kakaoTalkTest" android:host="starActivity"/>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
<activity
            android:name="com.mob.tools.MobUIShell"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:windowSoftInputMode="stateHidden|adjustResize" />

<!-- QQ客户端回调 -->
        <activity 
        android:name="cn.sharesdk.tencent.qq.ResultReceiver"
            android:launchMode="singleTask"
            android:noHistory="true">
            <!--
            如果集成QQ分享,或者使用QQ客户端来进行QQ空间的分享,须要在此处添加一个对ACTION_VIEW
            事件的过滤器,其中的scheme是“tencent”前缀再开发者应用的加上appId。如果此过滤器不设置,
            则分享结束以后不能得到正确的回调
            -->
            <intent-filter android:priority="1000" >
                <data android:scheme="tencent100371282" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>

<!-- 微信分享回调 -->
        <activity
            android:name=".wxapi.WXEntryActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:exported="true" /> 


3.修改Plugins/Android/assets/ShareSDK.xml文件:

第一:<ShareSDK    AppKey = "xxxxxxxxxx"/> <!-- 修改成你在sharesdk后台注册的应用的appkey"-->

第二:要分享的enable = true并填写AppKey和AppSecret ,不分享的为false。

第三:注意,ShareByAppClient="true",这个要设置成true,默认为false,false分享有问题。

4.根据教程挂在ShareSDK.cs.

5.新浪分享不成功,要设置下open.weibo.com里面的回调网址,都设置成百度即可。还有把自己的微博号添加进去用于测试用户里面,不是测试用户在app发布之前不能分享的。

6.添加自己的分享管理脚本ShareManager.cs

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using cn.sharesdk.unity3d;

public class ShareManager : MonoBehaviour
{
	public static ShareManager instance;

	void Awake()
	{
		instance = this;
	}
	void Start()
	{
		ShareSDK.setCallbackObjectName("Camera");
		ShareSDK.open("xxxxxxxxxx");
		//iOS 
		//Sina Weibo
		Hashtable sinaWeiboConf = new Hashtable();
		sinaWeiboConf.Add("app_key", "xxxxxxxxx");
		sinaWeiboConf.Add("app_secret", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");
		sinaWeiboConf.Add("redirect_uri", "http://www.sharesdk.cn");
		ShareSDK.setPlatformConfig(PlatformType.SinaWeibo, sinaWeiboConf);
		//WeChat
		Hashtable wcConf = new Hashtable();
		wcConf.Add("app_id", "xxxxxxxxxxxxxxxx");
		ShareSDK.setPlatformConfig(PlatformType.WeChatSession, wcConf);
		ShareSDK.setPlatformConfig(PlatformType.WeChatTimeline, wcConf);
		ShareSDK.setPlatformConfig(PlatformType.WeChatFav, wcConf);
		//QQ
		Hashtable qqConf = new Hashtable();
		qqConf.Add("app_id", "xxxxxxxxxxxx");
		ShareSDK.setPlatformConfig(PlatformType.QQ, qqConf);
	}

	// Update is called once per frame
	void OnDestroy()
	{
		ShareSDK.close();
	}

	public void ShowShareMenu()
	{
		Hashtable content = new Hashtable();
		content["content"] = "";
		//content["image"] = "http://img.baidu.com/img/image/zhenrenmeinv0207.jpg";


		string path = Application.dataPath + "/shareTemp.png";
	#if UNITY_EDITOR
		path = Application.dataPath + "/shareTemp.png";
	#elif UNITY_ANDROID || UNITY_IPHONE
		path =  Application.persistentDataPath + "/shareTemp.png";
	#endif
		
		content["image"] = path;
		content["title"] = "test title";
		content["description"] = "test description";
		content["url"] = "http://www.mob.com";
		content["type"] = Convert.ToString((int)ContentType.News);
		content["siteUrl"] = "http://www.mob.com";
		content["site"] = "ShareSDK";

		//开启,调用客户端授权 (only in android)
		content["disableSSOWhenAuthorize"] = false;
		content["shareTheme"] = "classic";//ShareTheme has only two value which are skyblue and classic

		ShareResultEvent evt = new ShareResultEvent(ShareResultHandler);
		ShareSDK.showShareMenu(null, content, 100, 100, MenuArrowDirection.Up, evt);
	}

	void ShareResultHandler(ResponseState state, PlatformType type, Hashtable shareInfo, Hashtable error, bool end)
	{
		print(" =============== ShareResultHandler");
		if (state == ResponseState.Success)
		{
			GameObject.Find("DebugInfo").GetComponent<Text>().text += "share result :" + MiniJSON.jsonEncode(shareInfo);
			print("share result :");
			print(MiniJSON.jsonEncode(shareInfo));
		}
		else if (state == ResponseState.Fail)
		{
			GameObject.Find("DebugInfo").GetComponent<Text>().text += ("---------fail! error code = " + error["error_code"] + "; error msg = " + error["error_msg"]);
			print("---------fail! error code = " + error["error_code"] + "; error msg = " + error["error_msg"]);
		}
		else if (state == ResponseState.Cancel)
		{
			GameObject.Find("DebugInfo").GetComponent<Text>().text += ("cancel !");
			print("cancel !");
		}
	}

	void AuthResultHandler(ResponseState state, PlatformType type, Hashtable error)
	{
		print(" =============== AuthResultHandler");
		if (state == ResponseState.Success)
		{
			print("success !");
		}
		else if (state == ResponseState.Fail)
		{
			print("fail! error code = " + error["error_code"] + "; error msg = " + error["error_msg"]);
		}
		else if (state == ResponseState.Cancel)
		{
			print("cancel !");
		}
	}

	void GetUserInfoResultHandler(ResponseState state, PlatformType type, Hashtable user, Hashtable error)
	{
		print(" =============== GetUserInfoResultHandler");
		if (state == ResponseState.Success)
		{
			print("get user result :");
			print(MiniJSON.jsonEncode(user));
		}
		else if (state == ResponseState.Fail)
		{
			print("fail! error code = " + error["error_code"] + "; error msg = " + error["error_msg"]);
		}
		else if (state == ResponseState.Cancel)
		{
			print("cancel !");
		}
	}

}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值