PopupWindow
弹出窗口没有边距,不可拖动,也无法调节大小。此外,它们还会在失去焦点时自动关闭。它们用于显示短暂的信息或选项。
using UnityEngine;
using UnityEditor;
public class PopWindowExample : EditorWindow
{
private Rect buttonRect;
PopupExample example = new PopupExample();
[MenuItem("Tools/PopWindowExample")]
static void Open()
{
EditorWindow.GetWindow<PopWindowExample>().Show();
}
private void OnGUI()
{
GUILayout.Label("Editor window with Popup example", EditorStyles.boldLabel);
if (GUILayout.Button("Popup Options", GUILayout.Width(200)))
{
PopupWindow.Show(buttonRect, example);
}
GUILayout.Label( "Selection == " + example.Str);
if (Event.current.type == EventType.Repaint)
{
buttonRect = GUILayoutUtility.GetLastRect();
}
}
//继承PopupWindowContent
public class PopupExample : PopupWindowContent
{
private bool toggle1 = true;
private bool toggle2 = true;
private bool toggle3 = true;
public string Str
{
get
{
string str = "";
if (toggle1)
{
str += 1;
}
if (toggle2)
{
str += 2;
}
if (toggle3)
{
str += 3;
}
return str;
}
}
public override Vector2 GetWindowSize()
{
return new Vector2(200, 150);
}
//绘制
public override void OnGUI(Rect rect)
{
GUILayout.Label("Popup Options Example", EditorStyles.boldLabel);
toggle1 = EditorGUILayout.Toggle("Toggle 1", toggle1);
toggle2 = EditorGUILayout.Toggle("Toggle 2", toggle2);
toggle3 = EditorGUILayout.Toggle("Toggle 3", toggle3);
}
//打开
public override void OnOpen()
{
Debug.Log("Popup opened: " + this);
}
//关闭
public override void OnClose()
{
Debug.Log("Popup closed: " + this);
}
}
}
AdvancedDropdown
可搜索的下拉控件。
using UnityEditor;
using UnityEngine;
using UnityEditor.IMGUI.Controls;
class WeekdaysDropdown : AdvancedDropdown
{
public string selectStr;
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;
}
//选择回调
protected override void ItemSelected(AdvancedDropdownItem item)
{
base.ItemSelected(item);
selectStr = item.name;
}
}
public class AdvancedDropdownTestWindow : EditorWindow
{
WeekdaysDropdown dropdown = new WeekdaysDropdown(new AdvancedDropdownState());
[MenuItem("Tools/AdvancedDropdownTestWindow")]
static void Open()
{
EditorWindow.GetWindow<AdvancedDropdownTestWindow>().Show();
}
void OnGUI()
{
var rect = GUILayoutUtility.GetRect(new GUIContent("Show"), EditorStyles.toolbarButton);
if (GUI.Button(rect, new GUIContent("Show"), EditorStyles.toolbarButton))
{
dropdown.Show(rect);
}
GUILayout.Label(dropdown.selectStr);
}
}
编辑器免费课程:
独立游戏《Unity打造关卡编辑器》Unity 独立游戏 关卡编辑器https://bycwedu.vipwan.cn/promotion_channels/72762192