溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[unity3d]水果忍者-界面搭建

發(fā)布時間:2020-07-22 07:42:45 來源:網(wǎng)絡(luò) 閱讀:621 作者:蓬萊仙羽 欄目:游戲開發(fā)

今天開始用Unity3D做一下水果忍者的游戲,Keep study very day!

效果圖:                                                                                                                     


[unity3d]水果忍者-界面搭建

[unity3d]水果忍者-界面搭建

實現(xiàn)步驟:                                                                                                                  

1.貼圖的創(chuàng)建

,[unity3d]水果忍者-界面搭建

這里的Pixel Inset中X,Y,Width,Height是貼圖的位置以及寬高屬性,是只讀的,可恨的只讀,不然我就可以在后臺代碼更改貼圖的寬高來自適應(yīng)分辨率了,就是因為這不可修改,讓我苦惱的沒辦法做分辨率的自適應(yīng),如果我是在OnGUI方法來時時繪制GUITexture,但我又覺得太耗資源,我寫不了那樣的代碼。我具有強迫癥,那種性能不好的代碼不忍下手!??!
關(guān)于錨點問題:這里的X,Y是以圖片的左下角為基準(zhǔn)點,由于我之前是搞cocos2dx,對精靈的錨點等屬性熟悉,但后來搞懂Unity里面圖片的“錨點”默認(rèn)是左下角的(0,0)點,并且貌似不可修改,cocos2dx默認(rèn)是中心點(0.5,0.5),所以這里的X,Y就都設(shè)置為0,圖片的左下角就跟屏幕的左下角對其,然后寬高設(shè)置為屏幕的分辨率,我的手機分辨率是800*480,所以我就設(shè)置了這么大。

2.聲音的添加

創(chuàng)建一個空物體,點擊菜單欄GameObject->Create Empty,命名為audio,然后給他添加一個Audio Source組件,Component->Add->Audio Source,然后設(shè)置AudioClip為背景聲音,并且設(shè)置Loop循環(huán)播放和Play On Awake程序啟動時自動播放。
[unity3d]水果忍者-界面搭建
   
關(guān)于腳本控制聲音的開關(guān),下面介紹。

3.手動“繪制”按鈕

其他的菜單按鈕,我們在GUI里面“繪制”,GUI方法是每幀都調(diào)用的方法,用于在屏幕上繪制東西,之所以說GUI效率沒有NGUI好就是因為它每幀都不停的繪制,NGUI可能對這方面做了優(yōu)化。所以現(xiàn)在界面的制作普遍都是傾向于用NGUI,并且后者對自適應(yīng)做的非常好。
NGUI的自適應(yīng)非常簡單:
[unity3d]水果忍者-界面搭建
這里我還延續(xù)了之前cocos2dx的思想,先獲取屏幕的寬高,然后計算按鈕繪制的位置,這樣就能適應(yīng)不同的分辨率。
這里我就直接貼代碼了,代碼比較簡單,通俗易懂!
using UnityEngine; using System.Collections;  public class start : MonoBehaviour {       public GUISkin mySkin;      private bool isSound1Button = false; 	private bool isSound2Button = true;     private AudioSource sound; 	 	 	private float screenwitdh; 	private float screenheight; 	 	public GUITexture background; 	public GUITexture title; 	 	void Awake() 	{ //		screenwitdh = Screen.width; //		screenheight = Screen.height; 		 		//想改變背景的分辨率 //		background.guiTexture.pixelInset.x = 0; //		background.guiTexture.pixelInset.y = 0; //		background.guiTexture.pixelInset.width = screenwitdh; //		background.guiTexture.pixelInset.height = screenheight; 	}  	// Use this for initialization 	void Start ()  	{ 		sound = gameObject.GetComponent<AudioSource>(); 	} 	     void OnGUI()     { 		 		screenwitdh = Screen.width; 		screenheight = Screen.height; 		         GUI.skin = mySkin;  		//這里的GUI坐標(biāo)是以左上角為原點跟GUITexture的坐標(biāo)不同,GUITexture屬性中是OpenGL坐標(biāo)是從左下方開始為原點         if (GUI.Button(new Rect(screenwitdh/2-110, screenheight -280, 220, 66), "", GUI.skin.GetStyle("playbutton")))         {             Application.LoadLevel(1);         } 		if(GUI.Button (new Rect(screenwitdh/2-160,screenheight -200,320,66),"",GUI.skin.GetStyle("MoreButton"))) 		{ 			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013");	 		} 		if(GUI.Button(new Rect(screenwitdh/2-100,screenheight -120,200,66),"",GUI.skin.GetStyle("CreditButton"))) 		{ 			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013"); 		} 		 		if(isSound1Button) 		{ 			if(GUI.Button(new Rect(45,screenheight-45,20,31),"",GUI.skin.GetStyle("Sound1Button"))) 			{ 				sound.Play(); 				isSound1Button = false; 				isSound2Button = true; 			} 		} 		 		if(isSound2Button) 		{ 			if(GUI.Button(new Rect(45,screenheight-45,37,31),"",GUI.skin.GetStyle("Sound2Button"))) 			{ 				sound.Stop(); 				isSound1Button = true; 				isSound2Button = false; 			} 		}     } } 

不早了,早點去睡覺,后續(xù)的后面繼續(xù)補上!

==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互學(xué)習(xí),共同進步 ===================

 

轉(zhuǎn)載請注明出處:http://blog.csdn.net/dingxiaowei2013/article/details/18376397

歡迎關(guān)注我的微博:http://weibo.com/u/2590571922

需要工程文件的請留言!


今天將昨天的版本做了一些自適應(yīng)的修改:

GUITexture的屏幕自適應(yīng):

screenwitdh = Screen.currentResolution.width; screenheight = Screen.currentResolution.height; //改變背景的分辨率 background.transform.position = Vector3.zero; background.transform.localScale = Vector3.zero; background.guiTexture.pixelInset = new Rect(0, 0, screenwitdh, screenheight); print("height" + screenheight); print("width" + screenwitdh);

經(jīng)過反復(fù)的編譯apk,安裝到手機,經(jīng)過十幾次的反復(fù)終于嘗試出稍微完美的自適應(yīng),下面是完美的代碼:

using UnityEngine; using System.Collections;  public class start : MonoBehaviour {       public GUISkin mySkin;      private bool isSound1Button = false; 	private bool isSound2Button = true;     private AudioSource sound; 	 	 	private float screenwitdh; 	private float screenheight; 	 	public GUITexture background; 	public GUITexture title; 	 	void Awake() 	{         screenwitdh = Screen.currentResolution.width;         screenheight = Screen.currentResolution.height;         //改變背景的分辨率         background.transform.position = Vector3.zero;         background.transform.localScale = Vector3.zero;         background.guiTexture.pixelInset = new Rect(0, 0, screenwitdh, screenheight);           //title.transform.position = Vector3.zero;         //title.transform.localScale = Vector3.zero;         //坐標(biāo)是以左下角為原點開始的,錨點也是左下角         float width = 400;         float height = 200;         title.guiTexture.pixelInset = new Rect(screenwitdh/2-width/2, screenheight-30-height, width, height);          print("height" + screenheight);         print("width:" + screenwitdh); 	}  	// Use this for initialization 	void Start ()  	{ 		sound = gameObject.GetComponent<AudioSource>(); 	} 	     void OnGUI()     {         //screenwitdh = Screen.width;         //screenheight = Screen.height;         GUI.Label((new Rect(10, 10, 100, 20)), "作者:丁小未");         GUI.Label((new Rect(10, 30, 100, 20)), "height:"+screenheight.ToString());         GUI.Label((new Rect(10, 50, 100, 20)), "widht:"+screenwitdh.ToString());  		         GUI.skin = mySkin;  		//這里的GUI坐標(biāo)是以左上角為原點跟GUITexture的坐標(biāo)不同,GUITexture屬性中是OpenGL坐標(biāo)是從左下方開始為原點         if (GUI.Button(new Rect(screenwitdh/2-110, screenheight -250, 220, 66), "", GUI.skin.GetStyle("playbutton")))         {             Application.LoadLevel(1);         } 		if(GUI.Button (new Rect(screenwitdh/2-160,screenheight -185,320,66),"",GUI.skin.GetStyle("MoreButton"))) 		{ 			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013");	 		} 		if(GUI.Button(new Rect(screenwitdh/2-100,screenheight -120,200,66),"",GUI.skin.GetStyle("CreditButton"))) 		{ 			Application.OpenURL("http://blog.csdn.net/dingxiaowei2013"); 		} 		 		if(isSound1Button) 		{ 			if(GUI.Button(new Rect(60,screenheight-60,30,41),"",GUI.skin.GetStyle("Sound1Button"))) 			{ 				sound.Play(); 				isSound1Button = false; 				isSound2Button = true; 			} 		} 		 		if(isSound2Button) 		{ 			if(GUI.Button(new Rect(60,screenheight-60,45,41),"",GUI.skin.GetStyle("Sound2Button"))) 			{ 				sound.Stop(); 				isSound1Button = true; 				isSound2Button = false; 			} 		}     } } 


效果圖

[unity3d]水果忍者-界面搭建
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI