[Unity]界面GUI程序常用的脚本类

本文详细介绍了 Unity 中 GUI 控件的使用方法,包括按钮、文本框、滑块等多种控件的功能及实现方式,并展示了如何利用这些控件进行交互设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

脚本总索引:http://game.ceeger.com/Script/index.Classes.html

1 按钮生成
function OnGUI () {
	
	if (GUI.Button (Rect (10,10,150,100), "I am a button")) {
		
		print ("You clicked the button!");

	}
}



2 按钮场景载入

/* Example level loader */

function OnGUI () {

	// Make a background box

	GUI.Box (Rect (10,10,100,90), "Loader Menu");

	// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed

	if (GUI.Button (Rect (20,40,80,20), "Level 1")) {

		Application.LoadLevel (1);

	}

	// Make the second button.

	if (GUI.Button (Rect (20,70,80,20), "Level 2")) {

		Application.LoadLevel (2);

	}

}



3 按钮点击生效时间
/* Flashing button example */

function OnGUI () {

	if (Time.time % 2 < 1) {

		if (GUI.Button (Rect (10,10,200,20), "Meet the flashing button")) {

			print ("You clicked me!");

		}

	}

}




4 创建按钮背景BOX
/* Screen.width & Screen.height example */

function OnGUI () {

	GUI.Box (Rect (0,0,100,50), "Top-left");                             
	//Rect 生成2D矩形的函数,用于摄像机,画面,GUI

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

}


5 在按钮上显示文字
/* String Content example */

function OnGUI () {

	GUI.Label (Rect (0,0,100,50), "This is the text string for a Label Control");     
	//显示文字

}



6 显示图像,声明一个公共变量的Texture2D,并通过这样的内容作为参数变量的名称
/* Texture2D Content example */

var controlTexture : Texture2D;      //controlTexture为图像的名称

function OnGUI () {

	GUI.Label (Rect (0,0,100,50), controlTexture);

}




7 显示图像的例子
/* Button Content examples */

var icon : Texture2D;

function OnGUI () {

	if (GUI.Button (Rect (10,10, 100, 50), icon)) {

		print ("you clicked the icon");

	}

	if (GUI.Button (Rect (10,70, 100, 20), "This is text")) {

		print ("you clicked the text button");

	}

}




8 显示在一个图形用户界面控制的图像和文字在一起。可以为内容提供一个参数GUIContent对象,并定义字符串和图像显示的是在GUIContent

/* Using GUIContent to display an image and a string */

var icon : Texture2D;

function OnGUI () {

	GUI.Box (Rect (10,10,100,50), GUIContent("This is text", icon));

}



9 还可以定义在GUIContent工具提示,当他鼠标停留在按钮上时显示提示

/* Using GUIContent to display a tooltip */

function OnGUI () {

	// This line feeds "This is the tooltip" into GUI.tooltip

	GUI.Button (Rect (10,10,100,20), GUIContent ("Click me", "This is the tooltip"));

	// This line reads and displays the contents of GUI.tooltip

	GUI.Label (Rect (10,40,100,20), GUI.tooltip);

}



10 也可以使用GUIContent来显示字符串,图标,工具提示
/* Using GUIContent to display an image, a string, and a tooltip */

var icon : Texture2D;

function OnGUI () {

	GUI.Button (Rect (10,10,100,20), GUIContent ("Click me", icon "This is the tooltip"));

	GUI.Label (Rect (10,40,100,20), GUI.tooltip);

}




11 当鼠标停留在按钮上时显示提示【调用函数为GUI.tooltip】

function OnGUI () {

	// Make a button using a custom GUIContent parameter to pass in the tooltip.

	GUI.Button (Rect (10,10,100,20), GUIContent ("Click me", "This is the tooltip"));

	// Display the tooltip from the element that has mouseover or keyboard focus

	GUI.Label (Rect (10,40,100,40), GUI.tooltip);

}



12 GUI 显示样式

static function Button (position : Rect, content : GUIContent, style : GUIStyle) : bool   
//bool 布尔类型 【1 or 0】



13 也可以使用GUIContent来显示字符串,图标,工具提示
/* Using GUIContent to display an image, a string, and a tooltip */

var icon : Texture2D;

function OnGUI () {

	GUI.Button (Rect (10,10,100,20), GUIContent ("Click me", icon "This is the tooltip"));

	GUI.Label (Rect (10,40,100,20), GUI.tooltip);      // GUI.tooltip用来显示提示的函数

}



14 GUI设置样式

var mystyle:GUIStyle;

function OnGUI(){

	GUI.Button(Rect(10,10,100,20),"0",mystyle);

}



15 Lable标签非交互式。它是仅用于显示。它不能被点击或以其他方式移动。最好是只显示信息

/* GUI.Label example */

function OnGUI () {

	GUI.Label (Rect (25, 25, 100, 30), "Label");

}



16 Button是一个典型的互动式按钮。按钮包裹在一个if语句的GUI.Button功能。在if语句中是将要执行的按钮被点击时的代码

/* GUI.Button example */

function OnGUI () {

	if (GUI.Button (Rect (25, 25, 100, 30), "Button")) {

	// This code is executed when the Button is clicked

	}

}



17 RepeatButton点击和松开Button分别触发,普通的Button只有一次触发

/* GUI.RepeatButton example */

function OnGUI () {

	if (GUI.RepeatButton (Rect (25, 25, 100, 30), "RepeatButton")) {

		// This code is executed every frame that the RepeatButton remains clicked

	}

}



18 TextField为显示函数,可以编辑显示中的字符串 【单行显示】

/* GUI.TextField example */

var textFieldString = "text field";

function OnGUI () {

	textFieldString = GUI.TextField (Rect (25, 25, 100, 30), textFieldString);

}



19 TextArea 可编辑一个包含多行文本的字符串并显示 【多行显示】

/* GUI.TextArea example */

var textAreaString = "text area";

function OnGUI () {

	textAreaString = GUI.TextArea (Rect (25, 25, 100, 30), textAreaString);

}


20 Toggle函数,建立一个复选框,用户通过点击复选框,来切换开关状态,同时根据点击返回布尔真假值

/* GUI.Toggle example */

var toggleBool = true;

function OnGUI () {

	toggleBool = GUI.Toggle (Rect (25, 25, 100, 30), toggleBool, "Toggle");

}



21 Toolbar函数,在工具栏是通过整数积极跟踪按钮。因此必须提供给函数参数一个整数。为了使工具栏的互动,必须指定整数函数的返回值。数组中的内容将决定显示在工具栏上所提供的按钮数量。点击按钮时,按钮一直处于被激活状态,直到点击另一个按钮。

/* GUI.Toolbar example */

var toolbarInt = 0;

var toolbarStrings : String[] = ["Toolbar1", "Toolbar2", "Toolbar3"];

function OnGUI () {

	toolbarInt = GUI.Toolbar (Rect (25, 25, 250, 30), toolbarInt, toolbarStrings);

}



22 SelectionGrid函数,控制一个多行的工具栏,可以改变按钮的列数和行数,只有一个按钮可以处于激活状态。SelectionGrid按钮是通过跟踪一个整数。因此必须提供的函数参数中的一个整数。为了使SelectionGrid互动性,必须指定整数函数的返回值。在数组中的内容是在SelectionGrid显示的按钮的数量。还可以通过函数的参数决定的列数。
/* GUI.SelectionGrid example */

var selectionGridInt : int = 0;

var selectionStrings : String[] = ["Grid 1", "Grid 2", "Grid 3", "Grid 4"];

function OnGUI () {

	selectionGridInt = GUI.SelectionGrid (Rect (25, 25, 100, 30), selectionGridInt, selectionStrings, 2);   

}


23 HorizontalSlider 横向滑动函数,可以拖动到预定的变化之间的最大值和最小值。滑块的旋钮位置存储为一个浮动。要显示旋钮的位置,由于在函数的参数之一浮动。需要提供有两种确定最低和最高的值。如果想滑块旋钮可调,分配给滑块函数返回值。
/* Horizontal Slider example */

var hSliderValue : float = 0.0;

function OnGUI () {

	hSliderValue = GUI.HorizontalSlider (Rect (25, 25, 100, 30), hSliderValue, 0.0, 10.0);

}      


24 VerticalSlider 垂直滑动函数, 可以拖动到预定的变化之间的最小值和最大值。滑块的旋钮位置存储为一个浮动。要显示旋钮的位置,由于在函数的参数之一浮动。需要提供有两种确定最低和最高值。如果想滑块旋钮可调,分配给滑块函数返回值。
/* Vertical Slider example */

var vSliderValue : float = 0.0;

function OnGUI () {

	vSliderValue = GUI.VerticalSlider (Rect (25, 25, 100, 30), vSliderValue, 10.0, 0.0);

}



25 HorizontalScrollbar横向滚动函数,控制是类似于滑块控制,但视觉类似于Web浏览器或字处理器滚动元素。这种控制用于导航滚动控制。实现相同的水平滚动条和水平滑块一个例外:有一个额外的参数,它控制旋钮本身的滚动条宽度。
/* Horizontal Scrollbar example */

var hScrollbarValue : float;

function OnGUI () {

	hScrollbarValue = GUI.HorizontalScrollbar (Rect (25, 25, 100, 30), hScrollbarValue, 1.0, 0.0, 10.0);

}



26 VerticalScrollbar纵向滚动函数,控制是类似于滑块控制,但视觉类似于Web浏览器或字处理器滚动元素。这种控制用于导航滚动控制。垂直滚动实施垂直滑块相同,但有一个例外:有一个额外的参数,它控制旋钮的滚动条本身的高度。
/* Vertical Scrollbar example */

var vScrollbarValue : float;

function OnGUI () {

	vScrollbarValue = GUI. VerticalScrollbar (Rect (25, 25, 100, 30), vScrollbarValue, 1.0, 10.0, 0.0);  

   // vScrollbarValue =中的vScrollbarValue为返回值,括号内的vScrollbarValue为设置滑块的滑动速度

}



27 ScrollViews函数,显示一个更大的可视区域且可滚动。ScrollViews需要两个Rect作为参数。第一个Rect定义滚动屏幕上的可视区域的位置和大小。第二个Rect定义的可视区域内所载的空间的大小。如果可视区域内的空间是更大的,滚动条会显示为适当。还必须指定并提供二维向量,它存储可视区域显示的位置。
/* ScrollView example */

var scrollViewVector : Vector2 = Vector2.zero;  // Vector2是一个二维向量,Vector2.zero是二维向量的X轴Y轴的值都为0

var innerText : String = "I am inside the ScrollView";   //String类,初始化字符串

function OnGUI () {

	// Begin the ScrollView

	scrollViewVector = GUI.BeginScrollView (Rect (25, 25, 100, 100), scrollViewVector, Rect (0, 0, 400, 400));

	// Put something inside the ScrollView

	innerText = GUI.TextArea (Rect (0, 0, 400, 400), innerText);

	// End the ScrollView

	GUI.EndScrollView();

}


28 Window函数,建立窗口。它们的实现稍微不同于其他控件。每个窗口都有一个ID号。Window是唯一需要一个额外的功能才能正常工作的控件。你必须提供一个ID号码和一个函数的名称必须在窗口中执行。在窗口的函数中,可以建立实际的行为或包含的控件。

GUI.Window调用格式


static function Window (id : int, clientRect : Rect, func : WindowFunction, title : GUIContent, style : GUIStyle) : Rect

/* Window example */

var windowRect : Rect = Rect (20, 20, 120, 50);

function OnGUI () {

windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window");

}

function WindowFunction (windowID : int) {

// Draw any Controls inside the window here

}



29 为了检测用户是否有GUI操作,如果用户没有任何的GUI操作(点击一个按钮,拖动滑块等),将会从脚本中读取GUI.changed的值。当用户要做一些事情的时候,GUI.changed的值将被设置为真,使验证用户的输入变的很容易。

一个在工具栏里常见的情景,在工具栏里的按钮被点击的时候,你想要在哪里改变一个具体的值。你不想在每次调用OnGUI()赋值,只想当按钮中的其中一个被点击的时候调用OnGUI()。

题外话,我个人对这段话的理解是,工具栏上有很多按钮,每个按钮可以用来改变不同的数值,我们可以利用GUI.changed函数直接去改变我们想改变的值,而不用在每次点击的时候都要赋值给OnGUI,然后再调用。

关于帮助文档里GUI.changed的一些资料也给大家找来了

这里是调用的范例:static var changed : bool

对这个的说明是,它可以控制和改变任何输入的值


/* GUI.changed example */

private var selectedToolbar : int = 0;

private var toolbarStrings = ["One", "Two"];

function OnGUI () {

	// Determine which button is active, whether it was clicked this frame or not

	selectedToolbar = GUI.Toolbar (Rect (50, 10, Screen.width - 100, 30), selectedToolbar, toolbarStrings);

	// If the user clicked a new Toolbar button this frame, we'll process their input

	if (GUI.changed)

	{

		print ("The toolbar was clicked");

		if (selectedToolbar == 0)

		{

			print ("First button was clicked");

		}

		else

		{

			print ("Second button was clicked");

		}

	}

}



如果任何GUI在程序自动控制之前是由用户在操作的话,那么GUI.changed将返回true。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值