No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearcherExampleComponentEditor.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.Searcher
  6. {
  7. [CustomEditor(typeof(SearcherExampleComponent))]
  8. class SearcherExampleComponentEditor : Editor
  9. {
  10. Rect m_ButtonRect;
  11. public override void OnInspectorGUI()
  12. {
  13. var root = new SearcherItem("Root");
  14. var children = new List<SearcherItem>();
  15. for (var i = 0; i < 10; ++i)
  16. children.Add(new SearcherItem("B-" + i));
  17. var child = new SearcherItem("Child","", children);
  18. root.AddChild(child);
  19. {
  20. EditorGUILayout.BeginHorizontal();
  21. EditorGUILayout.PrefixLabel("Mouse Position");
  22. if (EditorGUILayout.DropdownButton(new GUIContent("Button"), FocusType.Passive, GUI.skin.button))
  23. {
  24. var editorWindow = EditorWindow.focusedWindow;
  25. var localMousePosition = Event.current.mousePosition;
  26. var worldMousePosition = editorWindow.rootVisualElement.LocalToWorld(localMousePosition);
  27. SearcherWindow.Show(
  28. editorWindow,
  29. new List<SearcherItem> { root },
  30. "Mouse Position",
  31. item => {
  32. Debug.Log("Searcher item selected: " + (item?.Name ?? "<none>"));
  33. return true;
  34. },
  35. worldMousePosition);
  36. }
  37. EditorGUILayout.EndHorizontal();
  38. }
  39. {
  40. EditorGUILayout.BeginHorizontal();
  41. EditorGUILayout.PrefixLabel("Button Center");
  42. var selected = EditorGUILayout.DropdownButton(new GUIContent("Button"), FocusType.Passive, GUI.skin.button);
  43. if (Event.current.type == EventType.Repaint)
  44. m_ButtonRect = GUILayoutUtility.GetLastRect();
  45. if (selected)
  46. {
  47. var editorWindow = EditorWindow.focusedWindow;
  48. var localButtonCenter = m_ButtonRect.center;
  49. var worldButtonCenter = editorWindow.rootVisualElement.LocalToWorld(localButtonCenter);
  50. SearcherWindow.Show(
  51. editorWindow,
  52. new List<SearcherItem> { root },
  53. "Button Center",
  54. item => {
  55. Debug.Log("Searcher item selected: " + (item?.Name ?? "<none>"));
  56. return true;
  57. },
  58. worldButtonCenter);
  59. }
  60. EditorGUILayout.EndHorizontal();
  61. }
  62. }
  63. }
  64. }