Unity 常用脚本:OnGUI(一)GUI(借鉴大神,仅作为笔记用)

OnGUI是Unity中通过代码驱动的GUI系统

主要用来创建调试工具、创建自定义属性面板、创建新的Editor窗口和工具达到扩展编辑器效果。

OnGUI不建议使用在项目UI中。

布局上,坐标系原点在屏幕左上角。

https://docs.unity3d.com/Manual/gui-Controls.html
Box矩形框

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
GUI.Box(new Rect(10,10,100,90),"看这里!");
}
}

 

 Button按钮

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
if (GUI.Button(new Rect(20, 40, 80, 20), "点这里!"))
{
Debug.Log("OK");
}
}
}

 

 Label标签

标签是非交互式的。只供展览。无法单击或以其他方式移动。最好只显示信息。 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
GUI.Label(new Rect(25, 25, 100, 30), "Label");
}
}

 

 RepeatButton按住后会重复执行单击操作的按钮

点击按钮时,每帧执行一次。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
if (GUI.RepeatButton(new Rect(25, 25, 100, 30), "RepeatButton"))
{
Debug.Log("OK");
}
}
}

 

 TextField单行输入框

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private string textFieldString = "text field";
void OnGUI()
{
textFieldString = GUI.TextField(new Rect(25, 25, 100, 30), textFieldString);
}
}

 

  TextArea多行输入框

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private string textAreaString = "text area \ntext area";

void OnGUI()
{
textAreaString = GUI.TextArea(new Rect(25, 25, 100, 60), textAreaString);
}
}

 

 Toggle单选框

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private bool toggleBool = true;

void OnGUI()
{
toggleBool = GUI.Toggle(new Rect(25, 25, 100, 30), toggleBool, "Toggle");
}
}

 

 Toolbar工具栏

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private int toolbarInt = 0;
private string[] toolbarStrings = { "Toolbar1", "Toolbar2", "Toolbar3" };

void OnGUI()
{
toolbarInt = GUI.Toolbar(new Rect(25, 25, 250, 30), toolbarInt, toolbarStrings);
}
}

 

 SelectionGrid网格选择形式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private int selectionGridInt = 0;
private string[] selectionStrings = { "Grid 1", "Grid 2", "Grid 3", "Grid 4" };

void OnGUI()
{
selectionGridInt = GUI.SelectionGrid(new Rect(25, 25, 300, 60), selectionGridInt, selectionStrings, 2);
}
}

 

 HorizontalSlider水平滑动条

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private float hSliderValue = 0.0f;

void OnGUI()
{
hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);
}
}

 

 VerticalSlider垂直滑动条

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private float vSliderValue = 0.0f;

void OnGUI()
{
vSliderValue = GUI.VerticalSlider(new Rect(25, 25, 100, 30), vSliderValue, 10.0f, 0.0f);
}
}

 

  HorizontalScrollbar水平滚动条

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private float hScrollbarValue;

void OnGUI()
{
hScrollbarValue = GUI.HorizontalScrollbar(new Rect(25, 25, 100, 30), hScrollbarValue, 1.0f, 0.0f, 10.0f);
}
}

 

 VerticalScrollbar垂直滚动条

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private float vScrollbarValue;

void OnGUI()
{
vScrollbarValue = GUI.VerticalScrollbar(new Rect(25, 25, 100, 30), vScrollbarValue, 1.0f, 10.0f, 0.0f);
}
}

 

 ScrollView滚动视图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private Vector2 scrollViewVector = Vector2.zero;
private string innerText = "I am inside the ScrollView";

void OnGUI()
{
// Begin the ScrollView
scrollViewVector = GUI.BeginScrollView(new Rect(25, 25, 100, 100), scrollViewVector, new Rect(0, 0, 400, 400));

// Put something inside the ScrollView
innerText = GUI.TextArea(new Rect(0, 0, 400, 400), innerText);

// End the ScrollView
GUI.EndScrollView();

}
}

 

 Window窗口,控件的可拖动容器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private Rect windowRect = new Rect(20, 20, 120, 50);

void OnGUI()
{
windowRect = GUI.Window(0, windowRect, WindowFunction, "My Window");
}

void WindowFunction(int windowID)
{
// Draw any Controls inside the window here
Debug.Log("WindowFunction");
}

}

 

 changed判断数据变化

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
private float hSliderValue = 0.0f;

void OnGUI()
{
hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0f, 10.0f);

if (GUI.changed)
{
Debug.Log(hSliderValue);
}
}
}

BeginGroup/EndGroup

分组

组内成员坐标相对于组的坐标。 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestOnGUI : MonoBehaviour
{
void OnGUI()
{
GUI.BeginGroup(new Rect(Screen.width/2-50,Screen.height/2-50,300,300));
GUI.Box(new Rect(0,0,200,200),"我的界面");
GUI.Button(new Rect(20, 50, 100, 30),"我是个按钮!");
GUI.EndGroup();
}
}

 

 

http://www.cnblogs.com/sanyejun/p/8483759.html

https://blog.csdn.net/u011480667/article/details/77542226

转载于:https://www.cnblogs.com/Damon-3707/p/11506640.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值