为Unity开发的android手机游戏添加一个社会化分享功能

先看一下我最近自己做的一个游戏的效果图,然后在为大家讲述怎么做这样一个分享功能,如果图片不直观,当然如果你也不介意我顺便宣传一下我的游戏,你可以点击以下这个链接,下载我的游戏,进去体验一下里面的这个分享功能:?recrefer=SE_D_BubbleDeer

好了,接下来我们就来一起探讨一下这个分享功能怎么做。

(1)提供下载一个社会化分析的SDK,这个我用的是mob.com网的ShareSDK,贴上网址:

(2)解压下载下来的这个ShareSDK,我们直接进入主题,用Unity打开里面的Unity3dDemo这个项目

(3)

你会在打开的工程里面看到这些东西,很显然,我们现在要做的是android的分享功能,那么里面有一个IOS这个东西,大可以直接删除,或者你愿意直接保留,那当然也没影响。你直接点Assets,你其实可以看到里面是有Demo的,你也可以直接将它打包成一个apk安装到手机上试试效果,然后在回来继续学习接下来的东西;

(4)如果你体验得差不多了,咱们继续。那么接下来就是关键了,我们点击Plugins,鼠标右键然后Export Package…,将当前我们要用到的这个东西到处到一个我们可以找得到的位置,这里我直接导出到桌面,命名为ShareSDK。

(5)现在,如果你信任我,我们直接打开我们的项目,然后把这个导出来的东西直接导入到我们的工程里面(怎么导入,这个不要说不会吧,直接拖拽嘛)……,当然,你太信任我也不好吧,先新建个工程学习一下在说会比较合适一点的,嗯,那好我们新建一个工程我命名为MyShareTest,然后将ShareSDK这个你导出来的东西导到现在这个新工程里面吧!

(6)接着,我们在Assets下建一个Scenes文件夹,然后保存当前场景到Scenes里面,我命名为MyShareScene;然后,我们点击Plugins,在Plugins下面,你会看到有一个ShareSDK这个脚本,嗯,记住这个脚本,把它拉到相机上面,然后呢?然后我们现在不用管它了(那不拖呢,不拖咱们接下去做的都是无用功了,哈哈)。

(7)那么,现在更关键的步骤来了,其实刚刚我有件事情没有告诉大家,现在你点击Assets,你看到啥了……,没错,就是有一些文件跟着打包出来了,先别急着删除,看到里面有个Demo的脚本吧,(要是没有,到其他文件夹里找找吧),这个是很关键的一个脚本

(8)现在我们打开这个脚本,当然,你会在里面看到这么一堆堆东西,头大了没?先别激动哈,新建一个脚本,咱们叫做ShareButtonCtrl,然后把下面的代码复制过去:

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 initializationvoid Start (){print(Application.persistentDataPath);ShareSDK.setCallbackObjectName("Main Camera");ShareSDK.open ("api20");//Sina WeiboHashtable sinaWeiboConf = new Hashtable();sinaWeiboConf.Add("app_key", "568898243");sinaWeiboConf.Add("app_secret", "38a4f8204cc784f81f9f0daaf31e02e3");sinaWeiboConf.Add("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.SinaWeibo, sinaWeiboConf);//Tencent WeiboHashtable tcConf = new Hashtable();tcConf.Add("app_key", "801307650");tcConf.Add("app_secret", "ae36f4ee3946e1cbb98d6965b0b2ff5c");tcConf.Add("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.TencentWeibo, tcConf);//SMSShareSDK.setPlatformConfig (PlatformType.SMS, null);//QZoneHashtable qzConf = new Hashtable();qzConf.Add ("app_id", "100371282");qzConf.Add ("app_key", "aed9b0303e3ed1e27bae87c33761161d");ShareSDK.setPlatformConfig (PlatformType.QZone, qzConf);//WeChatHashtable wcConf = new Hashtable();wcConf.Add ("app_id", "wx4868b35061f87885");ShareSDK.setPlatformConfig (PlatformType.WeChatSession, wcConf);ShareSDK.setPlatformConfig (PlatformType.WeChatTimeline, wcConf);ShareSDK.setPlatformConfig (PlatformType.WeChatFav, wcConf);//QQHashtable qqConf = new Hashtable();qqConf.Add ("app_id", "100371282");ShareSDK.setPlatformConfig (PlatformType.QQ, qqConf);//FacebookHashtable fbConf = new Hashtable();fbConf.Add ("api_key", "107704292745179");fbConf.Add ("app_secret", "38053202e1a5fe26c80c753071f0b573");ShareSDK.setPlatformConfig (PlatformType.Facebook, fbConf);//TwitterHashtable twConf = new Hashtable();twConf.Add ("consumer_key", "mnTGqtXk0TYMXYTN7qUxg");twConf.Add ("consumer_secret", "ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc");twConf.Add ("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.Twitter, twConf);//Google+Hashtable gpConf = new Hashtable();gpConf.Add ("client_id", "232554794995.apps.googleusercontent.com");gpConf.Add ("client_secret", "PEdFgtrMw97aCvf0joQj7EMk");gpConf.Add ("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.GooglePlus, gpConf);//RenRenHashtable rrConf = new Hashtable();rrConf.Add ("app_id", "226427");rrConf.Add ("app_key", "fc5b8aed373c4c27a05b712acba0f8c3");rrConf.Add ("secret_key", "f29df781abdd4f49beca5a2194676ca4");ShareSDK.setPlatformConfig (PlatformType.Renren, rrConf);//KaiXinHashtable kxConf = new Hashtable();kxConf.Add ("api_key", "358443394194887cee81ff5890870c7c");kxConf.Add ("secret_key", "da32179d859c016169f66d90b6db2a23");kxConf.Add ("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.Kaixin, kxConf);//MailShareSDK.setPlatformConfig (PlatformType.Mail, null);//PrintShareSDK.setPlatformConfig (PlatformType.Print, null);//CopyShareSDK.setPlatformConfig (PlatformType.Copy, null);//DropboxHashtable dbConf = new Hashtable();dbConf.Add ("api_key", "02e2cbe5ca06de5908a863b15e149b0b");dbConf.Add ("secret", "9f1e7b4f71304f2f");dbConf.Add ("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.DouBan, dbConf);//EvernoteHashtable enConf = new Hashtable();enConf.Add ("consumer_key", "sharesdk-7807");enConf.Add ("consumer_secret", "d05bf86993836004");enConf.Add ("host_type", "0");ShareSDK.setPlatformConfig (PlatformType.Evernote, enConf);//LinkedInHashtable liConf = new Hashtable();liConf.Add ("api_key", "ejo5ibkye3vo");liConf.Add ("secret_key", "cC7B2jpxITqPLZ5M");liConf.Add ("redirect_url", "");ShareSDK.setPlatformConfig (PlatformType.LinkedIn, liConf);//PinterestHashtable pinConf = new Hashtable();pinConf.Add ("client_id", "1432928");ShareSDK.setPlatformConfig (PlatformType.Pinterest, pinConf);//PocketHashtable pocketConf = new Hashtable();pocketConf.Add ("consumer_key", "11496-de7c8c5eb25b2c9fcdc2b627");pocketConf.Add ("redirect_uri", "pocketapp1234");ShareSDK.setPlatformConfig (PlatformType.Pocket, pocketConf);//InstapaperHashtable ipConf = new Hashtable();ipConf.Add ("consumer_key", "4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA");ipConf.Add ("consumer_secret", "GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe");ShareSDK.setPlatformConfig (PlatformType.Instapaper, ipConf);//YouDaoNoteHashtable ydConf = new Hashtable();ydConf.Add ("consumer_key", "dcde25dca105bcc36884ed4534dab940");ydConf.Add ("consumer_secret", "d98217b4020e7f1874263795f44838fe");ydConf.Add ("oauth_callback", "");ydConf.Add ("host_type", "1");ShareSDK.setPlatformConfig (PlatformType.YouDaoNote, ydConf);//Sohu SuiShenKanHashtable shkConf = new Hashtable();shkConf.Add ("app_key", "e16680a815134504b746c86e08a19db0");shkConf.Add ("app_secret", "b8eec53707c3976efc91614dd16ef81c");shkConf.Add ("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.SohuKan, shkConf);//FlickrHashtable flickrConf = new Hashtable();flickrConf.Add ("api_key", "33d833ee6b6fca49943363282dd313dd");flickrConf.Add ("api_secret", "3a2c5b42a8fbb8bb");ShareSDK.setPlatformConfig (PlatformType.Flickr, flickrConf);//TumblrHashtable tumblrConf = new Hashtable();tumblrConf.Add ("consumer_key", "2QUXqO9fcgGdtGG1FcvML6ZunIQzAEL8xY6hIaxdJnDti2DYwM");tumblrConf.Add ("consumer_secret", "3Rt0sPFj7u2g39mEVB3IBpOzKnM3JnTtxX2bao2JKk4VV1gtNo");tumblrConf.Add ("callback_url", "");ShareSDK.setPlatformConfig (PlatformType.Tumblr, tumblrConf);//DropboxHashtable dropboxConf = new Hashtable();dropboxConf.Add ("app_key", "7janx53ilz11gbs");dropboxConf.Add ("app_secret", "c1hpx5fz6tzkm32");ShareSDK.setPlatformConfig (PlatformType.Dropbox, dropboxConf);//InstagramHashtable instagramConf = new Hashtable();instagramConf.Add ("client_id", "ff68e3216b4f4f989121aa1c2962d058");instagramConf.Add ("client_secret", "1b2e82f110264869b3505c3fe34e31a1");instagramConf.Add ("redirect_uri", "");ShareSDK.setPlatformConfig (PlatformType.Instagram, instagramConf);//VKHashtable vkConf = new Hashtable();vkConf.Add ("application_id", "3921561");vkConf.Add ("secret_key", "6Qf883ukLDyz4OBepYF1");ShareSDK.setPlatformConfig (PlatformType.VKontakte, vkConf);}// Update is called once per framevoid Update () {if (Input.GetKeyDown(KeyCode.Escape)) {ShareSDK.close();Application.Quit();}}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"] = "";content["title"] = "test title";content["description"] = "test description";content["url"] = "";content["type"] = Convert.ToString((int)ContentType.News);content["siteUrl"] = "";content["site"] = "ShareSDK";content["musicUrl"] = "";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"] = "";content["title"] = "test title";content["description"] = "test description";content["url"] = "";content["type"] = Convert.ToString((int)ContentType.News);content["siteUrl"] = "";content["site"] = "ShareSDK";content["musicUrl"] = "";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"] = "";content["title"] = "test title";content["description"] = "test description";content["url"] = "";content["type"] = Convert.ToString((int)ContentType.News);content["siteUrl"] = "";content["site"] = "ShareSDK";content["musicUrl"] = "";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 !");}}}(9)之前有木有体验将Demo打包成apk安装到手机上啊?如果有,相信你已经体验到各种分享功能了,那么,这些功能怎么实现的呢?看代码多不要激动,先大概看一下,也许你会发现OnGUI()这个类里面的东西好像写的就是你体验的时候点击的那些按钮,那么,要改成咱们要用的关键代码,就是在这里面了。转动心中的期待,血在澎湃,吃苦流汗算什么。

为Unity开发的android手机游戏添加一个社会化分享功能

相关文章:

你感兴趣的文章:

标签云: