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 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...
随机推荐
- UNIX环境编程学习笔记(13)——文件I/O之标准I/O流
lienhua342014-09-29 1 标准 I/O 流 之前学习的都是不带缓冲的 I/O 操作函数,直接针对文件描述符的,每调用一次函数可能都会触发一次系统调用,单次调用可能比较快捷.但是,对于 ...
- 使用spring initialization创建SpringBoot项目
https://blog.csdn.net/liutong123987/article/details/79385513 有很多方法可以快速创建Springboot项目,可以通过idea的spring ...
- mongodb:修改oplog.rs 的大小size
其内容字段说明: ts:操作日志的timestamp t: 未知? h:操作唯一随机值 v:oplog.rs的版本 op:操作类型: i:insert操作 u:update操作 d:delete操作 ...
- Oauth2.0(四):Implicit 授权方式
Oauth2.0的核心机制已经总结完毕.除了核心机制,Oauth2.0 还提供了几种标准的授权流程,分别适用于不同的场景.其中一种叫做 Implicit 授权,适用于纯静态页面应用.所谓纯静态页面应用 ...
- Missing iOS Distribution signing identity
打包上传appstore的时候报错如下: 解决方法: Download https://developer.apple.com/certificationauthority/AppleWWDRCA.c ...
- AcceptEx 以及 获取远程IP与port
// 獲取本地以及遠程的IP和port setsockopt(clientfd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *)&listenfd ...
- HttpClientUtil请求http地址的工具类
直接贴代码: import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache ...
- Incorrect column count: expected 1, actual 5,JdbcTemplate queryForList 出错
spring JdbcTemplate queryForList 出错 Incorrect column count: expected 1, actual 5 >>>>&g ...
- Redis存读取数据
这一节演示下载.NET中怎样使用Redis存储数据.在.net中比较常用的客户端类库是ServiceStack,看下通过servicestack怎样存储数据. DLL下载:https://github ...
- OBS显示器获取显示黑色没有图像