GUI的布局模式

来源:互联网 发布:温州商学院淘宝班 编辑:程序博客网 时间:2024/05/02 02:12

固定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模式中起到组织可用项的,他让你在屏幕的一个区域中包含多个控件。你要把定义的控件放在GUI.BeginGroup()和 GUI.EndGroup()这对函数中间,所有控件的位置坐标都以Groups的0坐标为起点,假如你更改了group坐标,那么内部的控件也会跟随改变。

举个例子:

  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的范围,也可以不定义。不定义的时候整个屏幕就是它的范围,如果手工定义了它的范围,那么它内部的控件将以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

自动模式中还有还有两对组函数: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.  }