从此类继承以实现自己的下拉控件。
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 | 重写此方法可在选择项时获得通知。 |