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 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...
随机推荐
- 【scala】 scala 映射和元组操作(四)
1.映射 Map 定义 ,取值,遍历,排序 2. 元组 定义,取值,拉链操作 import scala.collection.mutable /** * 映射和元组 * * @author xwol ...
- [Scikit-learn] 1.4 Support Vector Machines - Linear Classification
Outline: 作为一种典型的应用升维的方法,内容比较多,自带体系,以李航的书为主,分篇学习. 函数间隔和几何间隔 最大间隔 凸最优化问题 凸二次规划问题 线性支持向量机和软间隔最大化 添加的约束很 ...
- UpLoader------实现上传大文件
代码: <div id="selectFile">选择文件1</div> <script> var da = newGuid(); var kk ...
- C#------如何使用Swagger调试接口
1.打开NuGet程序包 2.安装下面两个程序包 3.安装完后会出现SwaggerConfig.cs类,并修改里面的内容 代码: [assembly: PreApplicationStartMetho ...
- javascript拖拽操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 有人在贴吧问phpmyadmin如何设置插入的时候默认插入1条记录
在新版phpmyadmin中(我的版本是3.5.1) 插入的时候会提示插入两条,能够方便操作,让你多录入几条数据,如图 然而有人不想要这个界面默认插入两条,如何改为1条或者其他呢? 我审查了这个元素标 ...
- 【ArcGIS】ArcGIS Data Store配置
一.错误提示 Unable to configure the ArcGIS Data Store with the GIS Server. Please make sure that the GIS ...
- Explaining Delegates in C# - Part 5 (Asynchronous Callback - Way 2)
In this part of making asynchronous programming with delegates, we will talk about a different way, ...
- python文件操作笔记
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 2.切换工作目录: os.c ...
- JQuery插件的使用
今天就来总结一下jquery插件的使用,刚开始学习时间一直自己在写实现他的功能,现在发现jquery好强大啊,好多的功能都给我们封装好啦,jquery万岁,嘿嘿,下面就来简单的总结一下我们学习的: