您的位置:首页 > 产品设计 > UI/UE

GUI使用2——总结&NGUI、tookit2D、GUI比较

2015-06-09 16:24 489 查看
使用GUI实现窗体的主要代码

using UnityEngine;
using System.Collections;

public class MyGUI : MonoBehaviour {

//public Texture aaa;
//public string bbb;
//public int ccc;
//public string ddd;

private bool showWindow;//控制主窗体是否打开状态
private Rect windowRectB;//主窗体
private Rect btnClose;//关闭按钮
public GUISkin customSkin;//自定义皮肤

public Texture tHero;
public Texture status1;
public Texture status2;
public Texture killBox;

private int intToobar = 0;//被选择按钮的索引号。
private int selectionGrid = 0;//被选择表格按钮的索引号。

public string[] toobarString = { "STATUS", "INVENTORY", "EQUIPMENT" };

public Item[] items;

private Vector2 scrollPosition = Vector2.zero;//定义滚动条的初始位置

//public Item[]items; //定义item类型数组
//private Item currentItem;

// Use this for initialization
void Start ()
{
showWindow = false;
windowRectB = new Rect(10, 10, 680, 480);
btnClose = new Rect(620,10,26,22);

//items = new Item[2] { new Item(aaa, bbb, ccc, ddd), new Item(aaa, bbb, ccc, ddd) };
}

void Update ()
{
if (Input.GetKey(KeyCode.M))
{
showWindow = true;
}
}

void OnGUI()
{
GUI.skin = customSkin;
if (showWindow)
{
windowRectB = GUI.Window(0, windowRectB, MyDoWindow, "", GUI.skin.GetStyle("windowRectB"));

windowRectB.x = Mathf.Clamp(windowRectB.x, 0, Screen.width - windowRectB.width);//clamp限制函数,限制窗体的宽在0和creen.width - windowRectB.width之间
windowRectB.y = Mathf.Clamp(windowRectB.y, 0, Screen.height - windowRectB.height);
}
}

void MyDoWindow(int windowsID)
{
switch (intToobar)
{
case 0:
toolBar1();
break;
case 1:
toolBar2();
break;
case 2:
break;
}

intToobar = GUI.Toolbar(new Rect(35, 15, 490, 40), intToobar, toobarString, GUI.skin.GetStyle("myToolbar"));//绘制toolbar
if (GUI.Button(btnClose,"", GUI.skin.GetStyle("btnClose")))//关闭窗体代码

{
showWindow = false;
}
GUI.DragWindow();//GUI.DragWindow()函数必须写在按钮代码的后头,否则会出现惊悚的事情
GUI.DrawTexture(new Rect(19,35,225,441),tHero);//显示旁边的图片
}

void toolBar1()
{
GUI.Box(new Rect(237, 67, 360, 147), "");//绘制状态盒子
GUI.Box(new Rect(237, 230, 360, 207), "");//绘制收获盒子
GUI.DrawTexture(new Rect(252, 77, 331, 125), status1);
GUI.DrawTexture(new Rect(252, 244, 331, 125), status2);
GUI.DrawTexture(new Rect(460, 284, 117, 125), killBox);

GUI.Label(new Rect(313, 75, 120, 25), "123");
GUI.Label(new Rect(313, 100, 120, 25), "123");
GUI.Label(new Rect(313, 124, 120, 25), "123");
GUI.Label(new Rect(313, 150, 120, 25), "123");
GUI.Label(new Rect(313, 177, 120, 25), "123");

}

void toolBar2()
{
int intItems = 8;
GUI.Box(new Rect(237,67,360,247), "");
GUI.Box(new Rect(237,330,360,107), "");
//滚动条部分

scrollPosition=GUI.BeginScrollView(new Rect(257,87,320,200),scrollPosition,new Rect(0,0,280,40*intItems));

//GUIContent[] itemContent = new GUIContent[intItems];
//for (int i = 0; i < intItems; i++)
//{
//    selectionGrid = GUI.SelectionGrid(new Rect(0, 0, 280, 40 * intItems), selectionGrid, itemContent, 1);
//}

GUI.EndScrollView();
}
}


  

问题和总结:

1 在脚本中new一个自定义变量,自定义变量中的共有变量不能显示在属性面板上

(如果想让自定义的类的共有字段出现在属性面板上需要继承 MonoBehaviour )

2 不明白unity中的类和自定义类的区别

3 tookit2D使用起来比较方便,只是在添加方法上有些麻烦(见tookit2D使用3——给精灵添加GUI按钮效果),很适合制作2d游戏

4 给GUI添加皮肤组件可以实现和NGUI一样的效果缺点:使用GUI各个模块的距离必须自己控制,给按钮添加事件要靠使用变量来控制,和C#中的事件不一样,(NGUI相当于在游戏视窗中添加了游戏对象,而GUI只是在OnGUI函数中绘制,两者可以实现的相同的效果,实现的原理不同)

5 这几天接触自定义类和学习网络游戏相当受挫,还不清楚C#脚本和Unity脚本之间的关系,还不清楚C#的委托,接口,套接字,多线程在unity有没有变化,快要找工作了,觉得患得患失的,既不想花太多时间去解决一个问题,又觉得有好多没有学习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: