纳金网

标题: UnityGUI的拓展 [打印本页]

作者: 奔跑的小兔    时间: 2012-2-15 13:33
标题: UnityGUI的拓展
UnityGUI的拓展
有很多方法可以让UnityGUI延伸和拓展来满足你的需求,控制端可以混合和创建,并且你有很多方式讲明白用户怎样对GUI进行处理。

Compound Controls 复合控制
当两种控制端总是一起出现的时候,复合控制很适合于你的GUI。例如,你创建一个满屏的文字,并且带有一些水平滑块,所有的滑块需要一个标签来识别它们,这样用户才能知道它们是用来调整的。在这种情况下,伴随每次调用GUI.Label()函数,你需要调用GUI.HorizontalSlider()函数,或者你创建一个复合控制,其中包括标签和滑块。
/* Label and Slider Compound Control */ var mySlider : float = 1.0; function OnGUI () {               mySlider = LabelSlider (Rect (10, 100, 100, 20), mySlider, 5.0, "Label text here"); function LabelSlider (screenRect : Rect, sliderValue : float, sliderMaxValue : float, labelText : String) : float {               GUI.Label (screenRect, labelText);               screenRect.x += screenRect.width; // <- Push the Slider to the end of the Label               sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0, sliderMaxValue);               return sliderValue;}
    在这个例子中,调用LabelSlider()函数并且通过调试正确,将会提供一个带有水平滑块的标签。当写一个复合控制的时候,你必须记得在函数的最后返回一个修正值确保它能实现交互。
上面的复合控制总是创建一对控制端

Static Compound Controls 静态复合控制
    通过使用静态复合控制,你能创建一整套自己的独立复合控制端,通过这种方法,在你需要用到同样脚本的时候,你不必在申明一次。
/* This script is called CompoundControls */ static function LabelSlider (screenRect : Rect, sliderValue : float, sliderMaxValue : float, labelText : String) : float {               GUI.Label (screenRect, labelText);               screenRect.x += screenRect.width; // <- Push the Slider to the end of the Label               sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0, sliderMaxValue);               return sliderValue;}
    通过在复合控制脚本中保存上诉例子,你可以在任何其它脚本中通过简单的输入CompoundControls.LabelSlider()调用LabelSlider()函数.

Elaborate Compound Controls  复杂复合控制
你可以利用复合控制变得更有创造力,它们可以按你喜欢的方式排列成组,下面这个例子是创建一个RGB滑块。
/* RGB Slider Compound Control */ var myColor : Color; function OnGUI () {               myColor = RGBSlider (Rect (10,10,200,10), myColor);} function RGBSlider (screenRect : Rect, rgb : Color) : Color {               rgb.r = GUI.HorizontalSlider (screenRect, rgb.r, 0.0, 1.0);               screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping               rgb.g = GUI.HorizontalSlider (screenRect, rgb.g, 0.0, 1.0);               screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping               rgb.b = GUI.HorizontalSlider (screenRect, rgb.b, 0.0, 1.0);               return rgb;}
RGB滑块是由上面的例子创建的

为了证明一个复合控制如何在其他符合控制中调用,现在让我们在它们上面创建一个复合控制。我们将要创建一个新的RGB滑块,就像上面的那个一样,但我们将要用标签滑块来做。在这种方法里我们总能有一个标签来告诉我们哪一个滑块对应于哪种颜色。
/* RGB Label Slider Compound Control */ var myColor : Color; function OnGUI () {               myColor = RGBLabelSlider (Rect (10,10,200,20), myColor);} function RGBLabelSlider (screenRect : Rect, rgb : Color) : Color {               rgb.r = CompoundControls.LabelSlider (screenRect, rgb.r, 1.0, "Red");               screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping               rgb.g = CompoundControls.LabelSlider (screenRect, rgb.g, 1.0, "Green");               screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping               rgb.b = CompoundControls.LabelSlider (screenRect, rgb.b, 1.0, "Blue");               return rgb;}
RGB复合控制标签滑块是由上面的示例代码创建的


转自unity3d8.com




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5