Unity3D教程:GUI的布局模式

固定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 * 256, 32)); 
  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/2, 300, 300)); 
  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 (100, 50, 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教程:GUI的布局模式
文章网址:http://www.hantingmc.com/qtweb/news17/237717.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联