Unity3D笔记 GUI 四、实现选项卡三
一、代码:
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;
Vector2 scrollPosition3 = 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 #region 选项卡3
Rect equipBox = new Rect(, , , );
Rect equipWeanponBox = new Rect(, , , );
Rect statTextureEquip = new Rect(, , , );
Rect skillBoxEquip = new Rect(, , , );
Rect[] equipRect = { new Rect(, , , ), new Rect(, , , ), new Rect(, , , ), new Rect(, , , ) };
Rect equipWindow = new Rect(, , , );
//Vector2 scrollPosition = Vector2.zero;
bool[] equipBool = new bool[];
int inToolWeapons = ;
int inToolArmors = ;
int inToolAccess = ;
int inToolSkill = ;
public Texture tStatusBox1;
public Texture tStatusBox2;
public Texture tSkillBox; public Item[] weapons;
public Item[] armors;
public Item[] access;
public Texture[] skills;
Item currentWeapon;
Item currentArmors;
Item currentAccess;
Texture currentSkill;
#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 :
EquipWindow();
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.Box(equipBox, "");
//GUI.Box(equipWeanponBox, "");
//GUI.DrawTexture(statTextureEquip, tStatusBox2);
//GUI.DrawTexture(skillBoxEquip, tSkillBox);
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();
} /// <summary>
/// 选项卡3
/// </summary>
void EquipWindow()
{
GUI.Box(equipBox, "");
GUI.Box(equipWeanponBox, "");
GUI.DrawTexture(statTextureEquip, tStatusBox2);
GUI.DrawTexture(skillBoxEquip, tSkillBox); SetupEuiqipBox();
} void SetupEuiqipBox()
{
GUIContent[] equipContent = { guiWeaponCon, guiArmorCon, guiAccessCon, guiSkillCon };
for (int i = ; i < equipBool.Length; i++)
{
if (equipBool[i])
{
GUI.Label(equipRect[i], equipContent[i], "DisabledClick ");
switch (i)
{
case :
ShowWeapon();
break;
case :
ShowArmor();//['ɑ:mə] 装甲;盔甲;
break;
case :
ShowAccess();
break;
case :
ShowSkill();
break;
default:
break;
}
}
else
{
if (GUI.Button(equipRect[i], equipContent[i], "SelectedItem"))
{
equipBool[i] = true;
for (int j = ; j < equipBool.Length; j++)
{
if (i != j)
{
equipBool[j] = false;
}
}
}
}
}
} void ShowWeapon()
{
int inItems = ;
GUIContent[] itemsContent = new GUIContent[inItems];
for (int i = ; i < inItems; i++)
{
if (i == )
{
itemsContent[i] = new GUIContent(uneuip, "");
}
else
{
itemsContent[i] = new GUIContent(weapons[].name, weapons[].icon, "");
}
}
scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , * inItems));
inToolWeapons = GUI.SelectionGrid(new Rect(, , , * inItems), inToolWeapons, itemsContent, , GUI.skin.GetStyle("SelectedItem"));
GUI.EndScrollView();
guiWeaponCon = itemsContent[inToolWeapons];
}
void ShowArmor()
{
int inItems = ;
GUIContent[] itemsContent = new GUIContent[inItems];
for (int i = ; i < inItems; i++)
{
if (i == )
{
itemsContent[i] = new GUIContent(uneuip, "");
}
else
{
itemsContent[i] = new GUIContent(armors[].name, armors[].icon, "");
}
}
scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , * inItems));
inToolArmors = GUI.SelectionGrid(new Rect(, , , * inItems), inToolWeapons, itemsContent, , GUI.skin.GetStyle("SelectedItem"));
GUI.EndScrollView();
guiWeaponCon = itemsContent[inToolArmors];
}
void ShowAccess()
{
int inItems = ;
GUIContent[] itemsContent = new GUIContent[inItems];
for (int i = ; i < inItems; i++)
{
if (i == )
{
itemsContent[i] = new GUIContent(uneuip, "");
}
else
{
itemsContent[i] = new GUIContent(access[].name, access[].icon, "");
}
}
scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , * inItems));
inToolAccess = GUI.SelectionGrid(new Rect(, , , * inItems), inToolWeapons, itemsContent, , GUI.skin.GetStyle("SelectedItem"));
GUI.EndScrollView();
guiWeaponCon = itemsContent[inToolAccess];
}
void ShowSkill()
{
int inItems = ;
GUIContent[] itemsContent = new GUIContent[inItems];
for (int i = ; i < inItems; i++)
{
if (i == )
{
itemsContent[i] = new GUIContent(tSkillBox);
}
else
{
itemsContent[i] = new GUIContent(skills[i-]);
}
}
scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , ));
inToolSkill = GUI.SelectionGrid(new Rect(, , , ), inToolWeapons, itemsContent, inItems, GUI.skin.GetStyle("SelectedItem"));
GUI.EndScrollView();
if (inToolSkill != )
{
guiSkillCon = itemsContent[inToolSkill];
}
else
{
guiSkillCon = new GUIContent("");
}
}
}
二、有问题的效果
有个问题,还没解决就是GUI.Button() 没有执行,所以看下面效果就没有出来。。。
三、断点调式:
就是没有进入到Button方法下去。
怎么解决呢???
断点调式:
Unity3D笔记 GUI 四、实现选项卡三的更多相关文章
- Unity3D笔记 GUI 一
要实现的功能: 1.个性化Windows界面 2.减少个性化的背景图片尺寸 3.个性化样式ExitButton和TabButton 4.实现三个选项卡窗口 一.个性化Windows界面 1.1.创建一 ...
- Unity3D笔记 GUI 三、实现选项卡二窗口
实现目标: 1.使用个性化Box控件 2.个性化Lable控件 3.添加纵向滚动条 4.新建SelectedItem样式 一.最终效果: 二.主要代码 using UnityEngine; using ...
- Unity3D笔记 GUI 二 、实现选项卡一窗口
实现目标: 1.个性化Box控件 2.新建TextAmount样式 3.新建TextItem样式 一.个性化Windows界面 设置GUI Skin 1.2 部分代码 Rect stateBox = ...
- Unity3D笔记 愤怒的小鸟<四> 实现Selelction界面
一直跟着龚老师用js写,VS智能感应用习惯后发现这里用js对初学者比较蛋疼,MONO有提示但是还是无法和VS媲美就目前来看.所以这次还是换成熟悉的VS来开发. 目标:实现关卡页面 跑起来的效果如下: ...
- Unity3D笔记十四 力
Unity中力的方式有两种:第一种为普通力,需要设定力的方向与大小:第二种为目标位置力,需要设定目标点的位置,该物体将朝向这个目标位置施加力. 1.案例 添加两个球体和一个立方体,另外还要给两个球体添 ...
- JavaWeb和WebGIS学习笔记(四)——使用uDig美化地图,并叠加显示多个图层
系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...
- X-Cart 学习笔记(四)常见操作
目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...
- opencv学习笔记(四)投影
opencv学习笔记(四)投影 任选了一张图片用于测试,图片如下所示: #include <cv.h> #include <highgui.h> using namespace ...
- Memcached笔记——(四)应对高并发攻击【转】
http://snowolf.iteye.com/blog/1677495 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...
随机推荐
- ambari HDFS-HA 回滚
curl -u admin:admin -H "X-Requested-By: ambari" -X GET http://zwshen86:8080/api/v1/cluster ...
- 如何获取ABBYY FineReader 12注册码-激活码-序列号
提及OCR文字识别软件,相信不少人会脱口而出ABBYY FineReader,这款软件当前最新版本为FineReader 12,是市场领先的OCR图文识别软件,不仅可以将纸质文档和PDF文件以及图像文 ...
- ABBYY FineReader 12中的用户模式你会用吗
在ABBYY FineReader 12OCR文字识别软件中,有一个概念叫“训练”,它是在字符图像和字符本身之间建立对应关系的过程,训练模式可以提高含有装饰字体的文档或包含特殊字符(例如数学符号)文档 ...
- mySql的desc与explain分析性能(主要分析索引)
desc select * from A where id =‘110’; 查询结果的含义请参考:http://www.2cto.com/database/201209/156466.html
- 禁止requests请求https的提示InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more
提示这个 InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from ...
- Docker background
什么是 Docker? Docker 提供了一个可以运行你的应用程序的封套(envelope),或者说容器.它原本是 dotCloud 启动的一个业余项目,并在前些时候开源了.它吸引了大量的关注和讨论 ...
- iOS 严重问题解释(crash)
问题1:Exception Type: 00000020 Exception Codes: 0x000000008badf00d Exception Note: SIMULATED (this is ...
- MySQL存储过程的异常处理
阅读目录:存储过程的异常处理 定义异常处理 单一异常处理程序 continue exit 多个异常处理程序 关于错误编号和SQLSTATE码 使用3个处理程序 忽略某一异常的处理 异常处理的命名 异常 ...
- 【安全开发】IOS安全编码规范
申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...
- Python爬虫学习笔记-2.Requests库
Requests是Python的一个优雅而简单的HTTP库,它比Pyhton内置的urllib库,更加强大. 0X01 基本使用 安装 Requests,只要在你的终端中运行这个简单命令即可: pip ...