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

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

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

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

(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的脚本吧,(要是没有,到其他文件夹里找找吧),这个是很关键的一个脚本

扫描二维码关注公众号,回复: 3793394 查看本文章

(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 initialization
	void Start ()
	{
        print(Application.persistentDataPath);

		ShareSDK.setCallbackObjectName("Main Camera");
		ShareSDK.open ("api20");
		
		//Sina Weibo
		Hashtable sinaWeiboConf = new Hashtable();
		sinaWeiboConf.Add("app_key", "568898243");
		sinaWeiboConf.Add("app_secret", "38a4f8204cc784f81f9f0daaf31e02e3");
		sinaWeiboConf.Add("redirect_uri", "http://www.sharesdk.cn");
		ShareSDK.setPlatformConfig (PlatformType.SinaWeibo, sinaWeiboConf);
		
		//Tencent Weibo
		Hashtable tcConf = new Hashtable();
		tcConf.Add("app_key", "801307650");
		tcConf.Add("app_secret", "ae36f4ee3946e1cbb98d6965b0b2ff5c");
		tcConf.Add("redirect_uri", "http://www.sharesdk.cn");
		ShareSDK.setPlatformConfig (PlatformType.TencentWeibo, tcConf);
		
		//SMS
		ShareSDK.setPlatformConfig (PlatformType.SMS, null);
		
		//QZone
		Hashtable qzConf = new Hashtable();
		qzConf.Add ("app_id", "100371282");
		qzConf.Add ("app_key", "aed9b0303e3ed1e27bae87c33761161d");
		ShareSDK.setPlatformConfig (PlatformType.QZone, qzConf);
		
		//WeChat
		Hashtable wcConf = new Hashtable();
		wcConf.Add ("app_id", "wx4868b35061f87885");
		ShareSDK.setPlatformConfig (PlatformType.WeChatSession, wcConf);
		ShareSDK.setPlatformConfig (PlatformType.WeChatTimeline, wcConf);
		ShareSDK.setPlatformConfig (PlatformType.WeChatFav, wcConf);
    	
		//QQ
		Hashtable qqConf = new Hashtable();
		qqConf.Add ("app_id", "100371282");
		ShareSDK.setPlatformConfig (PlatformType.QQ, qqConf);
    	
		//Facebook
		Hashtable fbConf = new Hashtable();
		fbConf.Add ("api_key", "107704292745179");
		fbConf.Add ("app_secret", "38053202e1a5fe26c80c753071f0b573");
		ShareSDK.setPlatformConfig (PlatformType.Facebook, fbConf);
    
		//Twitter
		Hashtable twConf = new Hashtable();
		twConf.Add ("consumer_key", "mnTGqtXk0TYMXYTN7qUxg");
		twConf.Add ("consumer_secret", "ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc");
		twConf.Add ("redirect_uri", "http://www.sharesdk.cn");
		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", "http://localhost");
		ShareSDK.setPlatformConfig (PlatformType.GooglePlus, gpConf);
    	
		//RenRen
		Hashtable rrConf = new Hashtable();
		rrConf.Add ("app_id", "226427");
		rrConf.Add ("app_key", "fc5b8aed373c4c27a05b712acba0f8c3");
		rrConf.Add ("secret_key", "f29df781abdd4f49beca5a2194676ca4");
		ShareSDK.setPlatformConfig (PlatformType.Renren, rrConf);
		
		//KaiXin
		Hashtable kxConf = new Hashtable();
		kxConf.Add ("api_key", "358443394194887cee81ff5890870c7c");
		kxConf.Add ("secret_key", "da32179d859c016169f66d90b6db2a23");
		kxConf.Add ("redirect_uri", "http://www.sharesdk.cn/");
		ShareSDK.setPlatformConfig (PlatformType.Kaixin, kxConf);
    	
		//Mail
		ShareSDK.setPlatformConfig (PlatformType.Mail, null);
		
		//Print
		ShareSDK.setPlatformConfig (PlatformType.Print, null);
		
		//Copy
		ShareSDK.setPlatformConfig (PlatformType.Copy, null);
    	
		//Dropbox
		Hashtable dbConf = new Hashtable();
		dbConf.Add ("api_key", "02e2cbe5ca06de5908a863b15e149b0b");
		dbConf.Add ("secret", "9f1e7b4f71304f2f");
		dbConf.Add ("redirect_uri", "http://www.sharesdk.cn");
		ShareSDK.setPlatformConfig (PlatformType.DouBan, dbConf);
		
		//Evernote
		Hashtable enConf = new Hashtable();
		enConf.Add ("consumer_key", "sharesdk-7807");
		enConf.Add ("consumer_secret", "d05bf86993836004");
		enConf.Add ("host_type", "0");
		ShareSDK.setPlatformConfig (PlatformType.Evernote, enConf);
		
		//LinkedIn
		Hashtable liConf = new Hashtable();
		liConf.Add ("api_key", "ejo5ibkye3vo");
		liConf.Add ("secret_key", "cC7B2jpxITqPLZ5M");
		liConf.Add ("redirect_url", "http://sharesdk.cn");
		ShareSDK.setPlatformConfig (PlatformType.LinkedIn, liConf);
		
		//Pinterest
		Hashtable pinConf = new Hashtable();
		pinConf.Add ("client_id", "1432928");
		ShareSDK.setPlatformConfig (PlatformType.Pinterest, pinConf);
		
		//Pocket
		Hashtable pocketConf = new Hashtable();
		pocketConf.Add ("consumer_key", "11496-de7c8c5eb25b2c9fcdc2b627");
		pocketConf.Add ("redirect_uri", "pocketapp1234");
		ShareSDK.setPlatformConfig (PlatformType.Pocket, pocketConf);
		
		//Instapaper
		Hashtable ipConf = new Hashtable();
		ipConf.Add ("consumer_key", "4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA");
		ipConf.Add ("consumer_secret", "GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe");
		ShareSDK.setPlatformConfig (PlatformType.Instapaper, ipConf);
		
		//YouDaoNote
		Hashtable ydConf = new Hashtable();
		ydConf.Add ("consumer_key", "dcde25dca105bcc36884ed4534dab940");
		ydConf.Add ("consumer_secret", "d98217b4020e7f1874263795f44838fe");
		ydConf.Add ("oauth_callback", "http://www.sharesdk.cn/");
		ydConf.Add ("host_type", "1");
		ShareSDK.setPlatformConfig (PlatformType.YouDaoNote, ydConf);
    	
		//Sohu SuiShenKan
		Hashtable shkConf = new Hashtable();
		shkConf.Add ("app_key", "e16680a815134504b746c86e08a19db0");
		shkConf.Add ("app_secret", "b8eec53707c3976efc91614dd16ef81c");
		shkConf.Add ("redirect_uri", "http://sharesdk.cn");
		ShareSDK.setPlatformConfig (PlatformType.SohuKan, shkConf);
    
		//Flickr
		Hashtable flickrConf = new Hashtable();
		flickrConf.Add ("api_key", "33d833ee6b6fca49943363282dd313dd");
		flickrConf.Add ("api_secret", "3a2c5b42a8fbb8bb");
		ShareSDK.setPlatformConfig (PlatformType.Flickr, flickrConf);

		//Tumblr
		Hashtable tumblrConf = new Hashtable();
		tumblrConf.Add ("consumer_key", "2QUXqO9fcgGdtGG1FcvML6ZunIQzAEL8xY6hIaxdJnDti2DYwM");
		tumblrConf.Add ("consumer_secret", "3Rt0sPFj7u2g39mEVB3IBpOzKnM3JnTtxX2bao2JKk4VV1gtNo");
		tumblrConf.Add ("callback_url", "http://sharesdk.cn");
		ShareSDK.setPlatformConfig (PlatformType.Tumblr, tumblrConf);
    	
		//Dropbox
		Hashtable dropboxConf = new Hashtable();
		dropboxConf.Add ("app_key", "7janx53ilz11gbs");
		dropboxConf.Add ("app_secret", "c1hpx5fz6tzkm32");
		ShareSDK.setPlatformConfig (PlatformType.Dropbox, dropboxConf);
    	
		//Instagram
		Hashtable instagramConf = new Hashtable();
		instagramConf.Add ("client_id", "ff68e3216b4f4f989121aa1c2962d058");
		instagramConf.Add ("client_secret", "1b2e82f110264869b3505c3fe34e31a1");
		instagramConf.Add ("redirect_uri", "http://sharesdk.cn");
		ShareSDK.setPlatformConfig (PlatformType.Instagram, instagramConf);
		
		//VK
		Hashtable vkConf = new Hashtable();
		vkConf.Add ("application_id", "3921561");
		vkConf.Add ("secret_key", "6Qf883ukLDyz4OBepYF1");
		ShareSDK.setPlatformConfig (PlatformType.VKontakte, vkConf);
	}
	
	// Update is called once per frame
	void 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"] = "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 !");
		}
	}
}

(9)之前有木有体验将Demo打包成apk安装到手机上啊?如果有,相信你已经体验到各种分享功能了,那么,这些功能怎么实现的呢?看代码多不要激动,先大概看一下,也许你会发现OnGUI()这个类里面的东西好像写的就是你体验的时候点击的那些按钮,那么,要改成咱们要用的关键代码,就是在这里面了。

(10)还记得前两张图片吗,我实现的是打开一个菜单栏用于选择要分享的位置,然后进入分享。嗯,那现在我讲我使用的,估计其它东西大家也就跟着懂了,在OnGUI()里面找到

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);
		}

这几行代码实现的就是类似于我那个点击分享时弹出的菜单了,那么,要更改你分享的内容,当然就是里面你看到的content["content"]之类的这些赋值了,当然,中括号里面的这些内容的名字是不能改的,你能改的就是后面“=”号后面的东西了。以上是拷贝Demo的,那么,看一下我自己的游戏里面改过的东西吧:

// 分享内容填充
	public void ShareInfomation(){
		Hashtable content = new Hashtable();

		Application.CaptureScreenshot ("screen.png");// 截屏分享
		content["image"] = Application.persistentDataPath + "/screen.png";// 截屏存放的位置
		content["content"] = "小鹿鹿带着泡泡来了,逗比呆萌,只有你想不到,没有它做不到,你相信吗?一起来玩吧!";
		content["title"] = "《Bubble Deer》";
		content["url"] = "http://zhushou.360.cn/detail/index/soft_id/2954399?recrefer=SE_D_BubbleDeer";
		content["type"] = Convert.ToString((int)ContentType.News);
		content["siteUrl"] = "http://zhushou.360.cn/detail/index/soft_id/2954399?recrefer=SE_D_BubbleDeer";
		content["site"] = "《BubbleDeer》";
		
		ShareResultEvent evt = new ShareResultEvent(ShareResultHandler);
		ShareSDK.showShareMenu (null, content, 100, 100, MenuArrowDirection.Up, evt);
	}

没错,我把这个要用的东西弄成一个方法了,然后OnGUI()被我整个删了,为什么呢?因为我只需要用它这一个功能,然后呢,我想通过UGUI的一个按钮调用这个方法实现分享功能。

(11)那么,现在你们改完代码没?嗯,改完就继续接下来的步骤,创建一个UGUI的Button:

(12)把脚本拉给Button:然后在点击Button下的Button组件底下的+号,把按钮拉进去,添加点击事件,即找到方法ShareInfomation(这个是我自己上面那个方法)添加进去:


现在你编译一下保存一下,将这个打包成apk装到手机或者直接连接手机调试,看看什么情况;我相信按照我步骤走的,都差不多成功了,正常显示出来了,那么我们这下算成功了吗????非也非也……

(13)也许你按照我的步骤走完,确实在Demo阶段实现了你的分享功能,但是,请你记住,这个仅仅是一小步。现在你准备好将这个功能加如到你的项目里面了吗?准备好了就先备份一份你自己项目的工程吧,然后我们在继续,防止你在跟着我做的时候,把自己工程给毁坏掉了(虽然按着我的步骤走不会错,但是不能确保你走错路了,重新回过头,发现工程毁了)

(14)好,现在我告诉大家为什么说只是完成一小步了,首先不知道你的工程是不是完整的只缺分享功能,如果是完整的,那么我相信安装到手机的图标也弄好了,游戏名字也设置好了,又或者你的Unity是Pro版的,你的开始屏幕突变已经改成不是Unity官方的了等等……。如果是完整的,那么你会发现我接下来要说的问题,如果你的项目还不是完整的,那么也请你接下去看我要说的问题,帮助你减少开发带来莫名奇妙的问题。

(15)你有没有发现,你自从加了分享功能之后,你安装到手机上的APK的游戏图标安卓小机器人了,点击进去之后,游戏名字也不是你之前那个了,第一个图片也不听话的跑位了,等等一大堆问题,其实,据我所知,可怜的娃,连包名都被改了还有等等一大堆问题,那么这下闹热了。

(16)不要激动,不要悲伤,其实问题不大,也就是一个文件出被改了……,学过android的人都知道,android应用设置包名横屏图标等等都是在AndroidManifest.xml里面设置的。那么现在我们就来玩一下这个东西。点击你工程里面的Plugins下面的Android,然后你会发现,该文件夹下面有一个文件,名字就叫做AndroidManifest,没错,就是它搞的鬼,它把你编译成android的apk的时候产生的AndroidManifest文件给改了,那么,接下来我教大家两种方法,如果要求不是很苛刻的,可以使用第一种方法,要求高的,研究一下第二种方法。

方法一:打开我刚刚说的那个AndroidManifest文件(用mono或者vs啥的,只要能打开,能更改就行),

(1)找到:package="cn.sharesdk.onekeyshare",这个是设置包名的,你可以直接更改,比如我的游戏,我改成package="com.WMDC.BubbleDeer",包名问题就解决了。

(2)找到以下几行代码,我只复制里面的部分代码:

 <application
        android:allowBackup="true"
        android:label="ShareSDK" 
        android:name="cn.sharesdk.unity3d.ShareSDKApplication">
        <activity
            android:name="cn.sharesdk.unity3d.demo.MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:windowSoftInputMode="adjustPan|stateHidden" >

这个里面要改啥呢,看见没有,那里有个 android:label="ShareSDK" ,这个就是设置你的游戏名字的,改成你的游戏名字吧。然后我贴上我自己改完的,大家对照看看有什么不一样:

<application
        android:allowBackup="true"
        android:label="BubbleDeer" 
        android:name="cn.sharesdk.unity3d.ShareSDKApplication" 
        android:icon="@drawable/app_icon">
        <activity
            android:name="cn.sharesdk.unity3d.demo.MainActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
            android:windowSoftInputMode="adjustPan|stateHidden"
            android:label="@string/app_name"
            android:launchMode="singleTask"
             >


嗯哼?有没有发现啥?里面跟你的不一样吧?嗯,这就是接下来要说的。

(3)其中的android:icon="@drawable/app_icon",这里就是设置一个图标,使用的同学跟我一样写就行了,那为什么要这样设置呢?其实,在每次我们打包apk的时候,我们的Unity不仅仅是会创建AndroidManifest这个东西的,它还会把我们的图集等等一些东西,弄成跟你编写一个android工程一样的有各种各样的文件,然后上面android:icon="@drawable/app_icon"这句话,其实就是从drawable的文件夹下去取app_icon这个图片,而这个图片,是你在Unity打包apk前在Unity编辑器里面设置的那个图标。

(4)有没有发现这句话:

android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"

比之前那个里面的长了好多,这句话也是挺关键的一句更改,这个是用来配置你的游戏在运行过程中设备的一些状态的改变,如果不写,嘿嘿,你不知道啥时候会遇到问题,要尝试你试试去,不尝试我也没办法一个个告诉你,有部分涉及到android知识,我一年前用android,现在快忘光了,不好瞎说。好了,不废话哈,你可以直接复制过去使用就是了。

(5)android:launchMode="singleTask"这个是各个界面显示栈的问题,不懂也没事,添加就好,想懂的直接百度查找一下吧。

好了,说了这么多,想知道这样改的理由,看一下方法二。

方法二:在讲方法一的时候,我有提过,就是将一个项目打包成apk的过程,Unity会自动为你产生一个AndroidManifest.xml文件的,那么我们要防止这个xml文件里面的东西被更改的问题,当然就是拿一个正常的去参照着更改了,现在,我们备份的那个项目起效果了(大笑没备份的孩子……你怎么办……)。用Unity打开你原来的正常项目,然后呢,点击Build Setting…,选择Android,在小窗右侧里面显示出来的第一项打勾,然后点击右下角的Export,导出一个android工程,在android里面找到AndroidManifest.xml文件,然后复制到桌面打开它,然后呢,进入到你已经做分享功能的工程,打开那个原来的AndroidManifest.xml,然后参照着改一部分,那么。。。我讲的差不多结束了,最好呢,提醒你一下,哈哈,不要一参照,把什么都给改了哈,至于怎么改,你好好研究一番吧吐舌头


猜你喜欢

转载自blog.csdn.net/a3636987/article/details/45533063