Static problem with opening custom editor window

Hi… Im creating a Node editor system and im trying to accomplish a shader graph like system where you create a scriptable object then double click on it to open editor. Everything is working so far except for I need to pass graph data from the scriptable object to editor window. But opening the editor window is static method and so I can’t pass in (this) or (private Graph graph) it needs to be (static Graph graph) which won’t work cause this variable needs to be specefic to the scriptable object. Heres the script

[CreateAssetMenu(fileName = "New Graph", menuName = "Graph", order = 52)]
public class Graph : ScriptableObject
{
    public static Graph instance;

    public List<Node> nodes = new List<Node>();

    private void OnEnable()
    {
        instance = this;
        Debug.Log(instance.GetInstanceID());
    }

    [OnOpenAsset]
    public static bool OpenEditor(int instanceID, int line)
    {
        if (instanceID == instance.GetInstanceID())
        {
            WindowEditor.ShowWindow(instance);
        }
        return false;
    }
}

Anyone have any ideas for somehow passing through specific Graph data? Thanks

Just forget about your whole pseudo singleton approach. The instanceID you get in the OpenEditor method is the actual instance you’re interested in. You just use EditorUtility.InstanceIDToObject to actually get a reference to the object the instanceID belongs to.

Be aware that when you implement an Open Asset handler it will be called for “every” asset that is opened. The callback should return true when you actually want to handle the opening for that asset, otherwise you return false so other handlers have the chance of handling the open event. If none of the custom handlers returned true, Unity would do it’s “normal thing” and open the asset with whatever is the default.

So your handler has to determine if you want to handle a certain asset or not based on the passed instanceID. Probably something like this:

[OnOpenAsset]
public static bool OpenEditor(int instanceID, int line)
{
    Graph obj = EditorUtility.InstanceIDToObject( instanceID ) as Graph;
    if (obj != null)
    {
        WindowEditor.ShowWindow(obj);
        return true;
    }
    return false;
}

Here we take advantage of the as cast to result in a value of null if the object can not be casted into a “Graph”. So it doesn’t matter if the object might be null in the first place or if it’s just a completely different object. Whenever the object does not represent a Graph instance we just return false. However if the object is a Graph instance we can directly use that instance reference however you like.

I would recommend to add an ObjectField to your editor window where you display the instance this editor window is currently editing. It allows the user to quickly find that asset in the project and you could also use it to switch the graph you want to edit. Though if the double click works there’s probably no reason for that.

Note that there are different approaches for how to open a custom editor window. When you use GetWindow you always only get one window instance. This is often the wanted behaviour. However in some cases it might be useful to allow multiple windows to be opened to edit different graph objects. In that case you can create your editor window manually by using “CreateInstance” and call “Show” on that instance. Of course if you want to support multiple windows, opening the same asset twice should not open two windows. So in this case a bit more work is necessary. However that wasn’t the question here so I’ll stop here ^^.