public class ComboBox{public int index;//索引public string weight="";//权重private int labelWidth = 60;//标题的宽public float Weight{get { return float.Parse(weight); }}/// <summary>/// 复选框的宽度/// </summary>public const int WIDTH = 150;/// <summary>/// 复选框的高度/// </summary>public const int Height = 20;/// <summary>/// Lable/// </summary>public string label = "";public int viewAreaHeight = 200;public ComboBox(Dictionary<object, object> dataSource,int viewHeight = 200){if (dataSource == null){dataSource = new Dictionary<object, object>();for (int i = 0; i < 50; i++){dataSource.Add(i, string.Format("<color=#ff0000ff>请初始化数据 {0}</color>" ,i));}}viewAreaHeight = viewHeight;this.Init(dataSource,1);}private Dictionary<object, object> dataSource;private object currentValue;private object currentDisplayText;private int currentIndex;private bool showList;private Vector2 scrollPosition;private float showListTime;private int guiDepth;private bool alreadyInitGuiDepth;/// <summary>/// 选择项更改事件参数 /// </summary>public class SelectItemChangedArgs : EventArgs{private object itemValue;private object itemDisplayText;public object ItemValue{get{return itemValue;}}public object ItemDisplayText{get{return itemDisplayText;}}public SelectItemChangedArgs(object iValue, object iDisplayText){itemValue = iValue;itemDisplayText = iDisplayText;}}/// <summary>/// 选择项更改事件 /// </summary>public event EventHandler<SelectItemChangedArgs> OnSelectItemChanged;public object SelectValue{get { return currentValue; }}public object SelectDisplayText{get { return currentDisplayText; }}public float ShowListTime{get { return showListTime; }}/// <summary>/// 数据源 /// </summary>public Dictionary<object, object> DataSource{set{dataSource = value;currentDisplayText = null;currentValue = null;}get{return dataSource;}}/// <summary>/// 初始化各项参数,控件应该只调用一次此方法,进行重置数据源直接使用DataSource属性即可 /// </summary>/// <param name="data">/// A <see cref="Dictionary<System.Object, System.Object>"/>/// </param>/// <param name="depth">/// A <see cref="System.Int32"/>/// </param>public void Init(Dictionary<object, object> data, int depth){dataSource = data;currentIndex = -1;//将控件置于当前GUI元素之上,并且只在第一次调用初始化方法设置GUI深度,防止循环中重复调用if (!alreadyInitGuiDepth){//guiDepth = GUI.depth - 1;alreadyInitGuiDepth = true;}currentDisplayText = null;currentValue = null;}/// <summary>/// 强行设置下拉列表是否是示 /// </summary>/// <param name="show">/// A <see cref="System.Boolean"/>/// </param>public void SetShowList(bool show){if (showList){showList = show;}}/// <summary>/// 绘制下拉列表框 /// </summary>public void Draw(){if (dataSource == null){return;}if (currentDisplayText == null || currentValue == null){if (dataSource.Count > 0){currentDisplayText = dataSource.Values.First();currentValue = dataSource.Keys.First();currentIndex = 0;}else{currentDisplayText = "无";currentValue = -1;currentIndex = -1;}}GUILayout.BeginVertical(GUILayout.Width(WIDTH));GUILayout.BeginHorizontal();int spaceW = labelWidth;if (label == null || label.Equals("")){spaceW = 0;}GUILayout.Label(label, GUILayout.Width(spaceW));string buttonName = "";if(currentDisplayText == null){buttonName = "无";}else if(currentDisplayText is monster_model){buttonName = ((monster_model)currentDisplayText).name;}else if(currentDisplayText is string){buttonName = currentDisplayText.ToString();}buttonName += (showList ? "   <color=#f27272FF>></color>" : "   <color=#f27272FF>V</color>");if (GUILayout.Button(buttonName,GUILayout.Width(WIDTH + 20))){showList = !showList;if (showList){showListTime = Time.time;}else{showListTime = 0f;}}GUILayout.EndHorizontal();this.DrawList();GUILayout.EndVertical();}/// <summary>/// 绘制列表项/// </summary>public void DrawList(){if (showList == true){//为了留出最方下的横线,这里高度减1GUILayout.BeginHorizontal();int spaceW = labelWidth;if (label == null || label.Equals("")){spaceW = 0;}GUILayout.Label("", GUILayout.Width(spaceW));scrollPosition = Vector2.Lerp(scrollPosition, GUILayout.BeginScrollView(scrollPosition, true, true,GUILayout.Width(WIDTH+20),GUILayout.Height(this.viewAreaHeight)), 0.5f);for (int i = 0; i < dataSource.Count; i++){drawItem(i);}GUILayout.EndScrollView();GUILayout.EndHorizontal();}}/// <summary>/// 绘制内容项,并响应事件 /// </summary>/// <param name="index">/// A <see cref="System.Int32"/>/// </param>private void drawItem(int index){object value = dataSource.Values.ElementAt(index);string buttonName = value as string;if(value is monster_model){buttonName = ((monster_model)value).name;}if (GUILayout.Button(buttonName,GUILayout.Width(WIDTH))){if(value is monster_model){currentDisplayText = ((monster_model)value).name;currentValue = dataSource.Keys.ElementAt(index);}else{currentDisplayText = value;currentValue = dataSource.Keys.ElementAt(index);}if (currentIndex != index){currentIndex = index;if (OnSelectItemChanged != null){if(value is monster_model){OnSelectItemChanged(this, new SelectItemChangedArgs(currentValue, value));}else{OnSelectItemChanged(this, new SelectItemChangedArgs(currentValue, currentDisplayText));}}}showList = false;}}public void OnGUI(){GUI.depth = guiDepth;this.Draw();}/// <summary>/// 按时间比较各ComboBox控件的顺序,初衷是为了点击新的控件时,其它ComboBox控件下拉列表自动隐藏 /// </summary>public class ShowListComparer : IComparer<ComboBox>{#region IComparer[ComboBox] implementationint IComparer<ComboBox>.Compare(ComboBox x, ComboBox y){return x.showListTime.CompareTo(y.showListTime) * -1;}#endregion}}

先说明一下:这个功能是在一位大神的GUI的代码基础上修改的,这样就可以自动排版了,不过已经忘了是哪位了。

转载于:https://www.cnblogs.com/yixuan1980/p/6648411.html

Unity运行模式用GUILayout实现ComboBox相关推荐

  1. Unity运行模式下修改代码保存后报错

    原因可能是,Unity偏好设置常规这里选择了第1个,推荐选择第2个,等编辑器停止运行后再重新编译 三个选项的含义: 重新编译并继续播放: 在完成播放后重新编译: 停止播放并重新编译:

  2. Unity运行时刻资源管理

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/03/2998157.html Unity运行时刻资源管理 ------------------ ...

  3. unity3d 非运行模式下执行脚本

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...

  4. 【Unity3D插件】RuntimeTransformGizmos插件分享《Unity运行时控制模型插件》

    推荐阅读 CSDN主页 GitHub开源地址 Unity3D插件分享 简书地址 我的个人博客 QQ群:1040082875 一.前文   Runtime Transform Gizmos看名字顾名思义 ...

  5. 2021年大数据Spark(四):三种常见的运行模式

    目录 Spark 运行模式 一.本地模式:Local Mode 二.集群模式:Cluster Mode 三.云服务:Kubernetes 模式 ​​​​​​​ Spark 运行模式 Spark 框架编 ...

  6. Hadoop运行模式 之 伪分布式运行模式

    什么是伪分布式模式?它与本地运行模式以及完全分布式模式有什么区别? 伪分布式的配置信息,完全是按照完全分布式的模式去搭建的,但是它只有一台服务器,可以用于学习和测试,真正的开发中不可以使用. 目录 一 ...

  7. Hadoop运行模式 之 本地运行模式

    Hadoop的运行模式包括:本地模式.伪分布式模式以及完全分布式模式 Hadoop官网地址:https://hadoop.apache.org/ 本次使用的Hadoop的版本是2.7.2 官网文档:h ...

  8. PHPWAMP集成环境Zend组件的相关介绍,环境默认的PHP运行模式

    目前最新版的PHPWAMP集成环境默认都安装了zend解密组件,下面是详情信息. PHP线程安全和非线程安全的选择和区别.  备注:在PHP5.3以后,PHP不再有ISAPI模式,安装后也不再有php ...

  9. Spark运行模式Local+Standalone+Yarn+mesos

    Spark运行模式Local+Standalone+Yarn+mesos bin/spark-submit --help 注意: --master MASTER_URL spark://host:po ...

最新文章

  1. java图片文件字符串jsp_将图片转成base64字符串并在JSP页面显示的Java代码
  2. 嫉恨别人不如审视自己
  3. antd select 下拉菜单的 style 属性_如何优雅地彻底解决 antd 全局样式问题
  4. Webx示例-PetStore分析1
  5. python中的全局变量和局部变量
  6. 往ABAP gateway system上和Cloud Foundry上部署HTML5应用
  7. Oracle从小白到大牛的刷题之路(建议收藏学习)
  8. 快应用quickapp快速入门教程 by五个半柠檬
  9. 在写代码过程中遇到的问题,以及当时的解决方法(如实记录)
  10. c 调用matlab.m文件,ubuntu系统下C++调用matlab程序的方法详解
  11. CENTOS 6.6初始化SHELL脚本
  12. 打造网站克隆、仿站工具、含源码(.NET CORE + selenium + chromedriver)
  13. 12套员工激励方法和108个技巧
  14. win11蓝牙连接耳机有回音的问题
  15. Batch Normalization反方向传播求导
  16. ECharts动态图表展示
  17. 工程管理系统源码-专注项目数字化管理-工程管理
  18. java 持久监听blockqueue的变化_Curator目录监听
  19. 计算机辅助手段在英语教学中使用,试谈计算机在英语教学中的辅助作用
  20. 爱克发胶片_AGFA一次性胶片相机——记录第一次拍胶片的“扑街”现场

热门文章

  1. TCP协议的部分解析
  2. 应用于3D图形制作环境的_JITStack轻量级GPU云桌面
  3. 开辟新战场,打造新的商业模式
  4. tlias 教学过程_全方位提升授课质量,传智专修学院让学生实现快速成长
  5. UITextField实现给定词搜索,跳转页面
  6. ME909S-821 4G-LTE模块在Linux系统下拨号上网测试
  7. 数据基础---mysql数据库操作(一)---基础操作
  8. 清华大学计算机学院公开课,清华大学公开课
  9. 为什么你的问题群里没人回答?这就是原因!
  10. 计算机语言发展浅层笔记