Unity3D GUI翻译.docx
《Unity3D GUI翻译.docx》由会员分享,可在线阅读,更多相关《Unity3D GUI翻译.docx(59页珍藏版)》请在冰豆网上搜索。
![Unity3D GUI翻译.docx](https://file1.bdocx.com/fileroot1/2023-2/2/59582676-d92d-40a1-a5b6-94148c65a060/59582676-d92d-40a1-a5b6-94148c65a0601.gif)
Unity3DGUI翻译
GUIBasics图形用户界面基础
ReferenceManual>GUIScriptingGuide>GUIBasics
ThissectionwillexplainthebarenecessitiesforscriptingControlswithUnityGUI.
这一节将解释裸必需品脚本控件UnityGUI。
MakingControlswithUnityGUI
决策控制,UnityGUI
UnityGUIcontrolsmakeuseofaspecialfunctioncalledOnGUI().TheOnGUI()functiongetscalledeveryframeaslongasthecontainingscriptisenabled-justliketheUpdate()function.
UnityGUI控制使用的特殊功能要求OnGUI()。
该OnGUI()函数得到所谓的每一帧,只要含有脚本启用-就像Update()函数。
GUIcontrolsthemselvesareverysimpleinstructure.Thisstructureisevidentinthefollowingexample.
图形用户界面控制本身是非常简单的结构。
这种结构体现在下面的例子。
/*Examplelevelloader*/
functionOnGUI(){
//Makeabackgroundbox
GUI.Box(Rect(10,10,100,90),"LoaderMenu");
//Makethefirstbutton.Ifitispressed,Application.Loadlevel
(1)willbeexecuted
if(GUI.Button(Rect(20,40,80,20),"Level1")){
Application.LoadLevel
(1);
}
//Makethesecondbutton.
if(GUI.Button(Rect(20,70,80,20),"Level2")){
Application.LoadLevel
(2);
}
}
Thisexampleisacomplete,functionallevelloader.Ifyoucopy/pastethisscriptandattachitaGameObject,you'llseethefollowingmenuappearinwhenyouenterPlayMode:
这个例子是一个完整的,功能级别装载机。
如果您复制/粘贴此脚本,并附加了GameObject,您会看到下面的菜单出现在当您进入播放模式:
TheLoaderMenucreatedbytheexamplecode
装载机菜单创建的示例代码
Let'stakealookatthedetailsoftheexamplecode:
让我们来看看详细的示例代码
ThefirstGUIline,GUI.Box(Rect(10,10,100,90),"LoaderMenu");displaysaBoxControlwiththeheadertext"LoaderMenu".ItfollowsthetypicalGUIControldeclarationschemewhichwe'llexploremomentarily.
第一桂线,GUI.Box(矩形(10,10,100,90),“装载机菜单”);显示一个框控件的标题文字“装载机菜单”。
它使用的是典型的图形用户界面控制宣言计划,我们将探讨瞬间。
ThenextGUIlineisaButtonControldeclaration.NoticethatitisslightlydifferentfromtheBoxControldeclaration.Specifically,theentireButtondeclarationisplacedinsideanifstatement.WhenthegameisrunningandtheButtonisclicked,thisifstatementreturnstrueandanycodeinsidetheifblockisexecuted.
下一个桂线是一个Button控件的声明。
请注意,这是稍有不同的方块控制宣言。
具体来说,整个按钮宣言是放在if语句。
当游戏运行,按下按钮,这个if语句返回true和任何代码块内,如果被执行。
SincetheOnGUI()codegetscalledeveryframe,youdon'tneedtoexplicitlycreateordestroyGUIcontrols.ThelinethatdeclarestheControlisthesameonethatcreatesit.IfyouneedtodisplayControlsatspecifictimes,youcanuseanykindofscriptinglogictodoso.
自OnGUI()的代码被称为每一帧,您不必创建或销毁明确的GUI控制。
该行宣布管制是同一个创建它。
如果您需要显示控制在特定时间,您可以使用任何类型的脚本逻辑这样做
/*Flashingbuttonexample*/
functionOnGUI(){
if(Time.time%2<1){
if(GUI.Button(Rect(10,10,200,20),"Meettheflashingbutton")){
print("Youclickedme!
");
}
}
}
Here,GUI.Button()onlygetscalledeveryothersecond,sothebuttonwillappearanddisappear.Naturally,theusercanonlyclickitwhenthebuttonisvisible.
在这里,GUI.Button()只得到所谓的其他每一秒,这样的按钮,就会出现和消失。
当然,用户可以只按一下按钮时,它是可见的。
Asyoucansee,youcanuseanydesiredlogictocontrolwhenGUIControlsaredisplayedandfunctional.NowwewillexplorethedetailsofeachControl'sdeclaration.
正如你所看到的,您可以使用任何想要的逻辑来控制控件的显示界面和功能。
现在,我们将探讨细节每个控制的宣言。
AnatomyofaControl
剖析控制
TherearethreekeypiecesofinformationrequiredwhendeclaringaGUIControl:
有三个关键件所需的信息时宣布的GUI控制:
Type(Position,Content)类(位置、内容)
Observethatthisstructureisafunctionwithtwoarguments.We'llexplorethedetailsofthisstructurenow.
观察到这种结构是一个功能有两个论点。
我们将探讨这个结构的细节现在。
Type类
TypeistheControlType,andisdeclaredbycallingafunctioninUnity'sGUIclassortheGUILayoutclass,whichisdiscussedatlengthintheLayoutModessectionoftheGuide.Forexample,GUI.Label()willcreateanon-interactivelabel.Allthedifferentcontroltypesareexplainedlater,intheControlssectionoftheGuide.
一种是控制型,并宣布致电功能的统一的GUI类或GUILayout级,这是详细讨论了在布局模式部分的指南。
例如,GUI.Label()将创建一个非交互式标签。
所有不同的控制类型的解释后,在控制部分的指南。
Position位置
ThePositionisthefirstargumentinanyGUIControlfunction.TheargumentitselfisprovidedwithaRect()function.Rect()definesfourproperties:
left-mostposition,top-mostposition,totalwidth,totalheight.Allofthesevaluesareprovidedinintegers,whichcorrespondtopixelvalues.AllUnityGUIcontrolsworkinScreenSpace,whichistheresolutionofthepublishedplayerinpixels.
的位置是第一个参数在任何GUI控制功能。
这个论点本身是提供一个矩形()函数。
矩形()定义四个属性:
最左边的位置,最顶层的位置,总宽,总高度。
所有这些价值观提供了整数,相对应的像素值。
所有UnityGUI控制工作的屏幕空间,这是解决出版的球员像素。
Thecoordinatesystemistop-leftbased.Rect(10,20,300,100)definesaRectanglethatstartsatcoordinates:
0,20andendsatcoordinates310,120.ItisworthrepeatingthatthesecondpairofvaluesinRect()aretotalwidthandheight,notthecoordinateswherethecontrolsend.Thisiswhytheexamplementionedaboveendsat310,120andnot300,100.
坐标系是左上角的基础。
矩形(10,20,300,100)定义了一个长方形始于坐标:
0,20和终点坐标310120。
值得重申的是,第二次对价值观的矩形()的总宽度和高度,而不是协调的控制目标。
这就是上面提到的例子结束310120,而不是三十零点○一万。
YoucanusetheScreen.widthandScreen.heightpropertiestogetthetotaldimensionsofthescreenspaceavailableintheplayer.Thefollowingexamplemayhelpclarifyhowthisisdone:
您可以使用Screen.width和Screen.height性能得到总面积的屏幕空间提供的播放器。
下面的例子也许有助于说明如何做到这一点:
Screen屏幕
可以解决不同的屏幕、不同的分辨率(自己分析的)
/*Screen.width&Screen.heightexample*/
functionOnGUI(){
GUI.Box(Rect(0,0,100,50),"Top-left");
GUI.Box(Rect(Screen.width-100,0,100,50),"Top-right");
GUI.Box(Rect(0,Screen.height-50,100,50),"Bottom-right");
GUI.Box(Rect(Screen.width-100,Screen.height-50,100,50),"Bottom-left");
}
TheBoxespositionedbytheaboveexample
箱子的位置由上面的例子
Content内容
ThesecondargumentforaGUIControlistheactualcontenttobedisplayedwiththeControl.MostoftenyouwillwanttodisplaysometextoranimageonyourControl.Todisplaytext,passastringastheContentargumentlikethis:
第二个参数为一个GUI控制是实际的内容,以显示与控制。
最经常会要显示一些文字或图片在您的控制。
要显示文字,通过一个字符串参数的内容如下所示:
/*StringContentexample*/
functionOnGUI(){
GUI.Label(Rect(0,0,100,50),"ThisisthetextstringforaLabelControl");
}
Todisplayanimage,declareaTexture2Dpublicvariable,andpassthevariablenameasthecontentargumentlikethis:
要显示的图像,宣布Texture2D公共变量,并通过变量名称为内容的论点如下所示:
/*Texture2DContentexample*/
varcontrolTexture:
Texture2D;
functionOnGUI(){
GUI.Label(Rect(0,0,100,50),controlTexture);
}
Hereisanexampleclosertoareal-worldscenario:
下面是一个例子更接近真实世界的情景
/*ButtonContentexamples*/
varicon:
Texture2D;
functionOnGUI(){
if(GUI.Button(Rect(10,10,100,50),icon)){
print("youclickedtheicon");
}
if(GUI.Button(Rect(10,70,100,20),"Thisistext")){
print("youclickedthetextbutton");
}
}
TheButtonscreatedbytheaboveexample
按钮所造成的上述例子
ThereisathirdoptionwhichallowsyoutodisplayimagesandtexttogetherinaGUIControl.YoucanprovideaGUIContentobjectastheContentargument,anddefinethestringandimagetobedisplayedwithintheGUIContent.
还有第三个选项,您可以显示图片和文字一起在一个GUI控制。
你可以提供一个GUIContent对象的内容的论点,并确定了字符串和图像显示的GUIContent。
/*UsingGUIContenttodisplayanimageandastring*/
varicon:
Texture2D;
functionOnGUI(){
GUI.Box(Rect(10,10,100,50),GUIContent("Thisistext",icon));
}
YoucanalsodefineaTooltipintheGUIContent,anddisplayitelsewhereintheGUIwhenthemousehoversoverit.
您还可以定义工具提示的GUIContent,并显示它在其他地方的GUI当老鼠盘旋权。
/*UsingGUIContenttodisplayatooltip*/
functionOnGUI(){
//Thislinefeeds"Thisisthetooltip"intoGUI.tooltip
GUI.Button(Rect(10,10,100,20),GUIContent("Clickme","Thisisthetooltip"));
//ThislinereadsanddisplaysthecontentsofGUI.tooltip
GUI.Label(Rect(10,40,100,20),gui.tooltip);
}
Ifyou'redaringyoucanalsouseGUIContenttodisplayastring,anicon,andatooltip!
如果你是大胆的,你也可以用GUIContent显示字符串,图标,并提示!
/*UsingGUIContenttodisplayanimage,astring,andatooltip*/
varicon:
Texture2D;
functionOnGUI(){
GUI.Button(Rect(10,10,100,20),GUIContent("Clickme",icon"Thisisthetooltip"));
GUI.Label(Rect(10,40,100,20),gui.tooltip);
}
ThescriptingreferencepageforGUIContent'sconstructorforanextensivelistofexamples.
脚本参考网页GUIContent的构造进行了广泛的清单,列举
Controls控制、管理
ReferenceManual>GUIScriptingGuide>Controls
ControlTypes
ThereareanumberofdifferentGUIControlsthatyoucancreate.ThissectionlistsalloftheavailabledisplayandinteractiveControls.ThereareotherGUIfunctionsthataffectlayoutofControls,whicharedescribedintheLayoutsectionoftheGuide.
有一些不同的图形用户界面控件,您可以建立。
本节列出了所有可用的显示和交互控制。
还有其他的图形用户界面功能,影响布局的控制,这说明在布局节指南。
Label标签,
TheLabelisnon-interactive.Itisfordisplayonly.Itcannotbeclickedorotherwisemoved.Itisbestfordisplayinginformationonly.
标签非交互式。
它是为只显示。
它不能被点击或以其他方式移动。
这是最好的展示仅供参考。
/*GUI.Labelexample*/
functionOnGUI(){
GUI.Label(Rect(25,25,100,30),"Label");
}
TheLabelcreatedbytheexamplecode
Button按钮
TheButtonisatypicalinteractivebutton.Itwillrespondasingletimewhenclicked,nomatterhowlongthemouseremainsdepressed.Theresponseoccursassoonasthemousebuttonisreleased.
按钮是一个典型的互动式按钮。
这将回应一个单一的时候点击,无论多长的老鼠仍然低迷。
发生的反应尽快鼠标按钮被释放。
BasicUsage基本用法
InUnityGUI,Buttonswillreturntruewhentheyareclicked.ToexecutesomecodewhenaButtonisclicked,youwrapthetheGUI.Buttonfunctioninanifstatement.InsidetheifstatementisthecodethatwillbeexecutedwhentheButtonisclicked.
在UnityGUI,按钮将返回真正当他们点击。
当按下Button时,会执行一些代码,你的总结的GUI.Button功能if语句。
在if语句的代码,将被处死时,按下按钮。
/*GUI.Buttonexample*/
functionOnGUI(){
if(GUI.Button(Rect(25,25,100,30),"Button")){
//ThiscodeisexecutedwhentheButtonisclicked
}
}
TheButtoncreatedbytheexamplecode
RepeatButton重复按钮
RepeatButtonisavariationoftheregularButton.Thediffe