Siccity / xNode

Unity Node Editor: Lets you view and edit node graphs inside Unity
MIT License
3.3k stars 582 forks source link

How to tint the active/running node #349

Closed maxhacker11 closed 2 years ago

maxhacker11 commented 2 years ago

So I would like to visually see which node in the graph is currently being executed.

I managed to achieve this but it only works when I am interacting with the graph while in play mode (for example if I drag select)

I think it's because this forces the UI for the graph to redraw so it updates the node tint. As soon as I stop interacting with it, the nodes return to their default tints.

Hope I explained it well, here is my code to help explain it further.

I have created a "BaseStep" which has the following code:

public abstract class BaseStep : Node 
{
    public enum State
    {
        None,
        Running,
        Failure,
        Success
    }

    [HideInInspector] public State state = State.None;

    [Input] public int entry;
    [Output] public int exit;

    public abstract void OnStart();
    public abstract State OnUpdate();
    public abstract void OnStop();
}

Then I also have the custom editor for the node which just checks if the node is running and updates the tint:

[CustomNodeEditor(typeof(BaseStep))]
public class BaseStepEditor : NodeEditor
{
    private BaseStep baseStep;

    public override Color GetTint()
    {
        if (baseStep == null) baseStep = target as BaseStep;
        return baseStep.state == BaseStep.State.Running ? Color.red : base.GetTint();
    }
}

Thanks a lot in advance! Regards, Max

maxhacker11 commented 2 years ago

Update: In the NodeEditorGUI.cs the nodes are drawn on "OnGUI" so that is why I am only able to update the tints while drag selecting and interacting with the graph. But I am still not sure how I can make them update while I am in play mode as well?

Siccity commented 2 years ago

Call NodeEditorWindow.current.Repaint(); each frame

public override void OnGUI() {
    // Keep repainting the GUI of the active NodeEditorWindow
    NodeEditorWindow.current.Repaint();
}
maxhacker11 commented 2 years ago

Wow, that solved it, thanks so much for the speedy reply and for creating such an amazing tool!