Version: 2022.3
public void AddSeparator (string path);

参数

path 到子菜单的路径(如果向子菜单添加分隔符)。在向菜单顶层添加分隔符时,请使用空字符串作为路径。

描述

向菜单添加一个分隔符项。

// This example shows how to create a context menu inside a custom EditorWindow.
using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow { [MenuItem("TestContextMenu/Open Window")] public static void Init() { var window = GetWindow(typeof(MyWindow)); window.position = new Rect(50, 50, 250, 60); window.Show(); }

public void Callback(object obj) { Debug.Log("Selected: " + obj); }

public void OnGUI() { Event evt = Event.current; Rect contextRect = new Rect(10, 10, 100, 100);

if (evt.type == EventType.ContextClick) { Vector2 mousePos = evt.mousePosition; if (contextRect.Contains(mousePos)) { // Now create the menu, add items and show it GenericMenu menu = new GenericMenu();

menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1"); menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2"); menu.AddSeparator(""); menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3"); menu.AddItem(new GUIContent("SubMenu/MenuItem4"), false, Callback, "item 4"); menu.AddItem(new GUIContent("SubMenu/MenuItem5"), false, Callback, "item 5"); menu.AddSeparator("SubMenu/"); menu.AddItem(new GUIContent("SubMenu/MenuItem6"), false, Callback, "item 6");

menu.ShowAsContext();

evt.Use(); } } } }