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

实现目标:
  1、个性化Box控件
  2、新建TextAmount样式
  3、新建TextItem样式

一、个性化Windows界面

设置GUI Skin

 

1.2 部分代码

Rect stateBox = new Rect(237, 67, 360, 147);
Rect weaponBox = new Rect(237, 230, 360, 147);

GUI.Box(stateBox, "");
GUI.Box(weaponBox, "");//['wepən] n. 武器,兵器

1.3效果

二、新建TextAmount样式

  2.1、 代码

    #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窗口
        GUI.Box(stateBox, "");
        GUI.Box(weaponBox, "");//['wepən] n. 武器,兵器
        GUI.DrawTexture(box1Rect, box1);
        GUI.DrawTexture(box2Rect, box2);
        GUI.DrawTexture(box3Rect, box3);
        #endregion

  2.2、

 

2.3、效果

2.4、TextAmount

三、新建TextItem样式

 3.1、个性化数字的现实

3.2、代码:

using UnityEngine;
using System.Collections;

public class myTest : MonoBehaviour
{
    /// <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);
    }

    // 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:
                break;
            case 2:
                break;
            default:
                break;
        }
        #endregion

    }

    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");

    }

}

3.3、最终效果

 

posted @ 2014-02-22 22:52  PEPE YU  阅读(1431)  评论(0编辑  收藏  举报