您的位置:首页 > 移动开发 > Unity3D

Unity项目工程集成Mob社会化分享SDK(android篇)

2015-08-06 12:53 531 查看
Unity项目工程集成Mob社会化分享SDK(Android篇)
  参考我的上一篇关于Unity项目利用Mob社会化分享ios平台前面几个步骤,将发布到Android平台需要的相关文件导入即可。Android目录文件及MiniJSON/和ShareSDK文件(如果工程里没有的话)



主要注意以下2份文件:
1、更换Demo.cs中ShareSDK.open("App项目Android版的Mob里的AppKey")
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using cn.sharesdk.unity3d;

public class Demo : MonoBehaviour {

public GUISkin demoSkin;
// Use this for initialization
void Start ()
{
ShareSDK.setCallbackObjectName("ShareSDK");
//appkey for your project(ios version)in Mob
ShareSDK.open ("xxxxxxxx");

//Sina Weibo
Hashtable sinaWeiboConf = new Hashtable();
sinaWeiboConf.Add("app_key", "xxxxxxxxx");
sinaWeiboConf.Add("app_secret", "623cb0668d6f6357bcda5db6e432b40f");
sinaWeiboConf.Add("redirect_uri", "http://cy.kyd2002.com/Picbook/default.aspx");
ShareSDK.setPlatformConfig (PlatformType.SinaWeibo, sinaWeiboConf);

//QZone
Hashtable qzConf = new Hashtable();
qzConf.Add("app_id", "xxxxxxxx");
qzConf.Add("app_key", "XjNXDmguk4KlySZE");
ShareSDK.setPlatformConfig (PlatformType.QZone, qzConf);

//WeChat
Hashtable wcConf = new Hashtable();
wcConf.Add("app_id", "wxxxxxxxx");
ShareSDK.setPlatformConfig (PlatformType.WeChatSession, wcConf);
ShareSDK.setPlatformConfig (PlatformType.WeChatTimeline, wcConf);
ShareSDK.setPlatformConfig (PlatformType.WeChatFav, wcConf);

//QQ
Hashtable qqConf = new Hashtable();
qqConf.Add("app_id", "XXXXXX");
ShareSDK.setPlatformConfig (PlatformType.QQ, qqConf);

}

// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape)) {
ShareSDK.close();
Application.Quit();
}
}

public void ShareFenX()
{
Hashtable content = new Hashtable();
content["content"] = "本应用以小刺猬的生日愿望 —— 制作生日蛋糕为故事导线,呈现了森林哺乳动物温馨友爱的场景,让孩子在丰富的互动体验中了解森林哺乳动物的特点。创意丰富的互动体验让孩子悦读悦美!http://cy.kyd2002.com/Picbook/default.aspx";
content["image"] = "http://cy.kyd2002.com/Picbook/icon.png";
content["title"] = "小刺猬的生日蛋糕";
content["description"] = "小朋友,一起来看看小刺猬这个特别的生日蛋糕吧!";
content["url"] = "http://cy.kyd2002.com/Picbook/default.aspx";
content["type"] = Convert.ToString((int)ContentType.News);
content["siteUrl"] = "http://cy.kyd2002.com/Picbook/default.aspx";
content["site"] = "互动绘本";
content["musicUrl"] = "";
ShareResultEvent evt = new ShareResultEvent(ShareResultHandler);
ShareSDK.showShareMenu(null, content, 100, 100, MenuArrowDirection.Up, evt);
//		ShareSDK.showShareView (PlatformType.Any, content, evt);
}

//void OnGUI ()
//{
//    GUI.skin = demoSkin;

//    float scale = 1.0f;

//    if (Application.platform == RuntimePlatform.IPhonePlayer)
//    {
//        scale = Screen.width / 320;
//    }

//    float btnWidth = 200 * scale;
//    float btnHeight = 45 * scale;
//    float btnTop = 20 * scale;
//    GUI.skin.button.fontSize = Convert.ToInt32(16 * scale);

//    if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "Authorize"))
//    {

//        AuthResultEvent evt = new AuthResultEvent(AuthResultHandler);
//        ShareSDK.authorize(PlatformType.SinaWeibo, evt);
//    }

//    btnTop += btnHeight + 20 * scale;
//    if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "Get User Info"))
//    {
//        GetUserInfoResultEvent evt = new GetUserInfoResultEvent(GetUserInfoResultHandler);
//        ShareSDK.getUserInfo(PlatformType.SinaWeibo, evt);
//    }

//    btnTop += btnHeight + 20 * scale;
//    if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "Show Share Menu"))
//    {
//        Hashtable content = new Hashtable();
//        content["content"] = "this is a test string.";
//        content["image"] = "http://img.baidu.com/img/image/zhenrenmeinv0207.jpg";
//        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";
//        content["musicUrl"] = "http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3";

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

//    btnTop += btnHeight + 20 * scale;
//    if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "Show Share View"))
//    {
//        Hashtable content = new Hashtable();
//        content["content"] = "this is a test string.";
//        content["image"] = "http://img.baidu.com/img/image/zhenrenmeinv0207.jpg";
//        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";
//        content["musicUrl"] = "http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3";

//        ShareSDK.customSinaWeiboShareContent(content, "sina weibo test string", InheritedValue.VALUE, null);

//        ShareResultEvent evt = new ShareResultEvent(ShareResultHandler);
//        ShareSDK.showShareView (PlatformType.Any, content, evt);
//    }

//    btnTop += btnHeight + 20 * scale;
//    if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "Share Content"))
//    {
//        Hashtable content = new Hashtable();
//        content["content"] = "this is a test string.";
//        content["image"] = "http://img.baidu.com/img/image/zhenrenmeinv0207.jpg";
//        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";
//        content["musicUrl"] = "http://mp3.mwap8.com/destdir/Music/2009/20090601/ZuiXuanMinZuFeng20090601119.mp3";

//        ShareResultEvent evt = new ShareResultEvent(ShareResultHandler);
//        ShareSDK.shareContent (PlatformType.SinaWeibo, content, evt);
//    }

//    btnTop += btnHeight + 20 * scale;
//    if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "Get Friends SinaWeibo "))
//    {
//        GetFriendsResultEvent evt = new GetFriendsResultEvent(GetFriendsResultHandler);
//        ShareSDK.getFriends (PlatformType.SinaWeibo, null, evt);
//    }

//    btnTop += btnHeight + 20 * scale;
//    if (GUI.Button(new Rect((Screen.width - btnWidth) / 2, btnTop, btnWidth, btnHeight), "Get Token SinaWeibo "))
//    {
//        GetCredentialResultEvent evt = new GetCredentialResultEvent(GetTokenResultHandler);
//        ShareSDK.getCredential (PlatformType.SinaWeibo, evt);
//    }

//}

void AuthResultHandler(ResponseState state, PlatformType type, Hashtable error)
{
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)
{
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 !");
}
}

void ShareResultHandler (ResponseState state, PlatformType type, Hashtable shareInfo, Hashtable error, bool end)
{
if (state == ResponseState.Success)
{
print ("share result :");
print (MiniJSON.jsonEncode(shareInfo));
}
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 GetFriendsResultHandler (ResponseState state, PlatformType type, ArrayList friends, Hashtable error)
{
if (state == ResponseState.Success)
{
print ("share result :");
print (MiniJSON.jsonEncode(friends));
}
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 GetTokenResultHandler (ResponseState state, PlatformType type, Hashtable credential, Hashtable error)
{
if (state == ResponseState.Success)
{
print ("share result :");
print (MiniJSON.jsonEncode(credential));
}
else if (state == ResponseState.Fail)
{
print ("fail! error code = " + error["error_code"] + "; error msg = " + error["error_msg"]);
}
else if (state == ResponseState.Cancel)
{
print ("cancel !");
}
}
}


2、AndroidManifest.xml文件中设置android:label与android:name="cn.sharesdk.unity3d.ShareSDKApplication"



3、发布打包apk时,需要进行发布设置



若不进行发布签名的话,可能会影响到微信分享!

Android相较于ios平台来说,稍微简单些!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity android 社会化 mob