OdinSelector<T>

OdinSelectors is an abstract base class that combines OdinMenuTrees and OdinEditorWindows to help making feature-rich selectors and popup selectors.

namespace Sirenix.OdinInspector.Editor
{
public abstract class OdinSelector<T>
}

Requirements

Namespace: Sirenix.OdinInspector.Editor
Assembly: Sirenix.OdinInspector
Version: 1.0.6.1

See Also

Properties

Return Type Definition Description
OdinMenuTree SelectionTree Gets the selection menu tree.
string Title Gets the title. No title will be drawn if the string is null or empty.

Methods

Return Type Definition Description
float DefaultWindowWidth() When ShowInPopup is called, without a specifed window width, this methods gets called. Here you can calculate and give a good default width for the popup. The default implementation returns 0, which will let the popup window determain the width itself. This is usually a fixed value.
void DrawSelectionTree() Draws the selection tree. This gets drawn using the OnInspectorGUI attribute.
IEnumerable<T> GetCurrentSelection() Gets the current selection from the menu tree whether it's valid or not.
bool IsValidSelection(IEnumerable<T>) Determines whether the specified collection is a valid collection. If false, the SlectionChanged and SelectionConfirm events will not be called. By default, this returns true if the collection contains one or more items.
void SetSelection(IEnumerable<T>) Sets the selection.
void SetSelection(T) Sets the selection.
OdinEditorWindow ShowInPopup() Opens up the selector instance in a popup at the specified rect position. The width of the popup is determined by DefaultWindowWidth, and the height is automatically calculated.
OdinEditorWindow ShowInPopup(float) The mouse position is used as the position for the window. Opens up the selector instance in a popup at the specified position.
OdinEditorWindow ShowInPopup(float, float) Opens up the selector instance in a popup with the specified width and height. The mouse position is used as the position for the window.
OdinEditorWindow ShowInPopup(Rect, float) Opens up the selector instance in a popup at the specified rect position.
OdinEditorWindow ShowInPopup(Rect, Vector2) Opens up the selector instance in a popup at the specified rect position.
OdinEditorWindow ShowInPopup(Vector2) Opens up the selector instance in a popup at the specified position. The width of the popup is determined by DefaultWindowWidth, and the height is automatically calculated.
OdinEditorWindow ShowInPopup(Vector2, float) Opens up the selector instance in a popup at the specified position.
void TriggerSelectionChanged() Triggers the selection changed event, but only if the current selection is valid.

Example

public class MySelector : OdinSelector<SomeType>
{
    private readonly List<SomeType> source;
    private readonly bool supportsMultiSelect;

    public MySelector(List<SomeType> source, bool supportsMultiSelect)
    {
        this.source = source;
        this.supportsMultiSelect = supportsMultiSelect;
    }

    protected override void BuildSelectionTree(OdinMenuTree tree)
    {
        tree.Config.DrawSearchToolbar = true;
        tree.Selection.SupportsMultiSelect = this.supportsMultiSelect;
        
        tree.Add("Defaults/None", null);
        tree.Add("Defaults/A", new SomeType());
        tree.Add("Defaults/B", new SomeType());

        tree.AddRange(this.source, x => x.Path, x => x.SomeTexture);
    }

    [OnInspectorGUI]
    private void DrawInfoAboutSelectedItem()
    {
        SomeType selected = this.GetCurrentSelection().FirstOrDefault();

        if (selected != null)
        {
            GUILayout.Label("Name: " + selected.Name);
            GUILayout.Label("Data: " + selected.Data);
        }
    }
}
Usage:
void OnGUI()
{
    if (GUILayout.Button("Open My Selector"))
    {
        List<SomeType> source = this.GetListOfThingsToSelectFrom();
        MySelector selector = new MySelector(source, false);

        selector.SetSelection(this.someValue);

        selector.SelectionCancelled += () => { };  // Occurs when the popup window is closed, and no slection was confirmed.
        selector.SelectionChanged += col => { };
        selector.SelectionConfirmed += col => this.someValue = col.FirstOrDefault();

        selector.ShowInPopup(); // Returns the Odin Editor Window instance, in case you want to mess around with that as well.
    }
}

// All Odin Selectors can be rendered anywhere with Odin.
[ShowInInspector]
MySelector inlineSelector;