Unity3D GUILayOut 的UI穿透解决问题,管理穿透

下次自动登录
现在的位置:
& 综合 & 正文
Unity3D中脚本语言选择的问题
Unity3D支持三种脚本语言,、C#、Bool,Bool这种语言我不熟悉、不做评论,下面就说说javaScript与C#,我本人是搞C++,javaScript与C#这二种语言以前都没有接触过,通过这段时间对Unity3D的了解,我个人比较偏向与C#,最主要的就是C#的 delegate,通过C#的delegate可以调用其他脚本,而javaScript只能通过sendMessage这种方式,Unity3D是C++写的,所以我估计SendMessage的方式应该是MFC的方式是一样的,从运行速度、和代码的结构上来讲C#都是更好的选择。
当然也不排斥javaScript,不过我是真心的不喜欢这种弱类型的语言。下面是Delegate和SendMessage之间的优化性能差距测试
using UnityE
using System.C
public class DelegateBasic : MonoBehaviour {
public delegate void MyDelegate(string arg1);
public MyDelegate myD
float timeS
bool isStartSendM
void Start () {
myDelegate += myFunciton1;
void Update () {
if(isStart )
isStart = false;
count = 0;
timeStart = Time.realtimeSinceS
Debug.Log("Start = " + timeStart );
for(int i= 0; i& 50000;i++)
if(myDelegate != null) myDelegate("");
if(isStartSendMessage)
isStartSendMessage = false;
count = 0;
timeStart = Time.realtimeSinceS
Debug.Log("Start = " + timeStart );
for(int i= 0; i& 50000;i++)
this.gameObject.SendMessage("myFunciton1","",SendMessageOptions.DontRequireReceiver );
void OnGUI()
if(GUILayout.Button("INVOKE Delegate"))
isStart = true;
if(GUILayout.Button("SendMessage"))
isStartSendMessage = true;
void myFunciton1(string s)
if(count == 50000)
Debug.Log("End = " + Time.realtimeSinceStartup );
Debug.Log("Time Spent = " + ( Time.realtimeSinceStartup - timeStart ) );
void myFunciton2(string s)
【上篇】【下篇】请尊重我的热情、
unity 基本函数整理[3_GUI控件+GUILayout界面布局]
//this.transform.Translate (Vector3.forward * Time.deltaTime);
//this.transform.Translate (Vector3.right * Time.deltaTime);
//this.transform.Translate (Vector3.up * Time.deltaTime);
//this.transform.Rotate (Vector3.up * Time.deltaTime * 5);
//this.transform.Rotate (new Vector3(0,1,0));
public Texture2D
// Use this for initialization
void Start () {
info = "hello";
void OnGUI(){
GUI.Label (new Rect(150,150,150,200),info);
* 连续按钮
if (GUI.RepeatButton (new Rect (50, 100, 150, 200), "I'm Label")) {
//GUI.Label(new Rect(200,200,20,80),"HELLO");
this.transform.Rotate (new Vector3(0,1,0));
//贴图按钮
if (GUI.Button (new Rect (50, 50, play.width, play.height), play)) {
always enter paly";
void OnGUI(){
username = GUI.TextField (new Rect(50,50,100,30),username,15);
password = GUI.PasswordField (new Rect(50,100,100,30),password,"*"[0],15);
public string[] resource = {"第一个","第二个","第三个","第四个"};
public bool s1 =
public bool s2 =
void OnGUI(){
select = GUI.Toolbar (new Rect(10,10,resource.Length * 100,30),select,resource);
switch(select){
s1 = GUI.Toggle(new Rect(10,50,150,30),s1,"一大个工具栏1");
s2 = GUI.Toggle(new Rect(10,100,150,30),s2,"一大个工具栏2");
s1 = GUI.Toggle(new Rect(10,50,150,30),s1,"二大个工具栏1");
s2 = GUI.Toggle(new Rect(10,100,150,30),s2,"二大个工具栏2");
s1 = GUI.Toggle(new Rect(10,50,150,30),s1,"三大个工具栏1");
s2 = GUI.Toggle(new Rect(10,100,150,30),s2,"三大个工具栏2");
s1 = GUI.Toggle(new Rect(10,50,150,30),s1,"四大个工具栏1");
s2 = GUI.Toggle(new Rect(10,100,150,30),s2,"四大个工具栏2");
VerticalSlider(滑动杆):
public float verticalvalue = 0;
public float horizontalvalue = 0;
void OnGUI(){
horizontalvalue = GUI.HorizontalSlider (new Rect(20,20,100,10),horizontalvalue,100,0);
verticalvalue = GUI.VerticalSlider (new Rect(50,50,30,100),verticalvalue,0,100);
GUI.Label (new Rect(100,100,200,30),horizontalvalue + "
" + verticalvalue);
}ScrollView视图(游戏的开始菜单说明):
public Vector2 scrollP
void Start () {
//滚动条中的:滚动按钮相对于整个滚动按钮的位置
scrollPosition.x = 0;
scrollPosition.y = 0;
void OnGUI(){
//怎么说呢?或许只有整理过一遍才算是学习过了一遍吧···
//第一个参数:滚动显示视图的范围
//第二个参数:设置滚动条中的滚动按钮相对于滚动条的位置
//第三个参数:整个滚动视图大小
//第四个、第五个参数皆为true时,表示仅当内容超过时才显示滚动条
scrollPosition = GUI.BeginScrollView (new Rect (Screen.width * 0.1f, Screen.height * 0.1f, Screen.width * 0.8f, Screen.height * 0.8f), scrollPosition,
new Rect (0, 0, Screen.width * 0.8f, 300), true, true);
GUI.Label (new Rect(100,40,Screen.width,30),"test GUI.ScrollPosition
test GUI.ScrollPosition
test GUI.ScrollPosition
test GUI.ScrollPosition
GUI.Label (new Rect(100,80,Screen.width,30),"test GUI.ScrollPosition
test GUI.ScrollPosition
test GUI.ScrollPosition
test GUI.ScrollPosition
GUI.EndScrollView ();
GUI.DrawTexture
用以绘制游戏界面的背景贴图
注:摆在最前面,显示的时候在最底层
public Texture2D tx1;
public Texture2D tx2;
public Texture2D
void OnGUI(){
GUI.DrawTexture (new Rect(0,0,Screen.width,Screen.height),background);
if(GUI.Button(new Rect(50,50,tx1.width,tx1.height),tx1)){
Application.LoadLevel("level_1");
if(GUI.Button(new Rect(50,100,tx2.width,tx2.height),tx2)){
Application.LoadLevel("level_1");
群组视图(GroupView)
用以设计游戏界面
注:群组内的视图永远以群组的左上角坐标作为自己的相对坐标位置另外,群组视图仅是将一群图标绑定在一起,它并无外边框,可是窗口有,请看下一条~:
public Texture2D tx1;
public Texture2D tx2;
void OnGUI(){
GUI.BeginGroup (new Rect(10,50,200,400));
GUI.DrawTexture (new Rect(0,0,50,50),tx1);
GUI.Label (new Rect(0,50,50,50),"one");
GUI.Button (new Rect(0,100,50,50),"oneButton");
GUI.EndGroup ();
GUI.BeginGroup (new Rect(210,50,200,400));
GUI.DrawTexture (new Rect(0,0,50,50),tx2);
GUI.Label (new Rect(0,50,50,50),"TWO");
GUI.Button (new Rect(0,100,50,50),tx2);
GUI.EndGroup ();
GUI.Box (new Rect(0,0,150,150),"窗口ID:");
void OnGUI(){
GUI.Window (0,new Rect(20,20,200,200),oneWindow,"first window");
GUI.Window (1,new Rect(250,20,200,200),oneWindow,"second window");
void oneWindow(int windowID){
GUI.Box (new Rect(0,20,150,150),"窗口ID:"+windowID);
if (GUI.Button (new Rect (0, 50, 150, 50), "Button")) {
Debug.Log("窗口ID:"+windowID);
Project : Create -&
GUI,可选择设置项:
Font: 设置字体
Box: 设置盒子
Button: 设置按钮
Toggle: 设置多选框
Label: 设置标签
Text Field: 设置单行输入框
Text Area: 设置多行输入框
Window: 设置窗口
Horizontal Slider: 设置水平滑动条
Horizontal Slider Thumb: 设置水平滑动条按钮
Vertical Slider: 设置垂直滑动条
Vertical Slider Thumb: 设置垂直滑动条按钮
Horizontal Scrollbar: 设置滚动视图水平滚动条
Horizontal Scrollbar Thumb: 设置滚动视图水平滚动条滑动按钮
Horizontal Scrollbar Left Button: 设置滚动视图水平滚动条左侧按钮
Horizontal Scrollbar Right Button:设置滚动视图水平滑动条右侧按钮
Vertical Scrollbar: 设置滚动视图垂直滑动条
Vertical Scrollbar Thumb: 设置滚动视图垂直滑动条按钮
Vertical Scrollbar Up Button: 设置滚动视图垂直滚动条上侧按钮
Vertical Scrollbar Down Button: 设置滚动视图垂直滚动条下侧按钮
Scroll View: 设置滚动视图
Custom Styles: 设置客户自定义风格
Setting: 其他的一些设置
对于这些设置下的选项有:
Normal:默认显示颜色和背景
Hover:鼠标滑动经过时的显示颜色和背景
Active:激活状态时的显示颜色和背景
Focused:获得焦点时的现实颜色和背景
On Normal:默认状态
On Hover:停留状态
On Active:激活状态
On Focused:获得焦点状态
Border:边界的设置
Padding:显示的内容与边缘按钮的偏移
Margin:设置整体位置的偏移
Overflow:原有按钮超出原有大小的距离
Font:针对本控件的字体
Image Position:图片位置
Alignment:设置内容方式
World: Wrap:是否换行
Text Clipping:文字剪切方式
Content Offset:内容偏移量
Fixed Width:边缘固定的宽度
Fixed Height:边缘固定的高度
Font Size:字体大小,默认为0
Font Style:字体风格(加粗,斜体等等)
Stretch Width:是否延伸宽度
Stretch Height:是否延伸高度
好了、最后就是设置自定义风格组件:
public GUISkin myGUI
string info = "我是要做海贼王的男人!";
void OnGUI(){
GUI.skin = myGUI
if (GUI.Button (new Rect (50, 150, 300, 50), "你好!JAY!", "Custom0")) {
info = "button effect";
OK、3-1的GUI控件到此为止,就基本结束了,接下来学习GUILayout界面布局:
3-2、GUILayout游戏布局
可以使得游戏更好地运行在不同屏幕分辨率的手机上~
string info = "Nothig";
void OnGUI(){
if (GUI.Button (new Rect (50, 50, 80, 30), info)) {
if (GUILayout.Button (info)) {
}发现:GUILayout的适应性非常好~
GUILayout提供设置的选项:
GUILayout.Width() GUILayout布局宽度
GUILayout.Height() GUILayout布局高度
GUILayout.MinWidth() GUILayout布局最小宽度
GUILayout.MinHeight() GUILayout布局最小高度
GUILayout.MaxWidth() GUILayout布局最大宽度
GUILayout.MaxHeight() GUILayout布局最大高度
GUILayout.ExpandWidth() GUILayout布局整体宽度
GUILayout.ExpandHeight()GUILayout布局整体高度
void OnGUI(){
GUILayout.Button ("我是要做海贼王的男人",GUILayout.Width(200),GUILayout.Height(30));
GUILayout.Button ("宽度不等于最宽按钮",GUILayout.ExpandWidth(false));
线性布局(水平布局or垂直布局):
void OnGUI(){
GUILayout.BeginHorizontal ();
GUILayout.Button ("我是要做海贼王的男人!");
GUILayout.Label ("我是蒙奇.路飞~");
GUILayout.EndHorizontal ();
GUILayout.BeginVertical ();
GUILayout.Button ("我是要做海贼王的男人!");
GUILayout.Label ("我是蒙奇.路飞~");
GUILayout.EndVertical ();
控件偏移:
void OnGUI(){
GUILayout.BeginArea (new Rect(0,0,200,60));
GUILayout.BeginHorizontal ();
GUILayout.BeginVertical ();
GUILayout.Box ("Test1");
GUILayout.Space (10);
GUILayout.Box ("Test3");
GUILayout.EndVertical ();
GUILayout.Space (20);
GUILayout.BeginVertical ();
GUILayout.Box ("Test2");
GUILayout.Space (10);
GUILayout.Box ("Test4");
GUILayout.EndVertical ();
GUILayout.EndHorizontal();
GUILayout.EndArea ();
对齐方式:
GUILayout.FlexibleSpace();
直接就产生对齐撑开抵满两边的效果:
GUILayout.FlexibleSpace();
窗口的实现
我勒个去,原来游戏里出来的[确认/否定]是个窗口性质
public ArrayList winArrayL
void Start(){
winArrayList = new ArrayList ();
winArrayList.Add (new Rect(winArrayList.Count * 250, 50,100,100));
void OnGUI(){
int count = winArrayList.C
for (int i = 0; i& i++) {
//GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
winArrayList[i] = GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
//winArrayList[i] = GUILayout.Window(i,,AddWindow,"");
void AddWindow(int WindowID){
GUILayout.BeginHorizontal ();
GUILayout.Label (icon,GUILayout.Width(50),GUILayout.Height(50));
GUILayout.Label ("this is a new window");
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
if (GUILayout.Button ("new a window")) {
winArrayList.Add(new Rect(winArrayList.Count * 150,50,100,100));
if (GUILayout.Button ("Remove window")) {
winArrayList.Remove(WindowID);
GUILayout.EndHorizontal ();
GUI.DragWindow (new Rect(0,0,Screen.width,Screen.height));
}---------------------------------public ArrayList winArrayL
void Start(){
winArrayList = new ArrayList ();
winArrayList.Add (new Rect(winArrayList.Count * 250, 50,100,100));
void OnGUI(){
int count = winArrayList.C
for (int i = 0; i& i++) {
//GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
winArrayList[i] = GUILayout.Window(i,(Rect)winArrayList[i],AddWindow,"WindowID: " +i);
//winArrayList[i] = GUILayout.Window(i,,AddWindow,"");
void AddWindow(int WindowID){
GUILayout.BeginHorizontal ();
GUILayout.Label (icon,GUILayout.Width(50),GUILayout.Height(50));
GUILayout.Label ("this is a new window");
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
if (GUILayout.Button ("new a window")) {
winArrayList.Add(new Rect(winArrayList.Count * 150,50,100,100));
if (GUILayout.Button ("Remove window")) {
winArrayList.Remove(WindowID);
GUILayout.EndHorizontal ();
GUI.DragWindow (new Rect(0,0,Screen.width,Screen.height));
设置字体Project:
Creat-&GUI Skin ,设置font
using UnityE
using System.C
public class Show : MonoBehaviour {
public GUISkin myGUIS
void OnGUI(){
GUI.skin = myGUIS
GUI.Label (new Rect(50,50,100,30),"王的男人!");
动态加载图片
将图片文件c.jpg放入Resources文件夹中:public Texture2D
void OnGUI(){
if(GUI.Button(new Rect(50,50,100,30),"load texture")){
txt = Resources.Load("c");
unity GUI绘制直线条
Unity3D 布局 GUILayout
Unity之路(八):GUI/GUILayout示例和Application
UnityGUI系统之Toggle、Scrollbar
[转载]unity 基本函数整理[3_GUI控件+GUILayout界面布局]
GUIlayout.space()偏移布局设置
【Unity 3D】学习笔记十二:GUILayout(界面布局)
Unity之EditorGUILayout-TextField、Toggle - 七
Unity之EditorGUILayout-显示控件禁止对控件进行操作
unity 3d GUI.Toolbar以及GUI.Toggle
没有更多推荐了,Unity 脚本手册
.Slider 滑动条
static function Slider (value : float, leftValue : float, rightValue : float, params options : []) : float
static function Slider (label : string, value : float, leftValue : float, rightValue : float, params options : []) : float
static function Slider (label : , value : float, leftValue : float, rightValue : float, params options : []) : float
Parameters参数
Optional label in front of the toggle. // 开关按钮前面的可选标签。
The value to edit. // 编辑的值
The value at the left end of the slider.
滑动条最左边的值
rightValue
The value at the right end of the slider.
滑动条最右边的值
An optional list of layout options that specify extra layouting
properties. Any values passed in here will override settings defined by
the style.
See Also: , , , , , , ,
指定额外布局属性的可选列表。这里传递任意值,将覆盖样式定义的设置。
float - The value that has been set by the user.
返回浮点数,由用户设置的值。
Description描述
Make a slider the user can drag to change a value between a min and a max.
制作一个滑动条用户可以拖动来改变值,在最小和最大值之间。
Scale the selected object between a range.
在一个范围缩放选择的物体。
// Editor script that lets you scale the selected GameObject between 1 and 100
//缩放选择的游戏物体,在1-100之间
class EditorGUILayoutSlider extends
var scale : float = 0.0;
@(&Examples/Editor GUILayout Slider usage&)
static function Init() {
var window = GetWindow(EditorGUILayoutSlider);
window.Show();
function OnGUI() {
scale = (scale,1, 100);
function OnInspectorUpdate() {
.localScale = Vector3(scale, scale, scale);
static function Slider (property : , leftValue : float, rightValue : float, params options : []) : void
static function Slider (property : , leftValue : float, rightValue : float, label : string, params options : []) : void
static function Slider (property : , leftValue : float, rightValue : float, label : , params options : []) : void
Parameters参数
Optional label in front of the toggle. // 开关按钮前面的可选标签。
The value the slider shows. This determines the position of the draggable thumb.
显示滑动条的值,可拖动滑块确定的位置。
The value at the left end of the slider.
滑动条最左边的值
rightValue
The value at the right end of the slider.
滑动条最右边的值
An optional list of layout options that specify extra layouting
properties. Any values passed in here will override settings defined by
the style.
See Also: , , , , , , ,
指定额外布局属性的可选列表。这里传递任意值,将覆盖样式定义的设置。
Description描述
Make a slider the user can drag to change a value between a min and a max.
制作一个滑动条用户可以拖动来改变值,在最小和最大值之间。但行好事 莫问前程
【Unity 3D】学习笔记十五:控件四角对齐(FlexibleSpace)
Unity 学习笔记十五
学习资料:《Unity 3D游戏开发》 宣雨松
在一般情况下,游戏的窗口都是矩形的,由左上角,右上角和左下角,右下角组成。如果要是控件分别对齐在这4个叫当中,就需要使用FlexibleSpace()方法对其偏移了。
FlexibleSpace()的原理是将两个控件完全左右或者上下对齐在游戏窗口中,它会自动的获取当前屏幕的宽度和高度保证控件不会超过游戏窗口。是一个非常重要的方法,可以在不同的分辨率下面直接确定控件的位置而且不会超出显示范围。
例子:function OnGUI()
//开始一个显示区域
GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
//开始最外层横向布局
GUILayout.BeginHorizontal ();
//嵌套一个纵向布局
GUILayout.BeginVertical ();
GUILayout.Box("左上");
//两个box之间上下对齐
GUILayout.FlexibleSpace();
GUILayout.Box("左下");
//结束嵌套的纵向局部
GUILayout.EndVertical ();
//布局之间左右对齐
GUILayout.FlexibleSpace();
//嵌套一个纵向布局
GUILayout.BeginVertical ();
GUILayout.Box("右上");
//两个box之间上下对齐
GUILayout.FlexibleSpace();
GUILayout.Box("右下");
//结束嵌套的纵向局部
GUILayout.EndVertical ();
//结束最外层横向布局
GUILayout.EndHorizontal ();
//结束显示区域
GUILayout.EndArea();
GUILayout.FlexibleSpace()使用
Unity-GUI创建控件时控制位置
Unity 3D 学习笔记
android控件的对齐方式,控件布局(顶部、底部)留着自用~~~
unity插件开发之对齐小工具
NGUI控件说明(中文) UIWidget
Unity3d NGUI 子控件之屏幕自适应
Unity3D中的对齐
没有更多推荐了,Unity3D 一键更换场景物体-腾讯游戏学院

我要回帖

更多关于 一段很现实的话 的文章

 

随机推荐