I found myself trying to write code for navigating in hierarchy like manner in Unitys Inspector (RuleSet -> Entity -> Component -> More To Come) today. First thought I had: Just put a reference field into every single scriptable object and your done ... well no.

using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public static class SelectionTracker
{
    private static Object active;
    private static Object lastActive;

    public static Object Active
    {
        get
        {
            return active;
        }
        set
        {
            if (active == value)
                return;
            LastActive = Active;
            active = value;
        }
    }

    public static Object LastActive
    {
        get
        {
            return lastActive;
        }
        set
        {
            if (lastActive == value || value == null)
                return;
            lastActive = value;
        }
    }

    static SelectionTracker()
    {
        EditorApplication.update += () => Active = Selection.activeObject;
    }
}

Previous Post Next Post