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 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...
随机推荐
- 非抢占式RCU实现(二),解释:为什么 RCU_NEXT_SIZE 宏值是4?
参考:2.6.34 一个很奇怪的问题. 没有查找到为什么 RCU_NEXT_SIZE的值为4的原因(包括Documentation),主要是在rcu_state中定义了一个四级的list,感到很有意思 ...
- Deep Voice
https://arxiv.org/abs/1702.07825 听起来和真人声非常接近了.
- node.js和socket.io实现im
im——Instant Messaging 即时通讯 基本技术原理 (1)通过IM服务器登陆或注销 (2)用户A通过列表找到B,用户B获得消息并与之交谈 (3)通过IM服务器指引建立与B单独的通讯通道 ...
- 解决sourcesafe admin用户自动登录并且不用密码的问题
用管理员(admin)登录Microsoft Visual SourceSafe Administration tools-> "SourceSafe Options界 ...
- requireJs压缩合并路径问题
描述 随着前端开发的重要性,以及业务的复杂性,前端的模块化开发也被大众所接收,最常见的js框架requireJs,一个js文件对应一个模块,方便开发人员调试与维护,但是一个文件对应一个模块增加了htt ...
- Eclipse------使用Debug As时报错java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX
报错信息: java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file ...
- 给一个由n-1个整数组成的未排序的序列,其元素都是1~n中的不同的整数。如何在线性时间复杂度内寻找序列中缺失的整数
思路分析:尼玛这不就是等差数列么.首先将该n-1个整数相加,得到sum,然后用(1+n)n/2减去sum,得到的差即为缺失的整数.因为1~n一共n个数,n个数的和为(1+n)n/2,而未排序数列的和为 ...
- 第四章 TCP粘包/拆包问题的解决之道---4.1---
4.1 TCP粘包/拆包 TCP是一个“流”协议,所谓流,就是没有界限的一串数据.TCP底层并不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际情况进行包的划分,所以在业务上认为,一个完整的包可 ...
- 在eclipse中查看android源代码
自己写了一个类MainAcvitivity extends Activity, 按F12(我把转到定义改成了F12的快捷键),转到Activity的定义,弹出下面这样的界面 就是说没有找到androi ...
- 服务端测试之接口测试工具——postman
今天跟大家分享一个非常常见大家也非常熟悉的测试工具——postman. 1.安装postman postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件.打开chrome浏览器, ...