Unity3D GUI类接口

文章转载自:http://www.d3dweb.com/Documents/201203/12-8472627152.html

=====================================================================

GUI 类

GUI类是Unity GUI的手工定位接口 参见:GUI tutorial

类变量

◆ static var backgroundColor : Color // 描述:全局的修改由GUI渲染的所有元素的背景颜色。 这个获取多个color。 参见:contentColor, color

◆ static var changed : bool // 描述:有任何控件的输入数据改变吗?

◆ static var color : Color // 描述:全局的修改GUI颜色。 这将影响背景和文本颜色。 参见:backgroundColor, contentColor

◆ static var contentColor : Color // 描述:修改由GUI渲染的所有文本的颜色。 这个获取多个color。 参见:backgroundColor : Color

◆ static var depth : int // 描述:当前执行的GUI行为的排序深度 当你有不同的脚本同时运行时。设置这个来决定顺序。

◆ static var enabled : bool // 描述:GUI启用了? 设置这个值为假将禁用所有GUI交互。所有的控件将以半透明方式绘制。并将不响应用户输入。


var allOptions = true; // 这个值跟踪扩展的选项是否可以被打开。

var extended1 = true;
var extended2 = true; // 两个扩展选项

function OnGUI()
{

allOptions = GUI.Toogle(Rect(0, 0, 150, 20), allOptions, “Edit All Options”); //制作一个开关控件以便允许用户编辑扩展的选项,
GUIenabled = allOptions //将它的值赋给GUIEnabled – 如果上面的复选框被禁用

extended1 = GUI.Toogle(Rect(20, 20, 130, 20), extended1, “Extended Option 1”); //这两个控件只在上面的按钮为On时启用。
extended2 = GUI.Toogle(Rect(20, 40, 130, 30), extended2, “Extended Option 2”);

GUI.enabled = true;

if(GUI.Button(Rect(0, 60, 150, 20), “OK”)) //使用条件语句,以使GUI代码可以再次启用
print(“user clicked ok”); //制作一个OK按钮
}

◆ static var matrix : Matrix4x4 // 描述:GUI变换矩阵

◆ static var skin : GUISkin // 描述:使用的全局皮肤 你可以在任何时候设置这个来改变GUI的外观。如果设置为null,这个皮肤将使用默认的Unity皮肤。

◆ static var tooltip : string // 描述:鼠标移动到空间上的提示信息(只读)。

创建GUI空间时,你可以给他传递一个提示。这可以通过改变内容参数来制作一个自定义GUIContent物体,而不是仅仅传递一个字符串。 但鼠标经过带有提示性的控件时,它设置全局的GUI.tooltip值为传入得知。在OnGUI代码的末端,你可以制作一个标签来显示GUI.tooltip的值。

function OnGUI()
{

GUI.Button(Rect(10, 10, 100, 20), GUIContent(“Click me”, “This is the tooltip”));//制作一个按钮,它使用自定义GUI.Content参数来传递提示。
GUI.Button(Rect(10, 55, 100, 20), “No tooltip here”); //显示鼠标指向或具有键盘焦点的控件提示
}

你可以使用元素的次序来创建’层次化的’提示:

function OnGUI()
{
GUI.Box(Rect(5, 35, 110, 75), GUIContent(“Box”, “this box has a tooltip”)); //这个box比随后的许多元素大,并且它有一个提示。
GUI.Button(Rect(10, 55, 100, 20), “No tooltip here”); // 这个按钮在box内部,但是没有提示,因此它不会覆盖这个box的提示。
GUI.Button(Rect(10, 80, 100, 20), GUIContent(“I have a tooltip”, “This button overrides the box”)); // 这个按钮在box内部,并且有一个提示,因此它会覆盖这个box的提示。
GUI.Label(Rect(10, 40, 100, 40), GUI.tooltip); //最后,显示来自鼠标指向或具有键盘焦点的提示
}

Tooltip也能用来实现一个OnMouseOver/OnMouseOut消息系统:
var lastTooltip = “”;

function OnGUI()
{

GUILayout.Button(GUIContent(“Play Game”, “Button1”));
GUILayout.Button(GUIContent(“Quit”, “Button2”));

if(Event.current.type == EventType.repaint && GUI.tooltip! = lastTooltip)
{

if(lastTooltip != “”)SendMessage(lastTooltip + “OnMouseOut”, SendMessageOptions, DontRequireReceiver);

if(GUI.tooltip != “”)SendMessage(GUI.tooltip + “OnMouseOut”, SendMessageOptions, DontRequireReceiver);

lastTooltip = GUI.tooltip;
}

}

类方法

◆ static function BeginGroup(position : Rect) : void
◆ static function BeginGroup(position : Rect, text : string) : void
◆ static function BeginGroup(position : Rect, image : Texture) : void
◆ static function BeginGroup(position : Rect, content : GUIContent) : void
◆ static function BeginGroup(position : Rect, style : GUIStyle) : void
◆ static function BeginGroup(position : Rect, text : string, style : GUIStyle) : void
◆ static function BeginGroup(position : Rect, image : Texture, style : GUIStyle) : void
◆ static function BeginGroup(position : Rect, content : GUIContent, style : GUIStyle) : void

描述:

必须与EndGroup调用匹配。当你开始一个组时,用于GUI控件的坐标系统被设置为(0, 0)是组的左上角。所有控件被附加到组。组可以嵌套 – 如果使用,子被附加到他们的父。当你在屏幕上移动一组GUI元素是这是非常有用的。一个普通的用法是设计你的菜单以适合一个特定的屏幕的尺寸。然后在更大的显示器上居中显示GUI。

参数

position屏幕用于组的矩形区域。开始组,
text 显示在该组上的文本。
image显示在该组上的Texture。
content 用于这个组的文本,图形和提示。如果提供,任何鼠标点击被组捕获,并且如果没有设置,不会渲染背景,和传递鼠标点击。
style 用于背景的风格。

例:

function OnGUI()
{
GUI.BeginGroup(new Rect(Screen.width / 2 – 400, Screen.height / 2 – 300, 800, 600)); // 约束所有的绘图在屏幕中心800*600的区域

GUI.Box(new Rect(0, 0, 800, 600), “This box is new centered! – Here you would put your main menu”); //在由BeginGroup定义的新坐标空间中绘制一个box,注意,现在(0,0)已经被移动了

GUI.EndGroup(); //需要用一个EndGroup来匹配所有的BeginGroup调用。
}

参见:matrix, BeginScrollView

◆ static function BeginScrollView(position : Rect, scrollPostion: Vector2, viewRect : Rect) : Vector2
◆ static function BeginScrollView(position : Rect, scrollPostion: Vector2, viewRect : Rect, alwaysShowHorizontal : bool, alwaysShowVertical : bool) : Vector2
◆ static function BeginScrollView(position : Rect, scrollPostion: Vector2, viewRect : Rect, horizontalScrollbar : GUIStyle, verticalScrollbar : GUIStyle) : Vector2
◆ static function BeginScrollView(position : Rect, scrollPostion: Vector2, viewRect : Rect, alwaysShowHorizontal : bool, alwaysShowVertical : bool, horizontalScrollbar : GUIStyle, verticalScrollbar : GUIStyle) : Vector2

描述:在你的GUI中开始滚动视。ScrollViews让你在屏幕上制作一个较小的区域。使用放置在ScrollView边上的滚动条来查看一个较大的区域。

参数

position 屏幕上用了ScrollView的矩形区域
scrollPosition 用来显示的位置。
viewRect 用在滚动视内部的矩形。
alwaysShowHorizontal 可选的参数用来总是显示水平滚动条。如果为假或不设置,它只在clientRect比position宽的时候显示
alwaysShowVertical 可选的参数用来总是显示垂直滚动条。如果为假或不设置,它只在clientRect比position长的时候显示。
horizontalScrollbar 用于水平滚动条的可选GUIStyle,如果不设置,将使用当前GUISkin的horizontalScrollbar.
verticalScrollbar 用于水平滚动条的可选GUIStyle,如果不设置,将使用当前GUISkin的verticalScrollbar.
返回Vector2 – 修改过的scrollPosition回传这个变量。如下的例子。

var smallPosition = Vector2.zero; //滚动视口的位置
function OnGUI()
{

scrollPosition = GUI.BeginScrollView(Rect(10, 300, 100, 100), scrollPosition, Rect(0, 0, 220, 200)); //一个绝对位置的例子。制作一个具有较大区域的滚动视。 并将它放置在一个小的矩形中。

GUI.Button(Rect(0, 0, 100, 20), “Top-left”); //制作四个按钮 – 每个角上有一个,由BeginScrollView最后一个参数定义的坐标系统。
GUI.Button(Rect(0, 0, 100, 20), “Top-right”);
GUI.Button(Rect(0, 0, 100, 20), “Bottom-left”);
GUI.Button(Rect(0, 0, 100, 20), “Bottom-right”);

GUI.EndScrollView();//结束前面开始滚动视

}


◆ static function Box(position : Rect, text : string) : void
◆ static function Box(position : Rect, image : Texture) : void
◆ static function Box(position : Rect, content : GUIContent) : void
◆ static function Box(position : Rect, text : string, style : GUIStyle) : void
◆ static function Box(position : Rect, image : Texture, style : GUIStyle) : void
◆ static function Box(position : Rect, content : GUIContent, style : GUIStyle) : void

描述:制作一个图形box。

参数

position屏幕上用于box的矩形区域
text 显示在该box上的文本
image显示在该box上的Texture
content用于这个box的文本,图形和提示
style 使用的风格。如果不设置,将使用当前GUISkin的box风格。

◆ static function BringWindowToBack(windowID: int) : void

描述:将特定的窗口放到浮动窗口的后面。

参数: windowID 在window调用中创建窗体时使用的唯一标识。

◆ static function BringWindowToFront(windowID: int) : void

描述:将特定的窗口放到浮动窗口的前面。

参数: windowID 在Window调用中创建窗体时使用的唯一标识。

◆ static function Button(position : Rect, text : string) : bool
◆ static function Button(position : Rect, image : Texture) : bool
◆ static function Button(position : Rect, content : GUIContent) : bool
◆ static function Button(position : Rect, text : string, style : GUIStyle) : bool
◆ static function Button(position : Rect, image : Texture, style : GUIStyle) : bool
◆ static function Button(position : Rect, content : GUIContent, style : GUIStyle) : bool

描述:制作一个简单的按钮,用户点击它们的时候,就会有些事情发生。返回bool - /true/当用户单击按钮时

参数

position屏幕上用于按钮的矩形区域
text 显示在该按钮上的文本
image显示在该按钮上的Texture
content用于这个按钮的文本,图形和提示
style 使用的风格。如果不设置,将使用当前GUISkin的button风格。

◆ static function DragWindow(position : Rect) : void

参数: position可以拖动的窗口部分。这个被附加到实际窗口。

描述:让窗口可拖动。 插入这个函数的在你的窗口代码中使窗口可拖动。

var windowRect = Rect(20, 20, 120, 50);

function OnGUI()
{
windowRect = GUI.Window(0, windowRect, DoMyWindow, “My Window"); //注册窗体
}

function DoMyWindows(windowID : int) //制作窗体内容
{

GUI.DragWindow(Rect(0, 0, 10000, 20));//制作一个非常长的矩形,高20像素。这将使得窗口可以调整大小,通过顶部的标题栏 – 不管它有多宽。
}

◆ static function DragWindow() : void

如果你想将整个窗体背景作为一个可拖动区域,不使用参数并将DragWindow放置到整体函数的最后。 这意味着任何其他类型的控件将首先被处理并且拖动将只在没有其他控件获得焦点时使用。

◆ static function DrawTexture(position : Rect, image : Texture, scaleMode : ScaleMode = stretchToFill, alphaBlend : bool = true, imageAspect : float = 0) : void

参数

position在屏幕上绘制一个内部包含有纹理的矩形。
image需要被绘制的纹理。
scaleMode 定义了当矩形的长宽比和内部图像的长宽比不同时如何缩放图像。
alphaBlend 定义了alpha值是否参与图像的混合(默认为真)。如果为假,图像将会被绘制并显示。
imageAspect 源图像的的长宽比。如果是0(默认值),则使用图像自身的长宽比。

描述:在一个矩形内部绘制一个纹理。 参见:GUI.color, GUI.contentColor

◆ static function EndGroup() : void // 描述:结束一个组 参见:BeginGroup()。

◆ static function EndScrollView : void // 描述:结束一个由BeginScrollView开始的滚动视。

◆ static function FocusControl(name : string) : void // 描述:把键盘焦点转移到定义的控件。 参见:SetNextControlName, GetNameOfFocusedControl。

var username = "username";

function OnGUI ()
{
GUI.SetNextControlName ("MyTextField"); // 设置一个文本域中内部名称

username = GUI.TextField (Rect (10,10,100,20), username); // 制作一个文本区域

if (GUI.Button (Rect (10,40,80,20), "Move Focus")) // 如果年下这个按钮,键盘焦点将被移动

GUI.FocusControl ("MyTextField");
}

◆ static function FocusWindow(windowID : int) : void

参数: windowID 当调用Window而创建的窗口的标识符。

描述:使一个窗口被激活。 参见:GUI.UnfocusWindow

◆ static function GetNameOfFocusedControl() : string

描述:返回当前激活的控件的名字。控件的名字是由SetNextControlName函数创建的。当有名字的控件被激活时,函数返回它的名字;否则的话返回一个空字符串。

var login = "username";
var login2 = "no action here";
function OnGUI ()
{
GUI.SetNextControlName ("user");
login = GUI.TextField (Rect (10,10,130,20), login);
login2 = GUI.TextField (Rect (10,40,130,20), login2);

if (Event.current.Equals (Event.KeyboardEvent ("return")) && GUI.GetNameOfFocusedControl () == "user")
{
Debug.Log ("Login");
}
if (GUI.Button (new Rect (150,10,50,20), "Login"))
Debug.Log ("Login");
}

参见:SetNextControlName, FocusControl

login2 = GUI.TextField(new Rect(10, 40, 130, 20), login2);

if(Event.current.Equals(Event.KeyboardEvent.(“return”))&&GUI.GetNameOfFocusedControl() == “user”) Debug.log(“Login”);

if(GUI.Button(new Rect(150, 10, 50, 20), “Login”)) Debug.log(“Login”);
}

参见:SetNextControlName

◆ static function HorizontalScrollbar(position : Rect, value : float, size : float, leftValue : float, rightValue : float) : bool
◆ static function HorizontalScrollbar(position : Rect, value : float, size : float, leftValue : float, rightValue : float, style : GUIStyle) : bool

描述:制作一个水平滚动条。滚动条可以用来滚动文档。大多数情况下,你会使用scrollViews代替。返回float – 修改后的值。这可以通过拖动这个滚动条,或者单击两端的箭头来改变。

参数

position屏幕上用于滚动条的矩形区域
value在min和max之间的位置
size 能看见多大?
leftValue滚动条左边的值
rightValue 滚动条右边的值
style 用于滚动条背景的风格。如果不设置,将使用当前GUISkin的horizontalScrollbar。

找到额外的元素: 在滚动条两端的按钮将在当前皮肤中搜索”leftbutton”和”rightbutton”作为风格。滚动条的滑块(你拖动的东西)将搜索并使用名为“thumb”的风格。

scrollPos = HoriztontalScrollbar(Rect(0, 0, 100, 20), scrollPos, 1, 0, 100, “My Scrollbar”);

//这将使用下面的风格名来决定该按钮的尺寸/位置, MyScrollbarrightbutton – 用于左侧按钮的风格名称, MyScrollbarleftbutton – 用于右侧按钮的风格名称, MyScrollbarthumb – 用于滑块的风格名称

◆ static function HorizontalSlider(position : Rect, value : float, leftValue : float, rightValue : float) : float
◆ static function HorizontalSlider(position : Rect, value : float, leftValue : float, rightValue : float, slider : GUIStyle, thumb : GUIStyle) : float

描述:一个用户可以拖动的滑杆。可以在min和max只见改变一个值。 返回float – 被用户设置的值。

参数

position屏幕上用于滑杆的矩形区域
value滑杆显示的值。这个决定可拖动的位置。
leftValue滑杆左边的值
rightValue 滑杆右边的值
slider用于显示拖动区域的GUIStyle。如果不设置, 将使用当前GUISkin的 horizontalSlider。
thumb用于显示拖动块的GUIStyle。如果不设置, 将使用当前GUISkin的horizontalSliderThumb。

◆ static function Label(position : Rect, text : string) : void
◆ static function Label(position : Rect, image : Texture) : void
◆ static function Label(position : Rect, content : GUIContent) : void
◆ static function Label(position : Rect, text : string, style : GUIStyle) : void
◆ static function Label(position : Rect, image : Texture, style : GUIStyle) : void
◆ static function Label(position : Rect, content : GUIContent, style : GUIStyle) : void

描述:在屏幕上制作一个文本或者纹理标签. 标签没有用户交互,不会获取鼠标点击并总是以普通风格渲染。如果你想制作一个可视化响应用户输入的控件,使用一个Box控件。

参数
position屏幕上用于标签的矩形区域
text 显示在该标签上的文本
image显示在该标签上的Texture
content用于这个标签的文本,图形和提示
style 使用的风格。如果不设置,将使用当前GUISkin的label

例如:绘制一个传统的Hello world字符串

function OnGUI
{
GUI.Label(Rect(10, 10, 100, 20), “Hello world”);
}

例如:在屏幕上绘制一个纹理。标签也用于显示纹理,而不仅是字符串。简单传递一个纹理。

var textureToDisplay : Texture2D;
function OnGUI()
{
GUI.Label(Rect(10, 40, textureToDisplay.width, textureToDiplay.height), textureToDisplay);
}

◆ static function PasswordField(position : Rect, password : string, markChar : char) : string
◆ static function PasswordField(position : Rect, password : string, markChar : char, maxLength : int) : string
◆ static function PasswordField(position : Rect, password : string, markChar : char, style : GUIStyle) : string
◆ static function PasswordField(position : Rect, password : string, markChar : char, markChar : char, style : GUIStyle) : string

描述:制作一个用户可以输入密码的文本域。 返回string – 编辑过的密码

参数

position屏幕上用于文本的矩形区域
password 用于编辑的密码。这个函数返回值应该被赋回这个字符串。如下的例 子。
markChar 用来隐藏密码的字符。
maxLength 字符串的最大长度。如果不设置,用户可以一直输入。
style使用的风格。不过不设置,将那个使用当前GUISkin的textField风格。

var passwordToEdit = “My Password”;

function OnGUI()
{
passwordToEdit = GUI.PasswordField(Rect(10, 10, 200, 20), passwordToEdit, “*”, 25); // 制作一个文本来调整stringToEdit。
}

◆ static function RepeatButton(position : Rect, text : string) : bool
◆ static function RepeatButton(position : Rect, image : Texture) : bool
◆ static function RepeatButton(position : Rect, content : GUIContent) : bool
◆ static function RepeatButton(position : Rect, text : string, style : GUIStyle) : bool
◆ static function RepeatButton(position : Rect, image : Texture, style : GUIStyle) : bool
◆ static function RepeatButton(position : Rect, content : GUIContent, style : GUIStyle) : bool

描述:制作一个按钮。当用户按住它时一直是激活的。返回bool - /true/当用户单击按钮时。

参数

position 屏幕上用于按钮的矩形区域
text显示在该按钮上的文本
image 显示在该按钮上的Texture
content 用于这个按钮的文本,图形和提示
style使用的风格。不过不设置,将那个使用当前GUISkin的button风格。

◆ static function ScrollTo(position : Rect) : void

描述:滚动所有包含在scrollview中的数据以便position可见。

◆ static function SelectionGrid (position : Rect, selected : int, texts : string[], xCount : int) : int
◆ static function SelectionGrid (position : Rect, selected : int, images : Texture[], xCount : int) : int
◆ static function SelectionGrid (position : Rect, selected : int, content : GUIContent[], xCount : int) : int
◆ static function SelectionGrid (position : Rect, selected : int, texts : string[], xCount : int, style : GUIStyle) : int
◆ static function SelectionGrid (position : Rect, selected : int, images : Texture[], xCount : int, style : GUIStyle) : int
◆ static function SelectionGrid (position : Rect, selected : int, content : GUIContent[], xCount : int, style : GUIStyle) : int

描述:制作一个按钮网络。返回int – 选择按钮的索引。

参数

position屏幕上用于网格的矩形区域。
selected选择的网格按钮的索引
texts 显示在网格按钮上的字符串数组
images显示在网格按钮上的纹理数组
contents用于这个网格按钮的文本,图形和提示数组
xCount在水平方向有多少个像素。空间将被缩放来适应,除非风格定义了一 个fixWidth。
style 使用的风格。如果不设置,将使用当前GUISkin的button风格。

◆ static function SetNextControlName(name : string) : void

描述:设置下一个控件的名称。 这是接下来的控件被注册。

◆ static function TextArea(position : Rect, text : string) : string
◆ static function TextArea(position : Rect, text : string, maxLength : int) : string
◆ static function TextArea(position : Rect, text : string, style : GUIStyle) : string
◆ static function TextArea(position : Rect, text : string, maxLength : int, style : GUIStyle) : string

描述:制作一个多行文本区域。这里用户可以编辑这个字符串。返回string – 编辑过的字符串

参数

position 屏幕上用于文本的矩形区域
text用于编辑的文本。这个函数返回值应该被赋回这个字符串。如下的例子。
maxLength 字符串的最大长度。如果不设置,用户可以一直输入。
style使用的风格。如果不设置,将使用当前GUISkin的textArea。

var stringToEdit = “Hello WorldnI’ve got 2 lines…”;
function OnGUI()
{
stringToEdit = GUI.TextArea(Rect(10, 10, 200, 100), stringToEdit, 200); //制作一个多行文本区域来调整stringToEdit
}

◆ static function TextField(position : Rect, text : string) : string
◆ static function TextField(position : Rect, text : string, maxLength : int) : string
◆ static function TextField(position : Rect, text : string, style : GUIStyle) : string
◆ static function TextField(position : Rect, text : string, maxLength : int, style : GUIStyle) : string

描述:制作一个单行文本域。这里用户可以编辑这个字符串。返回string – 编辑过的字符串

参数
position 屏幕上用于文本的矩形区域
text用于编辑的文本。这个函数返回值应该被赋回这个字符串。如下的例子。
maxLength 字符串的最大长度。如果不设置,用户可以一直输入。
style使用的风格。如果不设置,将使用当前GUISkin的textField风格。

var stringToEdit = “Hello World”;

function OnGUI()
{
stringToEdit = GUI.TextField(Rect(10, 10, 200, 20), stringToEdit, 25); //制作一个文本域来调整stringToEdit
}

◆ static function Toggle(position : Rect, value : bool, text : string) : bool
◆ static function Toggle(position : Rect, value : bool, image : Texture) : bool
◆ static function Toggle(position : Rect, value : bool, content : GUIContent) : bool
◆ static function Toggle(position : Rect, value : bool, text : string, style : GUIStyle) : bool
◆ static function Toggle(position : Rect, value : bool, image : Texture, style : GUIStyle) : bool
◆ static function Toggle(position : Rect, value : bool, content : GUIContent, style : GUIStyle) : bool

描述:制作一个on/off开关按钮,返回bool – 按钮的新值

参数

position屏幕上用于按钮的矩形区域
value这个按钮是打开的或关闭
text 显示在该按钮上的文本
image显示在该按钮上的Texture
content用于这个按钮的文本,图形和提示
style 使用的风格。如果不设置,将使用当前GUISkin的toggle风格。

◆ static function Toolbar(position : Rect, selected : int, texts : string[]) : int
◆ static function Toolbar(position : Rect, selected : int, images : Texture[]) : int
◆ static function Toolbar(position : Rect, selected : int, contents : GUIContent[]) : int
◆ static function Toolbar(position : Rect, selected : int, texts : string, style : GUIStyle[]) : int
◆ static function Toolbar(position : Rect, selected : int, images : Texture, style : GUIStyle[]) : int
◆ static function Toolbar(position : Rect, selected : int, contents : GUIContent[], style : GUIStyle) : int

描述:制作一个工具栏 ,返回int – 选择按钮的索引

参数

position屏幕上用于工具栏的矩形区域
selected选择按钮的索引
texts 显示在该工具栏上的字符串数组
images显示在工具栏按钮上的纹理数组
contents用于这个工具栏的文本,图形和提示数组。
style 使用的风格。如果不设置,将使用当前GUISkin的button风格。

◆ static function UnfocusWindows() : void

描述:从所有窗体上移除焦点。

◆ static function VerticalScrollbar(position : Rect, value : float, size : float, topValue : float, buttonValue : float) : float
◆ static function VerticalScrollbar(position : Rect, value : float, size : float, topValue : float, buttonValue : float, style : GUIStyle) : float

描述:制作一个垂直滚动条。滚动条可以用来滚动文档。大多数情况下,你会使用scrollViews代替。返回float – 修改后的值。这可以通过拖动这滚动条,或者单击两端的箭头来改变。

参数
position屏幕上用于滚动条的矩形区域。
value在min和max之间的位置。
size 能看见多大?
topValue滚动条顶端的值
bottomValue 滚动条底端的值
style 使用的风格。如果不设置,将使用当前GUISkin的horizontalScrollbar 风格。

找到额外的元素: 在滚动条两端的按钮将在当前皮肤中搜索“upbutton”和“downbutton”作为风格。滚动条的滑块(你拖动的东西)将搜索并使用名为“thumb”的风格。

scrollPos = HorizontalScrollbar(Rect(0, 0, 100, 20), scrollPos, 1, 0, 100, “MyVertScrollbar”);

//这将使用下面的风格名来决定该按钮的尺寸位置。MyVertScrollbarupbutton – 用于上端按钮的风格名称。MyVertScrollbardownbutton – 用于下端按钮的风格名称。MyVertScrollbarthumb – 用于滑块的风格名称。

◆ static function VerticalSlider(position : Rect, value : float, topValue : float, buttonValue : float) : float
◆ static function VerticalSlider(position : Rect, value : float, topValue : float, buttonValue : float, slider : GUIStyle, thumb : GUIStyle) : float

描述:一个用户可以拖动的垂直滑杆。可以在min和max之间改变一个值。返回float – 被用户设置的值。

参数

position屏幕上用于滑杆的矩形区域。
value滑杆显示的值。这个决定可移动滑块的位置。
topValue滑杆顶端的值
bottomValue 滑杆底端的值
slider用于显示拖动区域的GUIStyle。如果不设置,将使用当前GUISkin的horizontalSlider。
thumb用于显示土洞区域的GUIStyle。如果不设置,将使用当前GUISkin的horizontalSliderThumb。

◆ static function Window(id : int, position : Rect, func : WindowFunction, text : string) : Rect
◆ static function Window(id : int, position : Rect, func : WindowFunction, image : Texture) : Rect
◆ static function Window(id : int, position : Rect, func : WindowFunction, content : GUIContent) : Rect
◆ static function Window(id : int, position : Rect, func : WindowFunction, text : string, style : GUISytle) : Rect
◆ static function Window(id : int, position : Rect, func : WindowFunction, image : Texture, style : GUIStyle) : Rect
◆ static function Window(id : int, clientRect : Rect, func : WindowFunction, title : GUIContent, style : GUIStyle) : Rect

描述:制作一个弹出窗口 窗口浮动在普通GUI控件之上,具有单击激活的特点并可以有选择的随意被端用户拖动。不像其他的控件,你需要传递给他们一个独立的功能并放置在窗口中。注意:如果你使用GUILayout在窗口中放置你的组件,你应该使用GUILayout.Window。 返回Rect – 窗口位于的矩形位置

参数

id用于每个窗口的唯一ID。这是用于接口的ID。
clientRect 屏幕上用于组的矩形区域。
func在窗体内部创建GUI的函数。这个函数必须使用一个函数 – 当前创建 GUI的窗体id
text作为窗体标签的文本。
image 用于在标题栏上显示图片的Texture
content 用于这个窗口的文本,图形和提示。
style用于窗口的可选风格。如果不设置,将使用当前GUISkin的window。

这是一个小例子帮助你开始:

var windowRect = Rect(20, 20, 120, 50);
function OnGUI()
{
windowRect = GUI.Window(0, windowRect, DoMyWindow, “My Window”); //注册窗口。注意第三个参数。
}

function DoMyWindow(windowID : int) //制作窗口内容
{
if(GUI.Button(Rect(10, 20, 100, 20), ”Hello World”)) print(“Get a click”);
}

你可以使用相同的函数来创建多个窗口。需要确保每个窗口有一个自己的ID。例如:

var windowRect0 = Rect(20, 20, 120, 50);
var windowRect1 = Rect(20, 100, 120, 50);
function OnGUI()

windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, “My Window”); //注意窗口。我们创建了两个使用相同函数的窗体,
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, “My Window”); // 注意他们的ID不同。
}

function DoMyWindow(windowID : int) //制作窗口内容
{
if(GUI.Button(Rect(10, 20, 100, 20), “Hello World”)) print(“Get a click in window ” + windowID);
GUI.DragWindow(Rect(0, 0, 10000, 10000)); //使窗口可以被拖动
}

停止显示窗口,简单的在DoGUI函数内停止调用GUI.Window。

var doWindow() = true; //布尔变量以决定是否显示窗口,从游戏GUI,脚本,检视面板中或者其他地方改变这个决定窗口是否可见。
function OnGUI() //制作窗口内容
{
doWindow() = GUI.Toggle(Rect(10, 10, 100, 20), doWindow(), “Window 0”); //制作一个开关变量来隐藏或显示窗口
if(doWindow()) //确保仅在doWindow()为真时调用GUI.Window
GUI.Window(0, Rect(110, 10, 200, 60), DoWindow(), “Basic Window”);
}

为了使窗口从自动GUI获取它的尺寸,使用GUILayout.Window。 调用顺序 窗口需要从后向前绘制。在其他窗口顶部的窗口需要在其他窗口之后绘制。这就意味着你不能指望你的DoWindow函数以任何特定的顺序被调用。为了让这个能够工作,当你的创建窗口时下面值被存储(使用Window函数),当DoWindow被调用时取回:GUI.skin, GUI.enabled, GUI.color, GUI.backgroundColor, GUI.contentColor, GUI.matrix 这就是说很容易像这样制作彩色窗口:

var windowRect0 = Rect(20, 20, 120, 50);
var windowRect1 = Rect(20, 100, 120, 50);

function OnGUI()
{
GUI.color = Color.red; //这里我们制作了2个窗口,在这个之前设置GUI.color的值。
windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, “Red Window”);
GUI.color = Color.green;
windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, “Green Window”);
}

function DoMyWindow(windowID : int) //制作窗口内容 GUI.color的值被设置为窗口被创建之前的值。
{
if(GUI.Button(Rect(10, 20, 100, 20), “Hello world!”)) print(“Got a click in window with color ” + GUI.color);
GUI.DragWindow(Rect(0, 0, 10000, 10000)); //使窗口可以被拖动
}

提示:你可以使用GUI.color的alpha组件来淡入淡出窗口。

参见:DragWindow, BringWindowToFront, BringWindowToBack

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值