自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Unity3D教程:GUI的布局模式

開發(fā) 游戲開發(fā)
有兩種方法讓你去布置和組織你的GUI,一個是固定的,一個是活動的,所有GUI打頭的例子都是固定的,要想使用活動的Layout,就得用GUILayout代替GUI.它同樣也是寫在OnGUI()函數(shù)里。

固定Layout是用在你預先設計好界面的時候,活動Layout工作在你不知道會有多少組件或者如何定位他們的時候,這里有個例子:

  1. function OnGUI () { 
  2.    // 固定Layout 
  3.    GUI.Button (Rect (25,25,100,30), “I am a Fixed Layout Btton”); 
  4.    // 活動Layout 
  5.    GUILayout.Button (“I am an Automatic Layout Button”); 
  6.    } 

排列控制

在活動Layout模式里,你可以控制組件的位置,并組織他們。在固定模式下你可以把多個控件放進Groups中,在活動模式下可以把他們放在Areas, Horizontal Groups和Vertical Groups里。

固定Layout中的Groups

Groups 在固定Layout模式中起到組織可用項的,他讓你在屏幕的一個區(qū)域中包含多個控件。你要把定義的控件放在GUI.BeginGroup()和 GUI.EndGroup()這對函數(shù)中間,所有控件的位置坐標都以Groups的0坐標為起點,假如你更改了group坐標,那么內(nèi)部的控件也會跟隨改變。

舉個例子:

  1. // 在Group中有一個box和一個button.這里,兩個控件是屬于Group的. 
  2.    function OnGUI(){ 
  3.    GUI.BeginGroup (Rect (Screen.width / 2 , Screen.height / 2 − 50, 100, 100)); 
  4.    GUI.Box (Rect (0,0,100,100), “Group is here”); 
  5.    GUI.Button (Rect (10,40,80,30), “Click me”); 
  6.    GUI.EndGroup (); 
  7.    } 

Group也可以嵌套,例如:

  1. var bgImage : Texture2D; // background image that is 256 x 32 
  2.    var fgImage : Texture2D; // foreground image that is 256 x 32 
  3.    var playerEnergy = 1.0// a float between 0.0 and 1.0 
  4.    function OnGUI () { 
  5.    // Create one Group to contain both images 
  6.    // Adjust the first 2 coordinates to place it somewhere else on−screen 
  7.    GUI.BeginGroup (Rect (0,0,256,32)); 
  8.    // Draw the background image 
  9.    GUI.Box (Rect (0,0,256,32), bgImage); 
  10.    // Create a second Group which will be clipped 
  11.    // We want to clip the image and not scale it, which is why we need the second Group 
  12.    GUI.BeginGroup (Rect (0,0,playerEnergy * 25632)); 
  13.    // Draw the foreground image 
  14.    GUI.Box (Rect (0,0,256,32), fgImage); 
  15.    // End both Groups 
  16.    GUI.EndGroup (); 
  17.    GUI.EndGroup (); 
  18.    } 

和我們熟知的if-else語句一樣,相互最接近的begin和end為一組。

活動Layout-Areas

Areas只用于活動Layout模式.作用和固定模式下的Group一樣。

在活動模式下,你可以定義Area的范圍,也可以不定義。不定義的時候整個屏幕就是它的范圍,如果手工定義了它的范圍,那么它內(nèi)部的控件將以Area的坐標為起始坐標。Unity3D教程手冊

一個例子:

  1. function OnGUI () { 
  2.    GUILayout.Button (“I am not inside an Area”); 
  3.    GUILayout.BeginArea (Rect (Screen.width/2, Screen.height/2300300)); 
  4.    GUILayout.Button (“I am completely inside an Area”); 
  5.    GUILayout.EndArea (); 
  6.    } 

自動Layout - Horizontal and Vertical Groups

自動模式中還有還有兩對組函數(shù):GUILayout.BeginHoriztontal(),GUILayout.EndHorizontal(),GUILayout.BeginVertical(),和GUILayout.EndVertical().他們用在Area中,同樣也是成對使用的。

其特點是Horiztontal中的控件呈水平排列,Vertical中的控件呈垂直排列。

例子一個:

  1. var sliderValue = 1.0
  2.    var maxSliderValue = 10.0
  3.    function OnGUI() 
  4.    { 
  5.    // Wrap everything in the designated GUI Area 
  6.    GUILayout.BeginArea (Rect (20,0,200,60)); 
  7.    // Begin the singular Horizontal Group 
  8.    GUILayout.BeginHorizontal(); 
  9.    // Place a Button normally 
  10.    if (GUILayout.RepeatButton (“Increase max\nSlider Value”)) 
  11.    { 
  12.    maxSliderValue += 3.0 * Time.deltaTime; 
  13.    } 
  14.    // Arrange two more Controls vertically beside the Button 
  15.    // 這里面有兩個控件 是按照垂直的形式排列的 
  16.    GUILayout.BeginVertical(); 
  17.    GUILayout.Box(“Slider Value: ” + Mathf.Round(sliderValue)); 
  18.    sliderValue = GUILayout.HorizontalSlider (sliderValue, 0.0, maxSliderValue); 
  19.    // End the Groups and Area 
  20.    GUILayout.EndVertical(); 
  21.    GUILayout.EndHorizontal(); 
  22.    GUILayout.EndArea(); 
  23.    } 

用GUILayoutOptions去定義一些控件   比如可以控制按鈕的長度,如:

  1. function OnGUI () { 
  2.    GUILayout.BeginArea (Rect (10050, Screen.width−200, Screen.height−100)); 
  3.    GUILayout.Button (“I am a regular Automatic Layout Button”); 
  4.    GUILayout.Button (“My width has been overridden”, GUILayout.Width (95)); 
  5.    GUILayout.EndArea (); 
  6.    } 

這里GUILayout.Width (95)就定義了按鈕的長度為95。

原文鏈接:http://www.unitymanual.com/6064.html

責任編輯:彭凡 來源: Unity3D教程手冊
相關推薦

2013-04-25 09:56:24

unity3D手機游戲引擎

2013-04-25 10:03:07

unity3D手機游戲引擎

2013-04-25 13:27:11

unity3D手機游戲引擎

2013-06-18 08:49:15

2013-06-14 09:54:04

Unity3D

2013-06-17 09:12:31

Unity3D

2013-04-25 09:08:39

unity3D手機游戲引擎

2013-04-09 13:42:23

Unity3D基礎知識梳理

2013-06-25 09:16:10

Unity3D

2012-12-24 08:46:50

iOSUnity3D

2012-12-24 09:11:58

iOSUnity3D

2012-12-24 08:48:25

iOSUnity3D

2012-12-24 08:45:19

iOSUnity3D

2012-12-24 09:20:48

AndoidUnity3D

2015-01-06 09:55:16

Unity3D實時繪制

2013-04-25 10:40:32

unity3D手機游戲引擎

2013-04-25 00:06:06

unity3D手機游戲引擎

2012-12-24 09:09:27

AndoidUnity3D

2012-12-24 09:13:23

iOSUnity3D

2013-06-24 08:48:55

Unity3D
點贊
收藏

51CTO技術棧公眾號