当前位置:网站首页>【Unity编辑器扩展基础】、GUI
【Unity编辑器扩展基础】、GUI
2022-06-28 12:00:00 【Unique_849997563】
一、静态属性
1、颜色
改变背景颜色:GUI.backgroundColor
改变内容颜色: GUI.contentColor
改变内容改变内容和背景颜色: GUI.color
//改变背景颜色
GUI.backgroundColor = Color.yellow;
GUI.Button(new Rect(0, 0, 200, 30), "改变背景颜色");
GUI.backgroundColor = Color.white;
GUI.Button(new Rect(0, m_interval, 200, 30), "改变背景颜色");
//改变内容颜色
GUI.contentColor = Color.yellow;
GUI.Button(new Rect(0, m_interval * 2, 200, 30), "改变内容颜色");
GUI.contentColor = Color.white;
GUI.Button(new Rect(0, m_interval * 3, 200, 30), "改变内容颜色");
//改变内容和背景颜色
GUI.color = Color.yellow;
GUI.Button(new Rect(0, m_interval * 4, 200, 30), "改变内容和背景颜色");
GUI.color = Color.white;
GUI.Button(new Rect(0, m_interval * 5, 200, 30), "改变内容和背景颜色");
2、文本输入框:GUI.TextField
GUI.changed:如果有输入控件的值发生改变,就会返回true。
stringToEdit = GUI.TextField(new Rect(0, m_interval * 6, 200, 20), stringToEdit, 25);
if (GUI.changed)
Debug.Log("GUI.TextField 内容有修改");
3、 GUI的激活状态:GUI.enabled
GUI.enabled控制之后的GUI的激活状态,未激活的GUI不能接收事件。
toggleGroup = GUI.Toggle(new Rect(0, m_interval * 7, 200, 20), toggleGroup, "ToggleGroup");
GUI.enabled = toggleGroup;
if (GUI.Button(new Rect(0, m_interval * 8, 200, 30), "测试toggleGroup的按钮1"))
{
Debug.Log("点击了button1");
}
if (GUI.Button(new Rect(0, m_interval * 9, 200, 30), "测试toggleGroup的按钮2"))
{
Debug.Log("点击了button2");
}
4、GUI.depth
5、GUI.matrix
6、GUI.skin
二、静态方法
1、GUI.Label
GUI.Label(new Rect(210, 10, 100, 20), "Hello World!");
2、 GUI.Box
GUI.Box(new Rect(210, 50, 50, 50), "A BOX");
3、GUI.Button
if (GUI.Button(new Rect(210, 110, 70, 30), "A button"))
Debug.Log("点击了按钮!");
4、 GUI.BeginGroup
m_beginGroupRect = new Rect(0, 0, Screen.width / 2, Screen.height / 2);
//BeginGroup 可以用来管理UI,UGUI的Panel,组里面的UI元素是相对于组创建的。
GUI.BeginGroup(m_beginGroupRect);
GUI.Box(new Rect(m_beginGroupRect), "自适应的 BeginGroup 测试!");
GUI.EndGroup();
5、GUI.BeginScrollView
//滑动区域
scrollPosition = GUI.BeginScrollView(new Rect(10, 10, 200, 200), scrollPosition, new Rect(0, 0, 220, 200));
GUI.Button(new Rect(0, 0, 100, 20), "Top-left");
GUI.Button(new Rect(120, 0, 100, 20), "Top-right");
GUI.Button(new Rect(0, 180, 100, 20), "Bottom-left");
GUI.Button(new Rect(120, 180, 100, 20), "Bottom-right");
GUI.EndScrollView();
6、 GUI.DrawTexture、GUI.DrawTextureWithTexCoords
//画一个图片
if (aTexture)
{
GUI.DrawTexture(new Rect(10, 110, 110, 110), aTexture, ScaleMode.StretchToFill, true, 10.0F);
GUI.DrawTextureWithTexCoords(new Rect(10, 240, 110, 110), aTexture, new Rect(10, 240, 110, 110), false);
}
7、 GUI.SetNextControlName
为下一个控件设置控件名称。
8、 GUI.FocusControl
通过控件名称设置聚焦,设置聚焦时参数为控件名称,取消聚焦时参数为null。(EditorGUILayout.TextField 控件如果被聚焦,返回的值修改之后,不会马上刷新,需要取消聚焦才能看到刷新之后的值。)
GUI.SetNextControlName("MyTextField");
username = GUI.TextField(new Rect(10, 410, 100, 20), username);
pwd = GUI.TextField(new Rect(10, 440, 100, 20), pwd);
if (GUI.Button(new Rect(10, 470, 80, 20), "设置聚焦"))
GUI.FocusControl("MyTextField");
if (GUI.Button(new Rect(10, 500, 80, 20), "取消聚焦"))
GUI.FocusControl(null);//取消聚焦
9、GUI.HorizontalSlider、GUI.VerticalSlider、GUI.HorizontalScrollbar、GUI.VerticalScrollbar
hSliderValue = GUI.HorizontalSlider(new Rect(210, 150, 100, 30), hSliderValue, 0.0F, 10.0F);
vSliderValue = GUI.VerticalSlider(new Rect(210, 170, 100, 30), vSliderValue, 10.0F, 0.0F);
hSValue = GUI.HorizontalScrollbar(new Rect(210, 210, 100, 30), hSValue, 1.0F, 0.0F, 10.0F);
vSValue = GUI.VerticalScrollbar(new Rect(210, 230, 100, 30), vSValue, 1.0F, 10.0F, 0.0F);
10、GUI.GetNameOfFocusedControl
获取当前聚焦的控件名称,如果没有聚焦或者没有控件命名返回空字符串。
11、GUI.Window、 GUI.FocusWindow、GUI.DragWindow
GUI.BringWindowToBack、GUI.BringWindowToFront
//GUI.depth、GUI.BringWindowToBack、GUI.BringWindowToFront都可以改变窗口的层级
//GUI.BringWindowToBack GUI.BringWindowToFront
private Rect windowRect = new Rect(20, 20, 120, 120);
private Rect windowRect2 = new Rect(80, 20, 120, 120);
private Rect windowRect3 = new Rect(150, 20, 120, 120);
// GUI.depth
int guiDepth1 = 0;
int guiDepth2 = 0;
//
string m_focusedControlName = "";
void OnGUI()
{
windowRect = GUI.Window(0, windowRect, DoMyWindow1, "第一个窗口");
windowRect2 = GUI.Window(1, windowRect2, DoMyWindow2, "第二个窗口");
windowRect3 = GUI.Window(2, windowRect3, DoMyWindow3, "第三个窗口");
}
void DoMyWindow1(int windowID)
{
// GUI.depth = guiDepth1;
if (GUI.Button(new Rect(10, 20, 100, 20), "Window1"))
{
//GUI.BringWindowToBack(1);//将窗口id为1的窗口设置到最后
//guiDepth1 = 1;
//guiDepth2 = 0;
//GUI.BringWindowToFront(1);//将窗口id为1的窗口设置到最前
GUI.FocusWindow(1); //聚焦到id为1的窗口
}
//DragWindow 可拖动的窗口
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
void DoMyWindow2(int windowID)
{
//GUI.depth = guiDepth2;
if (GUI.Button(new Rect(10, 20, 100, 20), "Window2"))
{
//GUI.BringWindowToBack(2);
//guiDepth1 = 0;
//guiDepth2 = 1;
GUI.BringWindowToFront(2);
}
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
void DoMyWindow3(int windowID)
{
// GUI.depth = guiDepth2;
if (GUI.Button(new Rect(10, 20, 100, 20), "Window3"))
{
//GUI.BringWindowToBack(0);
//guiDepth1 = 0;
//guiDepth2 = 1;
GUI.BringWindowToFront(0);
}
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
边栏推荐
- 赛尔号抽奖模拟求期望
- 【vi/vim】基本使用及命令汇总
- 6.A-B
- Self use demo of basic component integration of fluent
- Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
- AGCO AI frontier promotion (2.16)
- Day28 strict mode, string JS 2021.09.22
- Day32 JS note event (Part 1) September 27, 2021
- [no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
- 2. single digit statistics
猜你喜欢
【C语言】判断三角形
Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
Day30 JS notes BOM and DOM 2021.09.24
In less than an hour, apple destroyed 15 startups
Leetcode 48. 旋转图像(可以,已解决)
SEO优化的许多好处是与流量有直接关系
Day34 JS notes regular expression 2021.09.29
Build your own website (18)
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Is it feasible to be a programmer at the age of 26?
随机推荐
Excel import / export convenience tool class
【C语言】关于scanf()与scanf_s()的一些问题
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
Chapter 2 do you remember the point, line and surface (2)
Still using simpledateformat for time formatting? Be careful that the project collapses!
Sha256 encryption tool class
Batch will png . bmp . JPEG format pictures are converted to Jpg format picture
Self use demo of basic component integration of fluent
Oracle date format exception: invalid number
携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
什么是数据合规?怎样做到数据合规?
MapReduce项目案例1
MapReduce project case 3 - temperature statistics
Usage and principle of precomputedtextcompat
Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
来吧元宇宙,果然这热度一时半会儿过不去了
Day28 strict mode, string JS 2021.09.22
Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
AcWing 610. Salary and bonus (implemented in C language)
Prefix and (one dimension)