查看: 2472|回复: 0
打印 上一主题 下一主题

Unity3D脚本中文教程2.21

[复制链接]

1602

主题

1

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
24658
精华
6

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-5-21 14:12:55 |只看该作者 |倒序浏览
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 = ***e;

//两个扩展选项

var extended1 = ***e;

var extended2 = ***e;

function OnGUI()

{

//制作一个开关控件以便允许用户编辑扩展的选项

allOptions = GUI.Toogle(Rect(0, 0, 150, 20), allOptions, “Edit All Options”);
//将它的值赋给GUIEnabled – 如果上面的复选框被禁用

//这些GUI元素将

GUIenabled = allOptions
//这两个控件只在上面的按钮为On时启用。

extended1 = GUI.Toogle(Rect(20, 20, 130, 20), extended1, “Extended Option 1”);

extended2 = GUI.Toogle(Rect(20, 40, 130, 30), extended2, “Extended Option 2”);
//使用条件语句,以使GUI代码可以再次启用

GUI.enabled = ***e;

//制作一个OK按钮

if(GUI.Button(Rect(0, 60, 150, 20), “OK”))

print(“user clicked 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.Content参数来传递提示。

GUI.Button(Rect(10, 10, 100, 20), GUIContent(“Click me”, “This is the tooltip”));
//显示鼠标指向或具有键盘焦点的控件提示

GUI.Button(Rect(10, 55, 100, 20), “No tooltip here”);

}

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

function OnGUI()

{

//这个box比随后的许多元素大,并且它有一个提示。

GUI.Box(Rect(5, 35, 110, 75), GUIContent(“Box”, “this box has a tooltip”));
这个按钮在box内部,但是没有提示,因此它不会覆盖这个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”));

//最后,显示来自鼠标指向或具有键盘焦点的提示

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);

  lastTool        ip = 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

参数

position屏幕用于组的矩形区域。

text 显示在该组上的文本。

image显示在该组上的Texture。

content  用于这个组的文本,图形和提示。如果提供,任何鼠标点击被组捕获,并且如果没有设置,不会渲染背景,和传递鼠标点击。

style 用于背景的风格。

描述:

开始组,必须与EndGroup调用匹配。

当你开始一个组时,用于GUI控件的坐标系统被设置为(0, 0)是组的左上角。所有控件被附加到组。组可以嵌套 – 如果使用,子被附加到他们的父。

当你在屏幕上移动一组GUI元素是这是非常有用的。一个普通的用法是设计你的菜单以适合一个特定的屏幕的尺寸。然后在更大的显示器上居中显示GUI。

function OnGUI()

{

//约束所有的绘图在屏幕中心800*600的区域

GUI.BeginGroup(new Rect(Screen.width / 2 – 400, Screen.height / 2 – 300, 800, 600));

//在由BeginGroup定义的新坐标空间中绘制一个box

//注意,现在(0,0)已经被移动了

GUI.Box(new Rect(0, 0, 800, 600), “This box is new centered! – Here you would put your main menu”);

//需要用一个EndGroup来匹配所有的BeginGroup调用。

GUI.EndGroup();

}

参见: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

参数

position  屏幕上用了ScrollView的矩形区域

scrollPosition 用来显示的位置。

viewRect  用在滚动视内部的矩形。

alwaysShowHorizontal  可选的参数用来总是显示水平滚动条。如果为假或不设置,它只在clientRect比position宽的时候显示

alwaysShowVertical 可选的参数用来总是显示垂直滚动条。如果为假或不设置,它只在clientRect比position长的时候显示。

horizontalScrollbar 用于水平滚动条的可选GUIStyle,如果不设置,将使用当前GUISkin的horizontalScrollbar.

verticalScrollbar 用于水平滚动条的可选GUIStyle,如果不设置,将使用当前GUISkin的verticalScrollbar.

返回Vector2 – 修改过的scrollPosition回传这个变量。如下的例子。

描述:在你的GUI中开始滚动视。

ScrollViews让你在屏幕上制作一个较小的区域。使用放置在ScrollView边上的滚动条来查看一个较大的区域。

//滚动视口的位置

var smallPosition = Vector2.zero;

function OnGUI()

{

//一个绝对位置的例子。制作一个具有较大区域的滚动视。

//并将它放置在一个小的矩形中。

scrollPosition = GUI.BeginScrollView(Rect(10, 300, 100, 100), scrollPosition, Rect(0, 0, 220, 200));

//制作四个按钮 – 每个角上有一个,由BeginScrollView最后一个参数定义的坐标系统。

GUI.Button(Rect(0, 0, 100, 20), “Top-left”);

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
参数

position屏幕上用于box的矩形区域

text 显示在该box上的文本

image显示在该box上的Texture

content用于这个box的文本,图形和提示

style 使用的风格。如果不设置,将使用当前GUISkin的box风格。

描述:制作一个图形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

参数

position屏幕上用于按钮的矩形区域

text 显示在该按钮上的文本

image显示在该按钮上的Texture

content用于这个按钮的文本,图形和提示

style 使用的风格。如果不设置,将使用当前GUISkin的button风格。

返回bool - /***e/当用户单击按钮时

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

◆ 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)

{

//制作一个非常长的矩形,高20像素。

//这将使得窗口可以调整大小,通过顶部的标题栏 – 不管它有多宽。

GUI.DragWindow(Rect(0, 0, 10000, 20));

}

◆ static function DragWindow() : void

如果你想将整个窗体背景作为一个可拖动区域,不使用参数并将DragWindow放置到整体函数的最后。

这意味着任何其他类型的控件将首先被处理并且拖动将只在没有其他控件获得焦点时使用。

◆ static function DrawTexture(position : Rect, image : Texture, scaleMode : ScaleMode =  stretchToFill, alphaBlend : bool = ***e, 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");

  // Make the actual text field.

  username = GUI.TextField (Rect (10,10,100,20), username);

  // If the user presses this button, keyboard focus will move.

  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

参数

position屏幕上用于滚动条的矩形区域

value在min和max之间的位置

size 能看见多大?

leftValue滚动条左边的值

rightValue  滚动条右边的值

style 用于滚动条背景的风格。如果不设置,将使用当前GUISkin的horizontalScrollbar。

返回float – 修改后的值。这可以通过拖动这个滚动条,或者单击两端的箭头来改变。

描述:制作一个水平滚动条。滚动条可以用来滚动文档。大多数情况下,你会使用scrollViews代替。

找到额外的元素:

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

//这将使用下面的风格名来决定该按钮的尺寸/位置

//MyScrollbarrightbutton – 用于左侧按钮的风格名称

//MyScrollbarleftbutton – 用于右侧按钮的风格名称

//MyScrollbarthumb – 用于滑块的风格名称

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

◆ 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

参数

position屏幕上用于滑杆的矩形区域

value滑杆显示的值。这个决定可拖动的位置。

leftValue滑杆左边的值

rightValue  滑杆右边的值

slider用于显示拖动区域的GUIStyle。如果不设置, 将使用当前GUISkin的  horizontalSlider。

thumb用于显示拖动块的GUIStyle。如果不设置, 将使用当前GUISkin的horizontalSliderThumb。

返回float – 被用户设置的值。

描述:一个用户可以拖动的滑杆。可以在min和max只见改变一个值。

◆ 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

参数

position屏幕上用于标签的矩形区域

text 显示在该标签上的文本

image显示在该标签上的Texture

content用于这个标签的文本,图形和提示

style 使用的风格。如果不设置,将使用当前GUISkin的label

描述:在屏幕上制作一个文本或者纹理标签.

标签没有用户交互,不会获取鼠标点击并总是以普通风格渲染。如果你想制作一个可视化响应用户输入的控件,使用一个Box控件。

例如:绘制一个传统的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

参数

position屏幕上用于文本的矩形区域

password  用于编辑的密码。这个函数返回值应该被赋回这个字符串。如下的例  子。

markChar  用来隐藏密码的字符。

maxLength  字符串的最大长度。如果不设置,用户可以一直输入。

style使用的风格。不过不设置,将那个使用当前GUISkin的textField风格。

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

var passwordToEdit = “My Password”;
function OnGUI()

{

  //制作一个文本来调整stringToEdit。

  passwordToEdit = GUI.PasswordField(Rect(10, 10, 200, 20), passwordToEdit, “*”, 25);

}
◆ 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

参数

position  屏幕上用于按钮的矩形区域

text显示在该按钮上的文本

image  显示在该按钮上的Texture

content  用于这个按钮的文本,图形和提示

style使用的风格。不过不设置,将那个使用当前GUISkin的button风格。

返回bool - /***e/当用户单击按钮时。

描述:制作一个按钮。当用户按住它时一直是激活的。

◆ 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

参数

position屏幕上用于网格的矩形区域。

selected选择的网格按钮的索引

texts 显示在网格按钮上的字符串数组

images显示在网格按钮上的纹理数组

contents用于这个网格按钮的文本,图形和提示数组

xCount在水平方向有多少个像素。空间将被缩放来适应,除非风格定义了一  个fixWidth。

style 使用的风格。如果不设置,将使用当前GUISkin的button风格。

返回int – 选择按钮的索引。

描述:制作一个按钮网络。

◆ 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

参数

position  屏幕上用于文本的矩形区域

text用于编辑的文本。这个函数返回值应该被赋回这个字符串。如下的例子。

maxLength 字符串的最大长度。如果不设置,用户可以一直输入。

style使用的风格。如果不设置,将使用当前GUISkin的textArea。

返回string – 编辑过的字符串

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

var stringToEdit = “Hello World
I’ve got 2 lines…”;

function OnGUI()

{

  //制作一个多行文本区域来调整stringToEdit

  stringToEdit = GUI.TextArea(Rect(10, 10, 200, 100), stringToEdit, 200);

}

◆ 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

参数

position  屏幕上用于文本的矩形区域

text用于编辑的文本。这个函数返回值应该被赋回这个字符串。如下的例子。

maxLength 字符串的最大长度。如果不设置,用户可以一直输入。

style使用的风格。如果不设置,将使用当前GUISkin的textField风格。

返回string – 编辑过的字符串

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

var stringToEdit = “Hello World”;
function OnGUI()

{

  //制作一个文本域来调整stringToEdit

  stringToEdit = GUI.TextField(Rect(10, 10, 200, 20), stringToEdit, 25);

}

◆ 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

参数

position屏幕上用于按钮的矩形区域

value这个按钮是打开的或关闭

text 显示在该按钮上的文本

image显示在该按钮上的Texture

content用于这个按钮的文本,图形和提示

style 使用的风格。如果不设置,将使用当前GUISkin的toggle风格。

返回bool – 按钮的新值

描述:制作一个on/off开关按钮

◆ 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

参数

position屏幕上用于工具栏的矩形区域

selected选择按钮的索引

texts 显示在该工具栏上的字符串数组

images显示在工具栏按钮上的纹理数组

contents用于这个工具栏的文本,图形和提示数组。

style 使用的风格。如果不设置,将使用当前GUISkin的button风格。

返回int – 选择按钮的索引

描述:制作一个工具栏
◆ 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

参数

position屏幕上用于滚动条的矩形区域。

value在min和max之间的位置。

size 能看见多大?

topValue滚动条顶端的值

bottomValue  滚动条底端的值

style 使用的风格。如果不设置,将使用当前GUISkin的horizontalScrollbar  风格。

返回float – 修改后的值。这可以通过拖动这滚动条,或者单击两端的箭头来改变。

描述:制作一个垂直滚动条。滚动条可以用来滚动文档。大多数情况下,你会使用scrollViews代替。

找到额外的元素:

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

//这将使用下面的风格名来决定该按钮的尺寸位置。

//MyVertScrollb***pbutton – 用于上端按钮的风格名称。

//MyVertScrollbardownbutton – 用于下端按钮的风格名称。

//MyVertScrollbarthumb – 用于滑块的风格名称。

scrollPos = HorizontalScrollbar(Rect(0, 0, 100, 20), scrollPos, 1, 0, 100, “MyVertScrollbar”);
◆ 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

参数

position屏幕上用于滑杆的矩形区域。

value滑杆显示的值。这个决定可移动滑块的位置。

topValue滑杆顶端的值

bottomValue  滑杆底端的值

slider用于显示拖动区域的GUIStyle。如果不设置,将使用当前GUISkin的horizontalSlider。

thumb用于显示土洞区域的GUIStyle。如果不设置,将使用当前GUISkin的horizontalSliderThumb。

返回float – 被用户设置的值。

描述:一个用户可以拖动的垂直滑杆。可以在min和max之间改变一个值。

◆ 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

参数

id用于每个窗口的唯一ID。这是用于接口的ID。

clientRect 屏幕上用于组的矩形区域。

func在窗体内部创建GUI的函数。这个函数必须使用一个函数 – 当前创建  GUI的窗体id

text作为窗体标签的文本。

image  用于在标题栏上显示图片的Texture

content  用于这个窗口的文本,图形和提示。

style用于窗口的可选风格。如果不设置,将使用当前GUISkin的window。

返回Rect – 窗口位于的矩形位置

描述:制作一个弹出窗口

窗口浮动在普通GUI控件之上,具有单击激活的特点并可以有选择的随意被端用户拖动。不像其他的控件,你需要传递给他们一个独立的功能并放置在窗口中。注意:如果你使用GUILayout在窗口中放置你的组件,你应该使用GUILayout.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()

  //注意窗口。我们创建了两个使用相同函数的窗体

  //注意他们的ID不同。

windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, “My Window”);

windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, “My Window”)

}

//制作窗口内容

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。

//布尔变量以决定是否显示窗口

//从游戏GUI,脚本,检视面板中或者其他地方改变这个决定窗口是否可见。

var doWindow() = ***e;

//制作窗口内容

function OnGUI()

{

//制作一个开关变量来隐藏或显示窗口

doWindow() = GUI.Toggle(Rect(10, 10, 100, 20), doWindow(), “Window 0”);

//确保仅在doWindow()为真时调用GUI.Window

if(doWindow())

  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()

{

  //这里我们制作了2个窗口,在这个之前设置GUI.color的值。

  GUI.color = Color.red;

  windowRect0 = GUI.Window(0, windowRect0, DoMyWindow, “Red Window”);

  GUI.color = Color.green;

  windowRect1 = GUI.Window(1, windowRect1, DoMyWindow, “Green Window”);

}

//制作窗口内容

//GUI.color的值被设置为窗口被创建之前的值。

function DoMyWindow(windowID : int)

{

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
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-14 10:04 , Processed in 0.088912 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部