Unity3D笔记 GUI 三、实现选项卡二窗口
实现目标:
1、使用个性化Box控件
2、个性化Lable控件
3、添加纵向滚动条
4、新建SelectedItem样式
一、最终效果:
二、主要代码
using UnityEngine;
using System.Collections; /// <summary>
/// 选项卡二
/// </summary>
[System.Serializable]
public class Item
{
public Texture icon;
public string name;//key name
public int amount;//数量
public string itemName;
/// <summary>
/// 处理ItemName
/// </summary>
public void SetUpItemName()
{
int iLength = this.name.Length + this.amount.ToString().Length;
if (iLength < )
{
while (this.name.Length < )
{
this.name += " ";
}
}
if (iLength < )
{
itemName = this.name + " " + this.amount;
}
else
{
itemName = this.name + this.amount;
}
}
/// <summary>
/// 获得ItemName
/// </summary>
/// <returns></returns>
public string GetItemName()
{
return itemName;
}
} public class myTest : MonoBehaviour
{
#region 选项卡2
Rect rItemBox = new Rect(, , , );
Rect rTipBox = new Rect(, , , );
Rect rItemsBox = new Rect(, , , );
Rect rTipButton = new Rect(, , , );
Rect rVerScroll = new Rect(, , , );
float fScrollPos = 1.0f;
Vector2 scrollPosition = Vector2.zero;//写Vector2(0, 0)的简码
Vector2 scrollPosition2 = Vector2.zero;
public Item[] items; Item currentItem;
int inToolItem = ;
#endregion /// <summary>
/// 是否打开窗口
/// </summary>
bool isOpenMenu = false;
/// <summary>
/// 窗体的大小和位置【矩形】
/// </summary>
Rect myWindow = new Rect(, , , );
/// <summary>
/// GUI Skin
/// </summary>
public GUISkin customSkin;
/// <summary>
/// 关闭按钮
/// </summary>
Rect closeButton = new Rect(, , , ); /// <summary>
/// 用于工具栏在屏幕上的矩形位置
/// </summary>
Rect tabButton = new Rect(, , , );
/// <summary>
/// 选项卡索引号
/// </summary>
int toolsCount = ;
/// <summary>
/// 显示在工具栏按钮上的字符串数组
/// </summary>
string[] toolsName = { "选项卡1", "选项卡2", "选项卡3" }; /// <summary>
/// 选项卡中的图片
/// </summary>
public Texture img;
/// <summary>
/// 选项卡中的图片位置
/// </summary>
Rect imgRect = new Rect(, , , ); #region 个性化Box控件
Rect stateBox = new Rect(, , , );
Rect weaponBox = new Rect(, , , );
public Texture box1;
public Texture box2;
public Texture box3;
Rect box1Rect = new Rect(, , , );
Rect box2Rect = new Rect(, , , );
Rect box3Rect = new Rect(, , , );
#endregion #region 实现Status窗口 选项卡一
GUIContent guiWeaponCon = new GUIContent();
GUIContent guiArmorCon = new GUIContent();
GUIContent guiAccessCon = new GUIContent();
GUIContent guiSkillCon = new GUIContent();
Rect weaponLable = new Rect(, , , );
Rect armorLable = new Rect(, , , );
Rect accessLable = new Rect(, , , );
Rect skillLable = new Rect(, , , );
string uneuip = "Hello";
Rect hpLabel = new Rect(, , , );
Rect mpLabel = new Rect(, , , );
Rect lvLabel = new Rect(, , , );
Rect expLabel = new Rect(, , , );
Rect nextLabel = new Rect(, , , );
Rect atkLabel = new Rect(, , , );
Rect defLabel = new Rect(, , , );
Rect agiLabel = new Rect(, , , );
Rect intLabel = new Rect(, , , );
Rect lucLable = new Rect(, , , );
int fullHP = ;
int fullMP = ;
int hp = ;
int mp = ;
int lv = ;
int exp = ;
int next = ;
int atk = ;
int def = ;
int agi = ;
int ints = ;
int luc = ;
#endregion // Use this for initialization
void Start()
{
isOpenMenu = false;
guiWeaponCon = new GUIContent(uneuip);
guiArmorCon = new GUIContent(uneuip);
//guiAccessCon = new GUIContent(uneuip); //位置 要调节下 目前位置有点错位
guiSkillCon = new GUIContent(uneuip); //选项卡一初始化
if (items.Length>)
{
items[].SetUpItemName();
currentItem = items[];
}
} // Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.M))//当通过名称指定的按键被用户按住时返回true
{
isOpenMenu = true;
}
} void OnGUI()
{
GUI.skin = customSkin;
if (isOpenMenu)
{
int windowId = ;
myWindow = GUI.Window(windowId, myWindow, WindowFunction, "Hello Window");
#region Mathf.Clamp 限制
// 限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
myWindow.x = Mathf.Clamp(myWindow.x, , Screen.width - myWindow.width);// [klæmp] vt.& vi. 夹紧,夹住;锁住;把(砖等)堆高
myWindow.y = Mathf.Clamp(myWindow.y, , Screen.height - myWindow.height);
#endregion
}
} void WindowFunction(int windowId)
{ //begin 关闭按钮
if (GUI.Button(closeButton, "", GUI.skin.GetStyle("closeButton")))
{
isOpenMenu = false;//单击关闭按钮:窗口菜单关闭
}
//end //beign 选项卡
//返回int类型,被选择按钮的索引号
toolsCount = GUI.Toolbar(tabButton, toolsCount, toolsName, GUI.skin.GetStyle("tabButton"));//工具栏
//end //begin 选项卡图片
GUI.DrawTexture(imgRect, img); //end
GUI.DragWindow();//拖动窗口 #region 实现Status窗口
switch (toolsCount)
{
case :
StatusWindow();
break;
case :
ItemWindow();
break;
case :
break;
default:
break;
}
#endregion } /// <summary>
/// 选项卡一
/// </summary>
void StatusWindow()
{ GUI.Box(stateBox, "");
GUI.Box(weaponBox, "");//['wepən] n. 武器,兵器
GUI.DrawTexture(box1Rect, box1);
GUI.DrawTexture(box2Rect, box2);
GUI.DrawTexture(box3Rect, box3); GUI.Label(hpLabel, hp.ToString() + "/" + fullHP.ToString(), "TextAmount");
GUI.Label(mpLabel, mp.ToString() + "/" + fullMP.ToString(), "TextAmount");
GUI.Label(lvLabel, lv.ToString(), "TextAmount");
GUI.Label(expLabel, exp.ToString(), "TextAmount");
GUI.Label(nextLabel, next.ToString(), "TextAmount");
GUI.Label(atkLabel, atk.ToString(), "TextAmount");
GUI.Label(defLabel, def.ToString(), "TextAmount");
GUI.Label(agiLabel, agi.ToString(), "TextAmount");
GUI.Label(intLabel, ints.ToString(), "TextAmount"); GUI.Label(lucLable, luc.ToString());
GUI.Label(weaponLable, guiWeaponCon, "TextItem");
GUI.Label(armorLable, guiArmorCon, "TextItem");
GUI.Label(accessLable, guiAccessCon, "TextItem");
GUI.Label(skillLable, guiSkillCon, "TextItem"); } /// <summary>
/// 选项卡二
/// </summary>
void ItemWindow()
{
int inItems = ;
GUI.Box(rItemBox, "");
GUI.Box(rTipBox, ""); //定义纵向滚动条
scrollPosition = GUI.BeginScrollView(new Rect(, , , ), scrollPosition, new Rect(, , , * inItems));
GUIContent[] itemsContent = new GUIContent[inItems];
for (int i = ; i < inItems; i++)
{
if (items.Length > )
{
if (i == )
{
itemsContent[i] = new GUIContent(currentItem.itemName, currentItem.icon, "Test Hello World Hello WorldHello WorldHello WorldHello World ********************");
}
else
{
itemsContent[i] = new GUIContent(currentItem.itemName, currentItem.icon, "This key is" + i);
}
}
else
{
itemsContent[i] = new GUIContent("None", "");
}
}
inToolItem = GUI.SelectionGrid(new Rect(, , , * inItems), inToolItem, itemsContent, , GUI.skin.GetStyle("SelectedItem"));//SelectionGrid()选择表格,创建一个网格按钮
GUI.EndScrollView();
//下部分滚动条
string sInfos = itemsContent[inToolItem].tooltip;
if (string.IsNullOrEmpty(sInfos))
{
sInfos = "show items information here ";
}
GUIStyle style = GUI.skin.GetStyle("label"); if (GUI.tooltip != "")
{
float fHeight = style.CalcHeight(new GUIContent(GUI.tooltip), 330.0f);//计算高度
scrollPosition2 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition2, new Rect(, , , fHeight));
GUI.Label(new Rect(, , , fHeight), GUI.tooltip);
}
else
{
float fHeight = style.CalcHeight(new GUIContent(sInfos), 330.0f);//计算高度 scrollPosition2 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition2, new Rect(, , , fHeight));
GUI.Label(new Rect(, , , fHeight), sInfos);
}
GUI.EndScrollView();
}
}
注意:
龚老师是用js写的,所以那个类Item直接在Unity3D Inspector中显示了,但是C#要注意了 ,在需要的类前面加一个特性[System.Serializable]js继承Object类默认就是会被序列化。
Unity3D笔记 GUI 三、实现选项卡二窗口的更多相关文章
- Unity3D笔记 GUI 四、实现选项卡三
一.代码: using UnityEngine; using System.Collections; /// <summary> /// 选项卡二 /// </summary> ...
- Unity3D笔记 GUI 一
要实现的功能: 1.个性化Windows界面 2.减少个性化的背景图片尺寸 3.个性化样式ExitButton和TabButton 4.实现三个选项卡窗口 一.个性化Windows界面 1.1.创建一 ...
- Unity3D笔记 GUI 二 、实现选项卡一窗口
实现目标: 1.个性化Box控件 2.新建TextAmount样式 3.新建TextItem样式 一.个性化Windows界面 设置GUI Skin 1.2 部分代码 Rect stateBox = ...
- Unity3D笔记 愤怒的小鸟<三> 实现Play界面2
前言:在Play页面中给Play页面添加一个“开始游戏”和“退出游戏”按钮顺便再来一个背景音乐 添加按钮可以是GUI.Button(),也可以是GUILayout.Button():给图片添加按钮可以 ...
- Unity3D笔记六 GUI游戏界面
1.Label:标签控件,可以在游戏中用来展示文本字符串信息,不仅可以写字还可以贴图片. 2.Button:按钮控件,一般分图片按钮和普通的按钮,还有一个连续按钮RepeatButton注意,这个在W ...
- PyQt4入门学习笔记(三)
# PyQt4入门学习笔记(三) PyQt4内的布局 布局方式是我们控制我们的GUI页面内各个控件的排放位置的.我们可以通过两种基本方式来控制: 1.绝对位置 2.layout类 绝对位置 这种方式要 ...
- Android群英传笔记——第三章:Android控件架构与自定义控件讲解
Android群英传笔记--第三章:Android控件架构与自定义控件讲解 真的很久没有更新博客了,三四天了吧,搬家干嘛的,心累,事件又很紧,抽时间把第三章大致的看完了,当然,我还是有一点View的基 ...
- JavaWeb和WebGIS学习笔记(三)——GeoServer 发布shp数据地图
系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...
- C#可扩展编程之MEF学习笔记(三):导出类的方法和属性
前面说完了导入和导出的几种方法,如果大家细心的话会注意到前面我们导出的都是类,那么方法和属性能不能导出呢???答案是肯定的,下面就来说下MEF是如何导出方法和属性的. 还是前面的代码,第二篇中已经提供 ...
随机推荐
- AdoConnect-获取连接字符串 (工具)
使用ADO访问数据库时需要设置正确的连接字符串,为此特提供一个获取连接字符串的小工具,方便编程使用. 使用方法: 1.点击“连接字符串”,弹出数据链接属性对话框 2.可以使用“提供程序”新建数据源,也 ...
- 缓存管理(本地缓存+memcached)
http://www.cnblogs.com/daizhj/archive/2009/11/17/1604436.html
- Python 常用类库
python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的libraries(modules)如下: 1 ...
- sql语句 case when then else end 语句实例
表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列. ----------------------------------------- ...
- python与VScode
用VScode写python是非常方便的.vscode是一个功能非常强大的编辑器,下面介绍大致的使用方法: 下载安装python,配置环境变量. 下载安装VScode(vscode会自动连接pytho ...
- git+gitolite+cgit+apache on Ubuntu
git+gitolite+cgit+apache on Ubuntu Just record, do *NOT* copy-paste. git+gitolite sudo apt-get insta ...
- [Ubuntu] arp-scan - 扫描网络设备
使用arp-scan扫描所有网络设备信息. 1. 安装arp-scan ifantastic@ubuntu:~$ sudo apt-get install arp-scan 2. 扫描网络所有设备 i ...
- Linux下用C获取当前时间
Linux下用C获取当前时间,具体如下: 代码(可以把clock_gettime换成time(NULL)) ? 1 2 3 4 5 6 7 8 9 10 void getNowTime() { ti ...
- 手机端点击复制链接到剪切板(以及PC端)
一直在找如何能点击按钮将一串字符串放到手机的剪切板上,但是可能是因为搜索的关键字不对,一直无果. 向同事请教了一下,给了一个clickboard.js的插件.开始试验的时候,使用手机自带浏览器进行测试 ...
- Android设计和开发系列第二篇:Action Bar(Develop—API Guides)
Action Bar IN THIS DOCUMENT Adding the Action Bar Removing the action bar Using a logo instead of an ...