• 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 < 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;
        Vector2 scrollPosition3 = 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
    
    
    
        #region 选项卡3
        Rect equipBox = new Rect(237, 67, 360, 207);
        Rect equipWeanponBox = new Rect(237, 280, 360, 157);
        Rect statTextureEquip = new Rect(252, 81, 331, 142);
        Rect skillBoxEquip = new Rect(460, 121, 127, 125);
        Rect[] equipRect = { new Rect(252, 101, 180, 40), new Rect(252, 161, 180, 40), new Rect(252, 221, 180, 40), new Rect(252, 281, 180, 40) };
        Rect equipWindow = new Rect(500, 0, 70, 100);
        //Vector2 scrollPosition = Vector2.zero;
        bool[] equipBool = new bool[4];
        int inToolWeapons = 0;
        int inToolArmors = 0;
        int inToolAccess = 0;
        int inToolSkill = 0;
        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 > 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:
                    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 = 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();
        }
    
        /// <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 = 0; i < equipBool.Length; i++)
            {
                if (equipBool[i])
                {
                    GUI.Label(equipRect[i], equipContent[i], "DisabledClick ");
                    switch (i)
                    {
                        case 0:
                            ShowWeapon();
                            break;
                        case 1:
                            ShowArmor();//['ɑ:mə]  装甲;盔甲;
                            break;
                        case 2:
                            ShowAccess();
                            break;
                        case 3:
                            ShowSkill();
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    if (GUI.Button(equipRect[i], equipContent[i], "SelectedItem"))
                    {
                        equipBool[i] = true;
                        for (int j = 0; j < equipBool.Length; j++)
                        {
                            if (i != j)
                            {
                                equipBool[j] = false;
                            }
                        }
                    }
                }
            }
        }
    
        void ShowWeapon()
        {
            int inItems = 6;
            GUIContent[] itemsContent = new GUIContent[inItems];
            for (int i = 0; i < inItems; i++)
            {
                if (i == 0)
                {
                    itemsContent[i] = new GUIContent(uneuip, "");
                }
                else
                {
                    itemsContent[i] = new GUIContent(weapons[0].name, weapons[0].icon, "");
                }
            }
            scrollPosition3 = GUI.BeginScrollView(new Rect(257, 300, 320, 120), scrollPosition3, new Rect(0, 0, 280, 40 * inItems));
            inToolWeapons = GUI.SelectionGrid(new Rect(0, 0, 280, 40 * inItems), inToolWeapons, itemsContent, 1, GUI.skin.GetStyle("SelectedItem"));
            GUI.EndScrollView();
            guiWeaponCon = itemsContent[inToolWeapons];
        }
        void ShowArmor()
        {
            int inItems = 6;
            GUIContent[] itemsContent = new GUIContent[inItems];
            for (int i = 0; i < inItems; i++)
            {
                if (i == 0)
                {
                    itemsContent[i] = new GUIContent(uneuip, "");
                }
                else
                {
                    itemsContent[i] = new GUIContent(armors[0].name, armors[0].icon, "");
                }
            }
            scrollPosition3 = GUI.BeginScrollView(new Rect(257, 300, 320, 120), scrollPosition3, new Rect(0, 0, 280, 40 * inItems));
            inToolArmors = GUI.SelectionGrid(new Rect(0, 0, 280, 40 * inItems), inToolWeapons, itemsContent, 1, GUI.skin.GetStyle("SelectedItem"));
            GUI.EndScrollView();
            guiWeaponCon = itemsContent[inToolArmors];
        }
        void ShowAccess()
        {
            int inItems = 6;
            GUIContent[] itemsContent = new GUIContent[inItems];
            for (int i = 0; i < inItems; i++)
            {
                if (i == 0)
                {
                    itemsContent[i] = new GUIContent(uneuip, "");
                }
                else
                {
                    itemsContent[i] = new GUIContent(access[0].name, access[0].icon, "");
                }
            }
            scrollPosition3 = GUI.BeginScrollView(new Rect(257, 300, 320, 120), scrollPosition3, new Rect(0, 0, 280, 40 * inItems));
            inToolAccess = GUI.SelectionGrid(new Rect(0, 0, 280, 40 * inItems), inToolWeapons, itemsContent, 1, GUI.skin.GetStyle("SelectedItem"));
            GUI.EndScrollView();
            guiWeaponCon = itemsContent[inToolAccess];
        }
        void ShowSkill()
        {
            int inItems = 6;
            GUIContent[] itemsContent = new GUIContent[inItems];
            for (int i = 0; i < inItems; i++)
            {
                if (i == 0)
                {
                    itemsContent[i] = new GUIContent(tSkillBox);
                }
                else
                {
                    itemsContent[i] = new GUIContent(skills[i-1]);
                }
            }
            scrollPosition3 = GUI.BeginScrollView(new Rect(253, 286, 330, 140), scrollPosition3, new Rect(0, 0, 600, 117));
            inToolSkill = GUI.SelectionGrid(new Rect(0, 4, 600, 117), inToolWeapons, itemsContent, inItems, GUI.skin.GetStyle("SelectedItem"));
            GUI.EndScrollView();
            if (inToolSkill != 0)
            {
                guiSkillCon = itemsContent[inToolSkill];
            }
            else
            {
                guiSkillCon = new GUIContent("");
            }
        }
    }

    二、有问题的效果

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

    三、断点调式:

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

     怎么解决呢???

     断点调式:


    作者:PEPE
    出处:http://pepe.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    每日总结6.14
    每日总结6.13
    每日总结6.12
    每日总结6.11
    用户故事与敏捷方法阅读笔记4
    用户故事与敏捷方法阅读笔记3
    团队冲刺第一阶段燃尽图
    团队冲刺10
    智能物联网:将人工智能的力量带入物联网
    MyEclipse修改文件后Building workspace时间过长
  • 原文地址:https://www.cnblogs.com/PEPE/p/3562326.html
Copyright © 2020-2023  润新知