Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example:
- // List<Type> items = GetAllSubclasses(typeof(ScriptableObject)).ToList();
- // GenericPopupContent<Type>.Show(items, OnScriptableObjectSelected, type => type.Name);
- // private void OnScriptableObjectSelected(Type selectedType)
- // {
- // _type = selectedType;
- // Repaint();
- // }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- namespace Game.EditorWindows
- {
- public class GenericPopupContent<T> : PopupWindowContent
- {
- private string _searchFilter = "";
- private readonly List<T> _items;
- private readonly Action<T> _onSelectCallback;
- private readonly Func<T, string> _nameSelector;
- private Vector2 _scrollView;
- private GenericPopupContent(List<T> items, Action<T> onSelect, Func<T, string> nameSelector = default)
- {
- _nameSelector = nameSelector ?? (t => t.ToString());
- _items = items;
- _onSelectCallback = onSelect;
- }
- public static void Show(List<T> items, Action<T> onSelect, Func<T, string> nameSelector = default)
- {
- var content = new GenericPopupContent<T>(items, onSelect, nameSelector);
- PopupWindow.Show(new Rect(Event.current.mousePosition, Vector2.zero), content);
- }
- public override void OnGUI(Rect rect)
- {
- EditorGUILayout.Space();
- _searchFilter = EditorGUILayout.TextField(_searchFilter, (GUIStyle)"SearchTextField");
- EditorGUILayout.Space();
- _scrollView = EditorGUILayout.BeginScrollView(_scrollView);
- IEnumerable<T> query = from item in _items
- let itemName = _nameSelector(item)
- where Contains(itemName)
- where GUILayout.Button(itemName)
- select item;
- foreach(T item in query)
- {
- _onSelectCallback?.Invoke(item);
- editorWindow.Close();
- }
- EditorGUILayout.EndScrollView();
- }
- private bool Contains(string itemName) => itemName.ToLower().Contains(_searchFilter.ToLower());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement