Search Unity

Can't edit values in inspector (scriptable object) when drawing in EditorWindow

Discussion in 'Immediate Mode GUI (IMGUI)' started by eses, Sep 4, 2019.

  1. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    [Solved... see answer below]

    TL;DR

    Each update I have to first draw nodes then edges between nodes. If I draw the edges, it prevents editing values in inspector...


    I can't figure this out, naturally I'm completely lacking understanding how editor stuff should be done and how editor updates...

    I get the node and edge data from Lists in scriptable object asset (list for Nodes, list for edges). Node is just a C# class with name, position and in / out connections fields.

    I can't for example edit node's name field I've defined when line drawing code is active. If I disable the line drawing, each value in node list is editable again.

    Here are snippets from my code.

    This one draws the edges and is in main editor window file:

    Code (CSharp):
    1. void DrawEdges()
    2. {
    3.     foreach (var node in database.nodes)
    4.     {
    5.         if (node.outConnections == null)
    6.             continue;
    7.        
    8.         foreach (var id in node.outConnections)
    9.         {
    10.             var source = node;
    11.             var target = database.GetNodeByID(id);
    12.             Edge.Draw(source.box, target.box);
    13.         }            
    14.     }
    15. }
    16.  

    If I disable this piece of code, I can again edit Scriptable object fields...

    Code (CSharp):
    1. // In Edge class, a static method
    2. // Takes Rects to draw a line from bottom edge of r1
    3. // to top edge of r2
    4. public static void Draw(Rect r1, Rect r2)
    5. {
    6.     var b1 = r1;
    7.     var b2 = r2;
    8.     Vector2 p1 = new Vector2(b1.width / 2 + b1.x, b1.y + b1.height);
    9.     Vector2 p2 = new Vector2(b2.width / 2 + b2.x, b2.y - 5);
    10.     Handles.DrawLine(p1, p2);
    11. }

    Any ideas?

    I guess I could take a look at other implementations, but then again I wouldn't understand what is going on here...

    Thanks.
     
    Last edited: Sep 4, 2019
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Well I kinda solved this myself;

    For some reason, having public references/fields to Nodes in edge class caused some sort of cyclic loop, although I didn't get that serialization depth limit warning... neither did I use these fields for anything.

    If I kept public references to nodes, even if I marked them HideInInspector, these prevent editing nodes in Scriptable Object asset file's node list. Anyway, I changed the code so that edges don't rely on keeping references to node instances from node list.

    Would be interesting to hear what actually is the issue here...?

    Code (CSharp):
    1. // in edge class
    2. public string fromId;
    3. public string toId;
    4. public Node fromNode; // problem, if public
    5. public Node toNode; // problem, if public
     
    Last edited: Sep 4, 2019