实现目标:

  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 < )
{
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;
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 // 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 :
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 = ;
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();
}
}

注意:

  龚老师是用js写的,所以那个类Item直接在Unity3D Inspector中显示了,但是C#要注意了 ,在需要的类前面加一个特性[System.Serializable]js继承Object类默认就是会被序列化。

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

  1. Unity3D笔记 GUI 四、实现选项卡三

    一.代码: using UnityEngine; using System.Collections; /// <summary> /// 选项卡二 /// </summary> ...

  2. Unity3D笔记 GUI 一

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

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

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

  4. Unity3D笔记 愤怒的小鸟<三> 实现Play界面2

    前言:在Play页面中给Play页面添加一个“开始游戏”和“退出游戏”按钮顺便再来一个背景音乐 添加按钮可以是GUI.Button(),也可以是GUILayout.Button():给图片添加按钮可以 ...

  5. Unity3D笔记六 GUI游戏界面

    1.Label:标签控件,可以在游戏中用来展示文本字符串信息,不仅可以写字还可以贴图片. 2.Button:按钮控件,一般分图片按钮和普通的按钮,还有一个连续按钮RepeatButton注意,这个在W ...

  6. PyQt4入门学习笔记(三)

    # PyQt4入门学习笔记(三) PyQt4内的布局 布局方式是我们控制我们的GUI页面内各个控件的排放位置的.我们可以通过两种基本方式来控制: 1.绝对位置 2.layout类 绝对位置 这种方式要 ...

  7. Android群英传笔记——第三章:Android控件架构与自定义控件讲解

    Android群英传笔记--第三章:Android控件架构与自定义控件讲解 真的很久没有更新博客了,三四天了吧,搬家干嘛的,心累,事件又很紧,抽时间把第三章大致的看完了,当然,我还是有一点View的基 ...

  8. JavaWeb和WebGIS学习笔记(三)——GeoServer 发布shp数据地图

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

  9. C#可扩展编程之MEF学习笔记(三):导出类的方法和属性

    前面说完了导入和导出的几种方法,如果大家细心的话会注意到前面我们导出的都是类,那么方法和属性能不能导出呢???答案是肯定的,下面就来说下MEF是如何导出方法和属性的. 还是前面的代码,第二篇中已经提供 ...

随机推荐

  1. JSP求和计算

    已知两个数的值,如何求和并输出? <%@ page language="java" import="java.util.*,java.text.*" co ...

  2. nagios安装check_linux_stats.pl插件报错Can't locate Sys/Statistics/Linux.pm in @INC的处理?

    问题描述: 今天想有没有监控主机内存的插件可以供nagios来使用,然后找到一个插件check_linux_stats.pl 但是在将脚本上传之后,执行的时候报错 [root@testvm02 lib ...

  3. centos7 安装 最新版本的docker

    yum update # vim /etc/yum.repos.d/docker.repo //添加以下内容 [dockerrepo] name=Docker Repository baseurl=h ...

  4. iOS去掉icon的(自带磨光效果)gloss effects

    只需两步,第一步:在项目的plist文件,最上层add row ,内容 icon already includes gloss effects   YES. 第二步在 icon files 字段里添加 ...

  5. PMP模拟考试-2

    1. Increasing resources on the critical path activities may not always shorten the length of the pro ...

  6. grid简单布局

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

  7. springboot测试service层的单元测试

    package com.test.service; import com.task.Application;import com.task.model.po.TaskRecordDo;import o ...

  8. Spring和junit测试之配置文件路径

    本人在测试一个方法时需要加载XML配置文件,spring提供了相应的方法,就小小研究了下,在此记录下具体的过程,方便初学者和自己日后回顾. Spring容器最基本的接口就是BeanFactory. B ...

  9. ANDROID – 單色漸層效果的改良 – GRADIENT SCRIMS(转)

    本篇是根據 +Roman Nurik 在 2014/11/24 發佈的一篇 G+ 而來.看到他發文後,起了好奇心,就根據他提出的方法嘗試著實作,並將之排列呈現,直接從視覺上做個比較. 他在 G+ 的發 ...

  10. 【2014年12月6日】HR交流会

    今天的交流会感觉还是不错,体会到了一些东西,把它记下来. 想到什么写什么,可能没什么条理. 1.先选行业,再选职业,再选公司 根据自己的兴趣以及个人特长,能力等方面,需要定一个大概的方向,然后根据方向 ...