爲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的腳本吧,(要是沒有,到其他文件夾裏找找吧),這個是很關鍵的一個腳本

(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,然後參照着改一部分,那麼。。。我講的差不多結束了,最好呢,提醒你一下,哈哈,不要一參照,把什麼都給改了哈,至於怎麼改,你好好研究一番吧吐舌頭


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章