一、代码:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /// <summary>
  5. /// 选项卡二
  6. /// </summary>
  7. [System.Serializable]
  8. public class Item
  9. {
  10. public Texture icon;
  11. public string name;//key name
  12. public int amount;//数量
  13. public string itemName;
  14. /// <summary>
  15. /// 处理ItemName
  16. /// </summary>
  17. public void SetUpItemName()
  18. {
  19. int iLength = this.name.Length + this.amount.ToString().Length;
  20. if (iLength < )
  21. {
  22. while (this.name.Length < )
  23. {
  24. this.name += " ";
  25. }
  26. }
  27. if (iLength < )
  28. {
  29. itemName = this.name + " " + this.amount;
  30. }
  31. else
  32. {
  33. itemName = this.name + this.amount;
  34. }
  35. }
  36. /// <summary>
  37. /// 获得ItemName
  38. /// </summary>
  39. /// <returns></returns>
  40. public string GetItemName()
  41. {
  42. return itemName;
  43. }
  44. }
  45.  
  46. public class myTest : MonoBehaviour
  47. {
  48. #region 选项卡2
  49. Rect rItemBox = new Rect(, , , );
  50. Rect rTipBox = new Rect(, , , );
  51. Rect rItemsBox = new Rect(, , , );
  52. Rect rTipButton = new Rect(, , , );
  53. Rect rVerScroll = new Rect(, , , );
  54. float fScrollPos = 1.0f;
  55. Vector2 scrollPosition = Vector2.zero;//写Vector2(0, 0)的简码
  56. Vector2 scrollPosition2 = Vector2.zero;
  57. Vector2 scrollPosition3 = Vector2.zero;
  58. public Item[] items;
  59.  
  60. Item currentItem;
  61. int inToolItem = ;
  62. #endregion
  63.  
  64. /// <summary>
  65. /// 是否打开窗口
  66. /// </summary>
  67. bool isOpenMenu = false;
  68. /// <summary>
  69. /// 窗体的大小和位置【矩形】
  70. /// </summary>
  71. Rect myWindow = new Rect(, , , );
  72. /// <summary>
  73. /// GUI Skin
  74. /// </summary>
  75. public GUISkin customSkin;
  76. /// <summary>
  77. /// 关闭按钮
  78. /// </summary>
  79. Rect closeButton = new Rect(, , , );
  80.  
  81. /// <summary>
  82. /// 用于工具栏在屏幕上的矩形位置
  83. /// </summary>
  84. Rect tabButton = new Rect(, , , );
  85. /// <summary>
  86. /// 选项卡索引号
  87. /// </summary>
  88. int toolsCount = ;
  89. /// <summary>
  90. /// 显示在工具栏按钮上的字符串数组
  91. /// </summary>
  92. string[] toolsName = { "选项卡1", "选项卡2", "选项卡3" };
  93.  
  94. /// <summary>
  95. /// 选项卡中的图片
  96. /// </summary>
  97. public Texture img;
  98. /// <summary>
  99. /// 选项卡中的图片位置
  100. /// </summary>
  101. Rect imgRect = new Rect(, , , );
  102.  
  103. #region 个性化Box控件
  104. Rect stateBox = new Rect(, , , );
  105. Rect weaponBox = new Rect(, , , );
  106. public Texture box1;
  107. public Texture box2;
  108. public Texture box3;
  109. Rect box1Rect = new Rect(, , , );
  110. Rect box2Rect = new Rect(, , , );
  111. Rect box3Rect = new Rect(, , , );
  112. #endregion
  113.  
  114. #region 实现Status窗口 选项卡一
  115. GUIContent guiWeaponCon = new GUIContent();
  116. GUIContent guiArmorCon = new GUIContent();
  117. GUIContent guiAccessCon = new GUIContent();
  118. GUIContent guiSkillCon = new GUIContent();
  119. Rect weaponLable = new Rect(, , , );
  120. Rect armorLable = new Rect(, , , );
  121. Rect accessLable = new Rect(, , , );
  122. Rect skillLable = new Rect(, , , );
  123. string uneuip = "Hello";
  124. Rect hpLabel = new Rect(, , , );
  125. Rect mpLabel = new Rect(, , , );
  126. Rect lvLabel = new Rect(, , , );
  127. Rect expLabel = new Rect(, , , );
  128. Rect nextLabel = new Rect(, , , );
  129. Rect atkLabel = new Rect(, , , );
  130. Rect defLabel = new Rect(, , , );
  131. Rect agiLabel = new Rect(, , , );
  132. Rect intLabel = new Rect(, , , );
  133. Rect lucLable = new Rect(, , , );
  134. int fullHP = ;
  135. int fullMP = ;
  136. int hp = ;
  137. int mp = ;
  138. int lv = ;
  139. int exp = ;
  140. int next = ;
  141. int atk = ;
  142. int def = ;
  143. int agi = ;
  144. int ints = ;
  145. int luc = ;
  146. #endregion
  147.  
  148. #region 选项卡3
  149. Rect equipBox = new Rect(, , , );
  150. Rect equipWeanponBox = new Rect(, , , );
  151. Rect statTextureEquip = new Rect(, , , );
  152. Rect skillBoxEquip = new Rect(, , , );
  153. Rect[] equipRect = { new Rect(, , , ), new Rect(, , , ), new Rect(, , , ), new Rect(, , , ) };
  154. Rect equipWindow = new Rect(, , , );
  155. //Vector2 scrollPosition = Vector2.zero;
  156. bool[] equipBool = new bool[];
  157. int inToolWeapons = ;
  158. int inToolArmors = ;
  159. int inToolAccess = ;
  160. int inToolSkill = ;
  161. public Texture tStatusBox1;
  162. public Texture tStatusBox2;
  163. public Texture tSkillBox;
  164.  
  165. public Item[] weapons;
  166. public Item[] armors;
  167. public Item[] access;
  168. public Texture[] skills;
  169. Item currentWeapon;
  170. Item currentArmors;
  171. Item currentAccess;
  172. Texture currentSkill;
  173. #endregion
  174.  
  175. // Use this for initialization
  176. void Start()
  177. {
  178. isOpenMenu = false;
  179. guiWeaponCon = new GUIContent(uneuip);
  180. guiArmorCon = new GUIContent(uneuip);
  181. //guiAccessCon = new GUIContent(uneuip); //位置 要调节下 目前位置有点错位
  182. guiSkillCon = new GUIContent(uneuip);
  183.  
  184. //选项卡一初始化
  185. if (items.Length > )
  186. {
  187. items[].SetUpItemName();
  188. currentItem = items[];
  189. }
  190. }
  191.  
  192. // Update is called once per frame
  193. void Update()
  194. {
  195. if (Input.GetKey(KeyCode.M))//当通过名称指定的按键被用户按住时返回true
  196. {
  197. isOpenMenu = true;
  198. }
  199. }
  200.  
  201. void OnGUI()
  202. {
  203. GUI.skin = customSkin;
  204. if (isOpenMenu)
  205. {
  206. int windowId = ;
  207. myWindow = GUI.Window(windowId, myWindow, WindowFunction, "Hello Window");
  208. #region Mathf.Clamp 限制
  209. // 限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
  210. myWindow.x = Mathf.Clamp(myWindow.x, , Screen.width - myWindow.width);// [klæmp] vt.& vi. 夹紧,夹住;锁住;把(砖等)堆高
  211. myWindow.y = Mathf.Clamp(myWindow.y, , Screen.height - myWindow.height);
  212. #endregion
  213. }
  214. }
  215.  
  216. void WindowFunction(int windowId)
  217. {
  218.  
  219. //begin 关闭按钮
  220. if (GUI.Button(closeButton, "", GUI.skin.GetStyle("closeButton")))
  221. {
  222. isOpenMenu = false;//单击关闭按钮:窗口菜单关闭
  223. }
  224. //end
  225.  
  226. //beign 选项卡
  227. //返回int类型,被选择按钮的索引号
  228. toolsCount = GUI.Toolbar(tabButton, toolsCount, toolsName, GUI.skin.GetStyle("tabButton"));//工具栏
  229. //end
  230.  
  231. //begin 选项卡图片
  232. GUI.DrawTexture(imgRect, img);
  233.  
  234. //end
  235. GUI.DragWindow();//拖动窗口
  236.  
  237. #region 实现Status窗口
  238. switch (toolsCount)
  239. {
  240. case :
  241. StatusWindow();
  242. break;
  243. case :
  244. ItemWindow();
  245. break;
  246. case :
  247. EquipWindow();
  248. break;
  249. default:
  250. break;
  251. }
  252. #endregion
  253.  
  254. }
  255.  
  256. /// <summary>
  257. /// 选项卡一
  258. /// </summary>
  259. void StatusWindow()
  260. {
  261.  
  262. GUI.Box(stateBox, "");
  263. GUI.Box(weaponBox, "");//['wepən] n. 武器,兵器
  264. GUI.DrawTexture(box1Rect, box1);
  265. GUI.DrawTexture(box2Rect, box2);
  266. //GUI.Box(equipBox, "");
  267. //GUI.Box(equipWeanponBox, "");
  268. //GUI.DrawTexture(statTextureEquip, tStatusBox2);
  269. //GUI.DrawTexture(skillBoxEquip, tSkillBox);
  270. GUI.DrawTexture(box3Rect, box3);
  271.  
  272. GUI.Label(hpLabel, hp.ToString() + "/" + fullHP.ToString(), "TextAmount");
  273. GUI.Label(mpLabel, mp.ToString() + "/" + fullMP.ToString(), "TextAmount");
  274. GUI.Label(lvLabel, lv.ToString(), "TextAmount");
  275. GUI.Label(expLabel, exp.ToString(), "TextAmount");
  276. GUI.Label(nextLabel, next.ToString(), "TextAmount");
  277. GUI.Label(atkLabel, atk.ToString(), "TextAmount");
  278. GUI.Label(defLabel, def.ToString(), "TextAmount");
  279. GUI.Label(agiLabel, agi.ToString(), "TextAmount");
  280. GUI.Label(intLabel, ints.ToString(), "TextAmount");
  281. GUI.Label(lucLable, luc.ToString());
  282. GUI.Label(weaponLable, guiWeaponCon, "TextItem");
  283. GUI.Label(armorLable, guiArmorCon, "TextItem");
  284. GUI.Label(accessLable, guiAccessCon, "TextItem");
  285. GUI.Label(skillLable, guiSkillCon, "TextItem");
  286. }
  287.  
  288. /// <summary>
  289. /// 选项卡二
  290. /// </summary>
  291. void ItemWindow()
  292. {
  293. int inItems = ;
  294. GUI.Box(rItemBox, "");
  295. GUI.Box(rTipBox, "");
  296.  
  297. //定义纵向滚动条
  298. scrollPosition = GUI.BeginScrollView(new Rect(, , , ), scrollPosition, new Rect(, , , * inItems));
  299. GUIContent[] itemsContent = new GUIContent[inItems];
  300. for (int i = ; i < inItems; i++)
  301. {
  302. if (items.Length > )
  303. {
  304. if (i == )
  305. {
  306. itemsContent[i] = new GUIContent(currentItem.itemName, currentItem.icon, "Test Hello World Hello WorldHello WorldHello WorldHello World ********************");
  307. }
  308. else
  309. {
  310. itemsContent[i] = new GUIContent(currentItem.itemName, currentItem.icon, "This key is" + i);
  311. }
  312. }
  313. else
  314. {
  315. itemsContent[i] = new GUIContent("None", "");
  316. }
  317. }
  318. inToolItem = GUI.SelectionGrid(new Rect(, , , * inItems), inToolItem, itemsContent, , GUI.skin.GetStyle("SelectedItem"));//SelectionGrid()选择表格,创建一个网格按钮
  319. GUI.EndScrollView();
  320. //下部分滚动条
  321. string sInfos = itemsContent[inToolItem].tooltip;
  322. if (string.IsNullOrEmpty(sInfos))
  323. {
  324. sInfos = "show items information here ";
  325. }
  326. GUIStyle style = GUI.skin.GetStyle("label");
  327.  
  328. if (GUI.tooltip != "")
  329. {
  330. float fHeight = style.CalcHeight(new GUIContent(GUI.tooltip), 330.0f);//计算高度
  331. scrollPosition2 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition2, new Rect(, , , fHeight));
  332. GUI.Label(new Rect(, , , fHeight), GUI.tooltip);
  333. }
  334. else
  335. {
  336. float fHeight = style.CalcHeight(new GUIContent(sInfos), 330.0f);//计算高度
  337.  
  338. scrollPosition2 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition2, new Rect(, , , fHeight));
  339. GUI.Label(new Rect(, , , fHeight), sInfos);
  340. }
  341. GUI.EndScrollView();
  342. }
  343.  
  344. /// <summary>
  345. /// 选项卡3
  346. /// </summary>
  347. void EquipWindow()
  348. {
  349. GUI.Box(equipBox, "");
  350. GUI.Box(equipWeanponBox, "");
  351. GUI.DrawTexture(statTextureEquip, tStatusBox2);
  352. GUI.DrawTexture(skillBoxEquip, tSkillBox);
  353.  
  354. SetupEuiqipBox();
  355. }
  356.  
  357. void SetupEuiqipBox()
  358. {
  359. GUIContent[] equipContent = { guiWeaponCon, guiArmorCon, guiAccessCon, guiSkillCon };
  360. for (int i = ; i < equipBool.Length; i++)
  361. {
  362. if (equipBool[i])
  363. {
  364. GUI.Label(equipRect[i], equipContent[i], "DisabledClick ");
  365. switch (i)
  366. {
  367. case :
  368. ShowWeapon();
  369. break;
  370. case :
  371. ShowArmor();//['ɑ:mə] 装甲;盔甲;
  372. break;
  373. case :
  374. ShowAccess();
  375. break;
  376. case :
  377. ShowSkill();
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. else
  384. {
  385. if (GUI.Button(equipRect[i], equipContent[i], "SelectedItem"))
  386. {
  387. equipBool[i] = true;
  388. for (int j = ; j < equipBool.Length; j++)
  389. {
  390. if (i != j)
  391. {
  392. equipBool[j] = false;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. }
  399.  
  400. void ShowWeapon()
  401. {
  402. int inItems = ;
  403. GUIContent[] itemsContent = new GUIContent[inItems];
  404. for (int i = ; i < inItems; i++)
  405. {
  406. if (i == )
  407. {
  408. itemsContent[i] = new GUIContent(uneuip, "");
  409. }
  410. else
  411. {
  412. itemsContent[i] = new GUIContent(weapons[].name, weapons[].icon, "");
  413. }
  414. }
  415. scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , * inItems));
  416. inToolWeapons = GUI.SelectionGrid(new Rect(, , , * inItems), inToolWeapons, itemsContent, , GUI.skin.GetStyle("SelectedItem"));
  417. GUI.EndScrollView();
  418. guiWeaponCon = itemsContent[inToolWeapons];
  419. }
  420. void ShowArmor()
  421. {
  422. int inItems = ;
  423. GUIContent[] itemsContent = new GUIContent[inItems];
  424. for (int i = ; i < inItems; i++)
  425. {
  426. if (i == )
  427. {
  428. itemsContent[i] = new GUIContent(uneuip, "");
  429. }
  430. else
  431. {
  432. itemsContent[i] = new GUIContent(armors[].name, armors[].icon, "");
  433. }
  434. }
  435. scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , * inItems));
  436. inToolArmors = GUI.SelectionGrid(new Rect(, , , * inItems), inToolWeapons, itemsContent, , GUI.skin.GetStyle("SelectedItem"));
  437. GUI.EndScrollView();
  438. guiWeaponCon = itemsContent[inToolArmors];
  439. }
  440. void ShowAccess()
  441. {
  442. int inItems = ;
  443. GUIContent[] itemsContent = new GUIContent[inItems];
  444. for (int i = ; i < inItems; i++)
  445. {
  446. if (i == )
  447. {
  448. itemsContent[i] = new GUIContent(uneuip, "");
  449. }
  450. else
  451. {
  452. itemsContent[i] = new GUIContent(access[].name, access[].icon, "");
  453. }
  454. }
  455. scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , * inItems));
  456. inToolAccess = GUI.SelectionGrid(new Rect(, , , * inItems), inToolWeapons, itemsContent, , GUI.skin.GetStyle("SelectedItem"));
  457. GUI.EndScrollView();
  458. guiWeaponCon = itemsContent[inToolAccess];
  459. }
  460. void ShowSkill()
  461. {
  462. int inItems = ;
  463. GUIContent[] itemsContent = new GUIContent[inItems];
  464. for (int i = ; i < inItems; i++)
  465. {
  466. if (i == )
  467. {
  468. itemsContent[i] = new GUIContent(tSkillBox);
  469. }
  470. else
  471. {
  472. itemsContent[i] = new GUIContent(skills[i-]);
  473. }
  474. }
  475. scrollPosition3 = GUI.BeginScrollView(new Rect(, , , ), scrollPosition3, new Rect(, , , ));
  476. inToolSkill = GUI.SelectionGrid(new Rect(, , , ), inToolWeapons, itemsContent, inItems, GUI.skin.GetStyle("SelectedItem"));
  477. GUI.EndScrollView();
  478. if (inToolSkill != )
  479. {
  480. guiSkillCon = itemsContent[inToolSkill];
  481. }
  482. else
  483. {
  484. guiSkillCon = new GUIContent("");
  485. }
  486. }
  487. }

二、有问题的效果

有个问题,还没解决就是GUI.Button() 没有执行,所以看下面效果就没有出来。。。

三、断点调式:

就是没有进入到Button方法下去。

怎么解决呢???

断点调式:

Unity3D笔记 GUI 四、实现选项卡三的更多相关文章

  1. Unity3D笔记 GUI 一

    要实现的功能: 1.个性化Windows界面 2.减少个性化的背景图片尺寸 3.个性化样式ExitButton和TabButton 4.实现三个选项卡窗口 一.个性化Windows界面 1.1.创建一 ...

  2. Unity3D笔记 GUI 三、实现选项卡二窗口

    实现目标: 1.使用个性化Box控件 2.个性化Lable控件 3.添加纵向滚动条 4.新建SelectedItem样式 一.最终效果: 二.主要代码 using UnityEngine; using ...

  3. Unity3D笔记 GUI 二 、实现选项卡一窗口

    实现目标: 1.个性化Box控件 2.新建TextAmount样式 3.新建TextItem样式 一.个性化Windows界面 设置GUI Skin 1.2 部分代码 Rect stateBox = ...

  4. Unity3D笔记 愤怒的小鸟<四> 实现Selelction界面

    一直跟着龚老师用js写,VS智能感应用习惯后发现这里用js对初学者比较蛋疼,MONO有提示但是还是无法和VS媲美就目前来看.所以这次还是换成熟悉的VS来开发. 目标:实现关卡页面 跑起来的效果如下: ...

  5. Unity3D笔记十四 力

    Unity中力的方式有两种:第一种为普通力,需要设定力的方向与大小:第二种为目标位置力,需要设定目标点的位置,该物体将朝向这个目标位置施加力. 1.案例 添加两个球体和一个立方体,另外还要给两个球体添 ...

  6. JavaWeb和WebGIS学习笔记(四)——使用uDig美化地图,并叠加显示多个图层

    系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...

  7. X-Cart 学习笔记(四)常见操作

    目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...

  8. opencv学习笔记(四)投影

    opencv学习笔记(四)投影 任选了一张图片用于测试,图片如下所示: #include <cv.h> #include <highgui.h> using namespace ...

  9. Memcached笔记——(四)应对高并发攻击【转】

    http://snowolf.iteye.com/blog/1677495 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...

随机推荐

  1. 【scala】 scala 映射和元组操作(四)

    1.映射  Map 定义 ,取值,遍历,排序 2. 元组 定义,取值,拉链操作 import scala.collection.mutable /** * 映射和元组 * * @author xwol ...

  2. [Scikit-learn] 1.4 Support Vector Machines - Linear Classification

    Outline: 作为一种典型的应用升维的方法,内容比较多,自带体系,以李航的书为主,分篇学习. 函数间隔和几何间隔 最大间隔 凸最优化问题 凸二次规划问题 线性支持向量机和软间隔最大化 添加的约束很 ...

  3. UpLoader------实现上传大文件

    代码: <div id="selectFile">选择文件1</div> <script> var da = newGuid(); var kk ...

  4. C#------如何使用Swagger调试接口

    1.打开NuGet程序包 2.安装下面两个程序包 3.安装完后会出现SwaggerConfig.cs类,并修改里面的内容 代码: [assembly: PreApplicationStartMetho ...

  5. javascript拖拽操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 有人在贴吧问phpmyadmin如何设置插入的时候默认插入1条记录

    在新版phpmyadmin中(我的版本是3.5.1) 插入的时候会提示插入两条,能够方便操作,让你多录入几条数据,如图 然而有人不想要这个界面默认插入两条,如何改为1条或者其他呢? 我审查了这个元素标 ...

  7. 【ArcGIS】ArcGIS Data Store配置

    一.错误提示 Unable to configure the ArcGIS Data Store with the GIS Server. Please make sure that the GIS ...

  8. 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, ...

  9. python文件操作笔记

    一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法. 1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 2.切换工作目录: os.c ...

  10. JQuery插件的使用

    今天就来总结一下jquery插件的使用,刚开始学习时间一直自己在写实现他的功能,现在发现jquery好强大啊,好多的功能都给我们封装好啦,jquery万岁,嘿嘿,下面就来简单的总结一下我们学习的: