Unity为开发者提供了一套非常完美的图形化界面引擎,包括游戏窗口、文本窗口、输入框、拖动条、按钮、贴图框等。本文巩固Unity GUI图形用户界面的知识,对GUI脚本编写和场景的切换进行总结。
文章目录
- 一、界面的切换
- 1、新建场景
- 2、新建一个脚本文件:Project-〉Assets-〉右击-〉Create-〉C#,重命名为Menu
- 3、编辑Menu.cs,使其具有三个按钮,每个按钮按下时print一个信息
- 4、GUI Style的设置
- 5、导入Unity package`Techno_Blue_GUI_Skin.unitypackage`
- 6、选择GUI Skin
- 7、将Menu.cs拖拽到 Menu场景中的Main Camera ,运行,显示三个按钮
- 8、在File -〉Build Setting,弹出“Build Setting”对话框,先将Menu场景拖拽到对话框,再将test场景拖拽到对话框,在对话框的右边显示两个场景的序号分别为0、1
- 9、在Menu.cs程序中,按下 "进入游戏"按钮时的处理程序中加入语句:
- 10、在Collisions.cs程序中,按下按钮Q时的处理程序中加入语句:
- 11、在Collisions.cs程序中加入
- 12、按下按钮Q时返回主菜单,但键盘和鼠标的控制权还在游戏场景中的第三人称玩家身上,此时的鼠标不起作用 ,在Menu.cs程序中的Start() 方法
- 13、新建一个场景,保存,命名为text,新建一个C#脚本程序text.cs,并与场景text中的Main Canera关联
- 14、text.cs中添加程序
- 15、File -〉Build Setting,弹出“Build Setting”对话框,将text场景拖拽到对话框,在对话框的右边显示场景的序号为2
- 16、修改Menu.cs语句`Application.LoadLevel(2);`
- 二、界面的退出
- 三、总结
一、界面的切换
1、新建场景
在之前Scene_Shot的基础上,再新建一个场景:File-〉New Scene,File-〉Save Scene,输入场景文件名为Menu
2、新建一个脚本文件:Project-〉Assets-〉右击-〉Create-〉C#,重命名为Menu
3、编辑Menu.cs,使其具有三个按钮,每个按钮按下时print一个信息
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Menu : MonoBehaviour
{
void Start() // Start is called before the first frame update
{
Screen.lockCursor = false;
Cursor.visible = true;
}
void OnGUI()
{
GUIStyle style = new GUIStyle();
style.normal.textColor = new Color(0,0,255);
style.fontSize = 80;
style.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(680, 60, 150, 30), "狙击精英",style);
if (GUI.Button(new Rect(750, 200, 150, 30), "进入游戏"))
{
print("button1");
}
if (GUI.Button(new Rect(750, 260, 150, 30), "显示说明文档"))
{
print("button2");
}
if (GUI.Button(new Rect(750, 320, 150, 30), "退出"))
{
print("button3");
}
}
}
4、GUI Style的设置
在前面建立的Menu.cs中,在public class Menu_simple: MonoBehaviour中加入:
public GUISkin skin;
(必须在函数中的第一行,运行时自动编译,在该脚本关联的Main Camera的Inspector下方出现skin选取按钮。)
在 void OnGUI()中加入:GUI.skin = skin;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Menu : MonoBehaviour
{
public GUISkin skin;
void Start() // Start is called before the first frame update
{
Screen.lockCursor = false;
Cursor.visible = true;
}
void OnGUI()
{
GUI.skin = skin;
GUIStyle style = new GUIStyle();
style.normal.textColor = new Color(0,0,255);
style.fontSize = 80;
style.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(680, 60, 150, 30), "狙击精英",style);
if (GUI.Button(new Rect(750, 200, 150, 30), "进入游戏"))
{
print("button1");
}
if (GUI.Button(new Rect(750, 260, 150, 30), "显示说明文档"))
{
print("button2");
}
if (GUI.Button(new Rect(750, 320, 150, 30), "退出"))
{
print("button3");
}
}
}
5、导入Unity packageTechno_Blue_GUI_Skin.unitypackage
6、选择GUI Skin
7、将Menu.cs拖拽到 Menu场景中的Main Camera ,运行,显示三个按钮
8、在File -〉Build Setting,弹出“Build Setting”对话框,先将Menu场景拖拽到对话框,再将test场景拖拽到对话框,在对话框的右边显示两个场景的序号分别为0、1
9、在Menu.cs程序中,按下 "进入游戏"按钮时的处理程序中加入语句:
`Application.LoadLevel(1)`
10、在Collisions.cs程序中,按下按钮Q时的处理程序中加入语句:
`Application.LoadLevel(0) `
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Menu : MonoBehaviour
{
public GUISkin skin;
void OnGUI()
{
GUI.skin = skin;
GUIStyle style = new GUIStyle();
style.normal.textColor = new Color(0,0,255);
style.fontSize = 80;
style.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(680, 60, 150, 30), "狙击精英",style);
if (GUI.Button(new Rect(750, 200, 150, 30), "进入游戏"))
{
Application.LoadLevel(1);
}
if (GUI.Button(new Rect(750, 260, 150, 30), "显示说明文档"))
{
Debug.Log("Clicked the button with text2");
}
if (GUI.Button(new Rect(750, 320, 150, 30), "退出"))
{
Application.Quit();
}
}
}
11、在Collisions.cs程序中加入
public class Collisions : MonoBehaviour
{ .........
void OnGUI()
{ GUI.Label(new Rect(10, 10, 150, 30), "按下Q健返回主菜单");
if (Input.GetKeyDown(KeyCode.Q))
{ Debug.Log("您按下了Q键");
Application.LoadLevel(0);
}
}
}
12、按下按钮Q时返回主菜单,但键盘和鼠标的控制权还在游戏场景中的第三人称玩家身上,此时的鼠标不起作用 ,在Menu.cs程序中的Start() 方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Menu : MonoBehaviour
{
public GUISkin skin;
void Start() // Start is called before the first frame update
{
Screen.lockCursor = false;
Cursor.visible = true;
}
void OnGUI()
{
GUI.skin = skin;
GUIStyle style = new GUIStyle();
style.normal.textColor = new Color(0,0,255);
style.fontSize = 80;
style.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(680, 60, 150, 30), "狙击精英",style);
if (GUI.Button(new Rect(750, 200, 150, 30), "进入游戏"))
{
Application.LoadLevel(1);
}
if (GUI.Button(new Rect(750, 260, 150, 30), "显示说明文档"))
{
Debug.Log("Clicked the button with text2");
}
if (GUI.Button(new Rect(750, 320, 150, 30), "退出"))
{
Application.Quit();
}
}
}
这两条语句使鼠标在切换场景后生效
13、新建一个场景,保存,命名为text,新建一个C#脚本程序text.cs,并与场景text中的Main Canera关联
14、text.cs中添加程序
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class text : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{ GUI.TextArea(new Rect(680, 70, 150, 150), "该场景实现了动画、粒子系统、物体拾取、手榴弹投掷等功能,场景设置与程序控制相结合。 ");
GUI.Label(new Rect(10, 10, 150, 30), "按下Q健返回主菜单");
if (Input.GetKeyDown(KeyCode.Q))
{ Debug.Log("您按下了Q键");
Application.LoadLevel(0);
} }
}
15、File -〉Build Setting,弹出“Build Setting”对话框,将text场景拖拽到对话框,在对话框的右边显示场景的序号为2
16、修改Menu.cs语句Application.LoadLevel(2);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Menu : MonoBehaviour
{
public GUISkin skin;
void Start() // Start is called before the first frame update
{
Screen.lockCursor = false;
Cursor.visible = true;
}
void OnGUI()
{
GUI.skin = skin;
GUIStyle style = new GUIStyle();
style.normal.textColor = new Color(0,0,255);
style.fontSize = 80;
style.fontStyle = FontStyle.Bold;
GUI.Label(new Rect(680, 60, 150, 30), "狙击精英",style);
if (GUI.Button(new Rect(750, 200, 150, 30), "进入游戏"))
{
Application.LoadLevel(1);
}
if (GUI.Button(new Rect(750, 260, 150, 30), "显示说明文档"))
{
Application.LoadLevel(2);
}
if (GUI.Button(new Rect(750, 320, 150, 30), "退出"))
{
Application.Quit();
}
}
}
二、界面的退出
1、按“退出”按钮退出
界面的退出,使用函数Application.Quit()
Editor下使用UnityEditor.EditorApplication.isPlaying = false
结束退出,只有当工程打包编译后的程序使用Application.Quit()才奏效
if (GUI.Button(new Rect(750, 320, 150, 30), "退出"))
{
UnityEditor.EditorApplication.isPlaying = false;
Application.Quit();
}
2、按Q键退出
Application.Quit()在编辑器中或者在Web环境下被忽略,同样使用UnityEditor.EditorApplication.isPlaying = false
if (Input.GetKeyDown(KeyCode.Q))
{
UnityEditor.EditorApplication.isPlaying = false;
Application.Quit();
}
三、总结
- 用OnGUI()函数来调用UnityGUI控件
- OnGUI()包含在脚本组件中,和同样包含在其中的Update()函数一样,当脚本组件被激活时,在运行的每一帧中都会被调用
- GUI控件的声明需要包含三个必要的关键信息:
Type(Position,Content) - Type类型为GUI的函数,完成各种功能。常见类型有:
Botton按钮、Label标签、RepeatButton重复按钮、TextFeld文本域、TextArea文本区域、Taggle开关、Toolbar工具栏等 - Position定位,定位参数由Rect()函数生成。Rect()定义了4个 对应屏幕空间像素单位的Integer值属性,分别对应左、顶、宽、高
- Content内容,在空间中实际显示的内容
- unity为开发者提供了input库,来支持键盘事件,鼠标事件以及触摸事件。
- 一般的PC键盘有104个不同的按键,在程序中通过监听这些按键事件,从而进一步执行逻辑操作。如:射击游戏中,W表示前进,S表示后退,A表示左移,D表示右移。
- 在脚本中,用input。GetKeyDown( )方法将按键值作为参数,监听此按键是否被按下。按下返回true,否者返回false
本次项目深入理解Unity GUI图形用户界面 ,真正贴近实际生产场景开发,为以后AR/VR场景的搭建打下基础。