Version 2.1.0.0

OdinSelector<T> class

Namespace: Sirenix.OdinInspector.Editor
Assembly: Sirenix.OdinInspector.Editor
public abstract class OdinSelector<T>
OdinSelectors is an abstract base class that combines OdinMenuTrees and OdinEditorWindows to help making feature-rich selectors and popup selectors.
Inheritance
  • System.Object
  • OdinSelector<T>
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;

Type Parameters

T

Constructors

OdinSelector()
protected OdinSelector()

Fields

DrawConfirmSelectionButton
If true, a confirm selection button will be drawn in the title-bar.
public bool DrawConfirmSelectionButton

Properties

SelectionTree
Gets the selection menu tree.
public OdinMenuTree SelectionTree { get; }
Title
Gets the title. No title will be drawn if the string is null or empty.
public virtual string Title { get; }

Methods

BuildSelectionTree(OdinMenuTree)
Builds the selection tree.
protected abstract void BuildSelectionTree(OdinMenuTree tree)
Parameters
OdinMenuTree tree

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.
protected virtual float DefaultWindowWidth()
Returns
System.Single

DrawSelectionTree()
Draws the selection tree. This gets drawn using the OnInspectorGUI attribute.
[OnInspectorGUI]
[PropertyOrder(-1)]
protected virtual void DrawSelectionTree()
DrawSelectorDropdown(GUIContent, String, Func<Rect, OdinSelector<T>>, GUIStyle, GUILayoutOption[])
Draws dropwdown field, that creates and binds the selector to the dropdown field.
public static IEnumerable<T> DrawSelectorDropdown(GUIContent label, string btnLabel, Func<Rect, OdinSelector<T>> createSelector, GUIStyle style = null, params GUILayoutOption[] options)
Parameters
UnityEngine.GUIContent label

System.String btnLabel

System.Func<UnityEngine.Rect, OdinSelector<T>> createSelector

UnityEngine.GUIStyle style

UnityEngine.GUILayoutOption[] options

Returns
System.Collections.Generic.IEnumerable<T>

DrawSelectorDropdown(GUIContent, GUIContent, Func<Rect, OdinSelector<T>>, GUIStyle, GUILayoutOption[])
Draws dropwdown field, that creates and binds the selector to the dropdown field.
public static IEnumerable<T> DrawSelectorDropdown(GUIContent label, GUIContent btnLabel, Func<Rect, OdinSelector<T>> createSelector, GUIStyle style = null, params GUILayoutOption[] options)
Parameters
UnityEngine.GUIContent label

UnityEngine.GUIContent btnLabel

System.Func<UnityEngine.Rect, OdinSelector<T>> createSelector

UnityEngine.GUIStyle style

UnityEngine.GUILayoutOption[] options

Returns
System.Collections.Generic.IEnumerable<T>

DrawSelectorDropdown(Rect, String, Func<Rect, OdinSelector<T>>, GUIStyle)
Draws dropwdown field, that creates and binds the selector to the dropdown field.
public static IEnumerable<T> DrawSelectorDropdown(Rect rect, string btnLabel, Func<Rect, OdinSelector<T>> createSelector, GUIStyle style = null)
Parameters
UnityEngine.Rect rect

System.String btnLabel

System.Func<UnityEngine.Rect, OdinSelector<T>> createSelector

UnityEngine.GUIStyle style

Returns
System.Collections.Generic.IEnumerable<T>

DrawSelectorDropdown(Rect, GUIContent, Func<Rect, OdinSelector<T>>, GUIStyle)
Draws dropwdown field, that creates and binds the selector to the dropdown field.
public static IEnumerable<T> DrawSelectorDropdown(Rect rect, GUIContent btnLabel, Func<Rect, OdinSelector<T>> createSelector, GUIStyle style = null)
Parameters
UnityEngine.Rect rect

UnityEngine.GUIContent btnLabel

System.Func<UnityEngine.Rect, OdinSelector<T>> createSelector

UnityEngine.GUIStyle style

Returns
System.Collections.Generic.IEnumerable<T>

EnableSingleClickToSelect()
Enables the single click to select.
public void EnableSingleClickToSelect()
GetCurrentSelection()
Gets the current selection from the menu tree whether it's valid or not.
public virtual IEnumerable<T> GetCurrentSelection()
Returns
System.Collections.Generic.IEnumerable<T>

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.
public virtual bool IsValidSelection(IEnumerable<T> collection)
Parameters
System.Collections.Generic.IEnumerable<T> collection

Returns
System.Boolean

OnInspectorGUI()
Draw the selecotr manually.
public void OnInspectorGUI()
SetSelection(T)
Sets the selection.
public virtual void SetSelection(T selected)
Parameters
T selected

SetSelection(IEnumerable<T>)
Sets the selection.
public virtual void SetSelection(IEnumerable<T> selection)
Parameters
System.Collections.Generic.IEnumerable<T> selection

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.
public OdinEditorWindow ShowInPopup()
ShowInPopup(Single)
The mouse position is used as the position for the window. Opens up the selector instance in a popup at the specified position.
public OdinEditorWindow ShowInPopup(float windowWidth)
Parameters
System.Single windowWidth

ShowInPopup(Single, Single)
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.
public OdinEditorWindow ShowInPopup(float width, float height)
Parameters
System.Single width

System.Single height

ShowInPopup(Rect)
Opens up the selector instance in a popup at the specified rect position.
public OdinEditorWindow ShowInPopup(Rect btnRect)
Parameters
UnityEngine.Rect btnRect

ShowInPopup(Rect, Single)
Opens up the selector instance in a popup at the specified rect position.
public OdinEditorWindow ShowInPopup(Rect btnRect, float windowWidth)
Parameters
UnityEngine.Rect btnRect

System.Single windowWidth

ShowInPopup(Rect, Vector2)
Opens up the selector instance in a popup at the specified rect position.
public OdinEditorWindow ShowInPopup(Rect btnRect, Vector2 windowSize)
Parameters
UnityEngine.Rect btnRect

UnityEngine.Vector2 windowSize

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.
public OdinEditorWindow ShowInPopup(Vector2 position)
Parameters
UnityEngine.Vector2 position

ShowInPopup(Vector2, Single)
Opens up the selector instance in a popup at the specified position.
public OdinEditorWindow ShowInPopup(Vector2 position, float windowWidth)
Parameters
UnityEngine.Vector2 position

System.Single windowWidth

TriggerSelectionChanged()
Triggers the selection changed event, but only if the current selection is valid.
protected void TriggerSelectionChanged()

Events

SelectionCancelled
Occurs when the window is closed, and no slection was confirmed.
public event Action SelectionCancelled
SelectionChanged
Occurs when the menuTrees selection is changed and IsValidSelection returns true.
public event Action<IEnumerable<T>> SelectionChanged
SelectionConfirmed
Occurs when the menuTrees selection is confirmed and IsValidSelection returns true.
public event Action<IEnumerable<T>> SelectionConfirmed