首页 > 代码库 > Unity运行模式用GUILayout实现ComboBox
Unity运行模式用GUILayout实现ComboBox
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 = http://www.mamicode.com/iValue;"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 = http://www.mamicode.com/null;"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 =http://www.mamicode.com/= null)"无"; currentValue = http://www.mamicode.com/-1;"")) { 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) { //为了留出最方下的横线,这里高度减1 GUILayout.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 = http://www.mamicode.com/dataSource.Values.ElementAt(index);>
先说明一下:这个功能是在一位大神的GUI的代码基础上修改的,这样就可以自动排版了,不过已经忘了是哪位了。
Unity运行模式用GUILayout实现ComboBox
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。