继承自此类以实现您自己的下拉控件。
using UnityEditor; using UnityEngine; using UnityEditor.IMGUI.Controls;
class WeekdaysDropdown : AdvancedDropdown { public WeekdaysDropdown(AdvancedDropdownState state) : base(state) { }
protected override AdvancedDropdownItem BuildRoot() { var root = new AdvancedDropdownItem("Weekdays");
var firstHalf = new AdvancedDropdownItem("First half"); var secondHalf = new AdvancedDropdownItem("Second half"); var weekend = new AdvancedDropdownItem("Weekend");
firstHalf.AddChild(new AdvancedDropdownItem("Monday")); firstHalf.AddChild(new AdvancedDropdownItem("Tuesday")); secondHalf.AddChild(new AdvancedDropdownItem("Wednesday")); secondHalf.AddChild(new AdvancedDropdownItem("Thursday")); weekend.AddChild(new AdvancedDropdownItem("Friday")); weekend.AddChild(new AdvancedDropdownItem("Saturday")); weekend.AddChild(new AdvancedDropdownItem("Sunday"));
root.AddChild(firstHalf); root.AddChild(secondHalf); root.AddChild(weekend);
return root; } }
public class AdvancedDropdownTestWindow : EditorWindow { void OnGUI() { var rect = GUILayoutUtility.GetRect(new GUIContent("Show"), EditorStyles.toolbarButton); if (GUI.Button(rect, new GUIContent("Show"), EditorStyles.toolbarButton)) { var dropdown = new WeekdaysDropdown(new AdvancedDropdownState()); dropdown.Show(rect); } } }
minimumSize | 下拉窗口的最小尺寸。默认情况下,下拉窗口将尝试匹配给定矩形或渲染内容的宽度。 |
Show | 调用此方法以在给定位置显示下拉菜单。 |
BuildRoot | 实现此方法以生成下拉菜单的内容。此方法在显示下拉菜单时调用。 |
ItemSelected | 重写此方法以在选择项目时收到通知。 |