本文转载自:https://blog.csdn.net/peanut__love/article/details/43537483
移动
-
//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 play;
-
public
string info;
-
// 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)) {
-
info =
" always enter paly";
-
}
-
-
-
}
登陆:
-
public
string username;
-
public
string password;
-
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);
-
}
ToggleBar
-
public
string[] resource = {
"第一个",
"第二个",
"第三个",
"第四个"};
-
public
bool s1 =
false;
-
public
bool s2 =
false;
-
public
int
select;
-
void OnGUI(){
-
-
select = GUI.Toolbar (
new Rect(
10,
10,resource.Length *
100,
30),
select,resource);
-
switch(
select){
-
case
0:
-
s1 = GUI.Toggle(
new Rect(
10,
50,
150,
30),s1,
"一大个工具栏1");
-
s2 = GUI.Toggle(
new Rect(
10,
100,
150,
30),s2,
"一大个工具栏2");
-
break;
-
case
1:
-
s1 = GUI.Toggle(
new Rect(
10,
50,
150,
30),s1,
"二大个工具栏1");
-
s2 = GUI.Toggle(
new Rect(
10,
100,
150,
30),s2,
"二大个工具栏2");
-
break;
-
case
2:
-
s1 = GUI.Toggle(
new Rect(
10,
50,
150,
30),s1,
"三大个工具栏1");
-
s2 = GUI.Toggle(
new Rect(
10,
100,
150,
30),s2,
"三大个工具栏2");
-
break;
-
case
3:
-
s1 = GUI.Toggle(
new Rect(
10,
50,
150,
30),s1,
"四大个工具栏1");
-
s2 = GUI.Toggle(
new Rect(
10,
100,
150,
30),s2,
"四大个工具栏2");
-
break;
-
}
-
}
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 scrollPosition;
-
-
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 background;
-
-
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:
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);
-
}
-
}
GUI Skin
Project : Create -> GUI Skin
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: 其他的一些设置
对于这些设置下的选项有:
-
Name:名字
-
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 myGUIskin;
-
string info =
"我是要做海贼王的男人!";
-
void OnGUI(){
-
GUI.skin = myGUIskin;
-
-
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)) {
-
info += info;
-
}
-
if (GUILayout.Button (info)) {
-
info += 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 winArrayList;
-
-
public Texture icon;
-
-
void Start(){
-
winArrayList =
new ArrayList ();
-
winArrayList.Add (
new Rect(winArrayList.Count *
250,
50,
100,
100));
-
}
-
-
void OnGUI(){
-
int count = winArrayList.Count;
-
for (
int i =
0; i<count; 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 winArrayList;
-
-
public Texture icon;
-
-
void Start(){
-
winArrayList =
new ArrayList ();
-
winArrayList.Add (
new Rect(winArrayList.Count *
250,
50,
100,
100));
-
}
-
-
void OnGUI(){
-
int count = winArrayList.Count;
-
for (
int i =
0; i<count; 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 UnityEngine;
-
using System.Collections;
-
-
public
class
Show :
MonoBehaviour {
-
-
public GUISkin myGUISkin;
-
-
void OnGUI(){
-
GUI.skin = myGUISkin;
-
GUI.Label (
new Rect(
50,
50,
100,
30),
"王的男人!");
-
}
-
}
-
动态加载图片
将图片文件c.jpg放入Resources文件夹中:
-
public Texture2D txt;
-
void OnGUI(){
-
if(GUI.Button(
new Rect(
50,
50,
100,
30),
"load texture")){
-
txt = Resources.Load(
"c");
-
}
-
}
</div>