Extending UnityGUI 扩展UnityGUI

优质
小牛编辑
132浏览
2023-12-01

There are a number of ways to leverage and extend UnityGUI to meet your needs. Controls can be mixed and created, and you have a lot of leverage into dictating how user input into the GUI is processed.

有多种可以影响和扩展UnityGUI的方法可以满足你的需要。控件可以被混合或是被创建,同时你也可以影响进入GUI的用户输入如何被执行。

Compound Controls 混合控件

There might be situations in your GUI where two types of Controls always appear together. For example, maybe you are creating a Character Creation screen, with several Horizontal Sliders. All of those Sliders need a Label to identify them, so the player knows what they are adjusting. In this case, you could partner every call to GUI.Label() with a call to GUI.HorizontalSlider(), or you could create a Compound Control which includes both a Label and a Slider together.

当在你的控件中总是有两种类型的预设控件一起出现时,例如,你可以创建一个带有几个水平滑动条的角色创建窗口。这些滑动条都需要一个描述它功能的标签控件,这样使用者就能知道他们正在调整的是什么数据。为了达到这个目的,你可以将一个接一个次序的调用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;
}

In this example, calling LabelSlider() and passing the correct arguments will provide a Label paired with a Horizontal Slider. When writing Compound Controls, You have to remember to return the correct value at the end of the function to make it interactive.

在上述例子中,调用LabelSlider()并传递适当的参数来提供和滑动条成对的标签。当你写混合控件时,请注意在函数结束的位置返回恰当的值使控件得以和程序可以交互。


The above Compound Control always creates this pair of Controls
上述混合控件总是成对显示控件

Static Compound Controls 静态混合控件

By using Static functions, you can create an entire collection of your own Compound Controls that are self-contained. This way, you do not have to declare your function in the same script you want to use it.

通过使用静态函数,你可以定制一个属于你自己的自包含的混合控件的完整的系列。这样,你就不必在你想用的那个脚本文件里面声明你的函数了

/* 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;
}

By saving the above example in a script called CompoundControls, you can call the LabelSlider() function from any other script by simply typing CompoundControls.LabelSlider() and providing your arguments.

将上述代码保存在一个叫CompoundControls的脚本中,你能够从其他脚本中简单的使用CompoundControls.LabelSlider()调用LabelSlider()函数,并传递你的参数。

Elaborate Compound Controls 复杂的混合控件

You can get very creative with Compound Controls. They can be arranged and grouped in any way you like. The following example creates a re-usable RGB Slider.

你可以在混合控件上倾注很多的创意。它们可以以任何你希望的方式排列和组合。接下来的例子将定制一个可重用的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;
}


The RGB Slider created by the example above
上面示例创建的RGB滑动条

Now let's build Compound Controls on top of each other, in order to demonstrate how Compound Controls can be used within other Compound Controls. To do this, we will create a new RGB Slider like the one above, but we will use the LabelSlider to do so. This way we'll always have a Label telling us which slider corresponds to which color.

现在让我们在这个混合控件的基础上再定制一个混合控件,为了证明混合控件是如何被其他混合控件使用的,我们创建了一个新的RGB滑动器就像上面的例子一样。但我们使用已经定制的LabelSlider来完成新的定制。用这个方式,我们可以拥有一个标签来告诉用户每个滑动条所对应的颜色。

/* 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;
}


The Compound RGB Label Slider created by the above code
上述代码创建的混合RGB 标签滑动条

页面最后更新于: 2010-07-20