实现目标:
1、使用个性化Box控件
2、个性化Lable控件
3、添加纵向滚动条
4、新建SelectedItem样式
一、最终效果:
二、主要代码
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 < 25) { while (this.name.Length < 17) { this.name += " "; } } if (iLength < 10) { 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(237, 67, 360, 247); Rect rTipBox = new Rect(237, 330, 360, 107); Rect rItemsBox = new Rect(257, 350, 340, 87); Rect rTipButton = new Rect(257, 350, 340, 87); Rect rVerScroll = new Rect(600, 87, 20, 227); float fScrollPos = 1.0f; Vector2 scrollPosition = Vector2.zero;//写Vector2(0, 0)的简码 Vector2 scrollPosition2 = Vector2.zero; public Item[] items; Item currentItem; int inToolItem = 0; #endregion /// <summary> /// 是否打开窗口 /// </summary> bool isOpenMenu = false; /// <summary> /// 窗体的大小和位置【矩形】 /// </summary> Rect myWindow = new Rect(10, 10, 640, 480); /// <summary> /// GUI Skin /// </summary> public GUISkin customSkin; /// <summary> /// 关闭按钮 /// </summary> Rect closeButton = new Rect(598, 8, 26, 22); /// <summary> /// 用于工具栏在屏幕上的矩形位置 /// </summary> Rect tabButton = new Rect(35, 15, 490, 40); /// <summary> /// 选项卡索引号 /// </summary> int toolsCount = 0; /// <summary> /// 显示在工具栏按钮上的字符串数组 /// </summary> string[] toolsName = { "选项卡1", "选项卡2", "选项卡3" }; /// <summary> /// 选项卡中的图片 /// </summary> public Texture img; /// <summary> /// 选项卡中的图片位置 /// </summary> Rect imgRect = new Rect(19, 35, 225, 441); #region 个性化Box控件 Rect stateBox = new Rect(237, 67, 360, 147); Rect weaponBox = new Rect(237, 230, 360, 147); public Texture box1; public Texture box2; public Texture box3; Rect box1Rect = new Rect(252, 77, 331, 125); Rect box2Rect = new Rect(252, 244, 331, 125); Rect box3Rect = new Rect(460, 284, 117, 125); #endregion #region 实现Status窗口 选项卡一 GUIContent guiWeaponCon = new GUIContent(); GUIContent guiArmorCon = new GUIContent(); GUIContent guiAccessCon = new GUIContent(); GUIContent guiSkillCon = new GUIContent(); Rect weaponLable = new Rect(252, 240, 180, 40); Rect armorLable = new Rect(252, 340, 180, 40); Rect accessLable = new Rect(252, 360, 180, 40); Rect skillLable = new Rect(252, 288, 180, 40); string uneuip = "Hello"; Rect hpLabel = new Rect(313, 70, 120, 25); Rect mpLabel = new Rect(313, 102, 120, 25); Rect lvLabel = new Rect(313, 120, 120, 25); Rect expLabel = new Rect(313, 150, 120, 25); Rect nextLabel = new Rect(313, 176, 120, 25); Rect atkLabel = new Rect(529, 71, 50, 25); Rect defLabel = new Rect(529, 100, 50, 25); Rect agiLabel = new Rect(529, 120, 50, 25); Rect intLabel = new Rect(529, 150, 50, 25); Rect lucLable = new Rect(529, 180, 50, 25); int fullHP = 9999; int fullMP = 99999; int hp = 9999; int mp = 999; int lv = 90; int exp = 1000; int next = 909; int atk = 123; int def = 345; int agi = 444; int ints = 666; int luc = 8888; #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>0) { items[0].SetUpItemName(); currentItem = items[0]; } } // 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 = 0; 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, 0, Screen.width - myWindow.width);// [klæmp] vt.& vi. 夹紧,夹住;锁住;把(砖等)堆高 myWindow.y = Mathf.Clamp(myWindow.y, 0, 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 0: StatusWindow(); break; case 1: ItemWindow(); break; case 2: 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.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 = 8; GUI.Box(rItemBox, ""); GUI.Box(rTipBox, ""); //定义纵向滚动条 scrollPosition = GUI.BeginScrollView(new Rect(257, 87, 320, 200), scrollPosition, new Rect(0, 0, 280, 40 * inItems)); GUIContent[] itemsContent = new GUIContent[inItems]; for (int i = 0; i < inItems; i++) { if (items.Length > 0) { if (i == 0) { 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(0, 0, 280, 40 * inItems), inToolItem, itemsContent, 1, 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(257, 343, 320, 75), scrollPosition2, new Rect(0, 0, 280, fHeight)); GUI.Label(new Rect(0, 0, 280, fHeight), GUI.tooltip); } else { float fHeight = style.CalcHeight(new GUIContent(sInfos), 330.0f);//计算高度 scrollPosition2 = GUI.BeginScrollView(new Rect(257, 343, 320, 75), scrollPosition2, new Rect(0, 0, 280, fHeight)); GUI.Label(new Rect(0, 0, 280, fHeight), sInfos); } GUI.EndScrollView(); } }
注意:
龚老师是用js写的,所以那个类Item直接在Unity3D Inspector中显示了,但是C#要注意了 ,在需要的类前面加一个特性[System.Serializable]js继承Object类默认就是会被序列化。