Search Unity

[Solved] Custom editor: Graph with edges

Discussion in 'Editor & General Support' started by jc-drile77, Feb 3, 2020.

  1. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    Hi,
    The title is self-explanatory, I´m trying to create a custom editor for a graph in the scene view as in the following image:
    upload_2020-2-3_22-58-30.png



    The logic behind the directed graph is quite intuitive for me But when it comes to the implementation of the custom editor, I am lost.
    The idea is to be able to spawn nodes (this is already done) , right click on them to create an edge and then move the mouse and right click on the destination of that edge to set it.

    upload_2020-2-3_23-3-49.png
    With the following code I am drawing the nodes:
    Code (CSharp):
    1.  
    2.     [DrawGizmo(GizmoType.Selected | GizmoType.NotInSelectionHierarchy)]
    3.     static void DrawGizmos(Path path, GizmoType gizmoType)
    4.     {
    5.         foreach (Node node in path.Nodes)
    6.         {
    7.             Gizmos.color = Color.red;
    8.             Gizmos.DrawSphere(node.transform.position, 1);
    9.            
    10.             Gizmos.color = Color.blue;
    11.             foreach (Edge edge in node.Edges)
    12.                 Gizmos.DrawLine(node.transform.position, edge.to.transform.position);
    13.         }
    14.     }
    The nodes are empty GameObjects + Node component, and therefore they are not clickable or raycastable.
    If I draw the nodes using OnDrawGizmos() in their class, they can be selected on the scene view but I do not know how to get this event, whereas with the upper code they cannot be selected. How can I raycast/click/select a node with code?

    Any input is welcome :)
     
  2. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    I guess I could get the closest node to my right click as long as it is within x distance. Or is there another way?
     
  3. jc-drile77

    jc-drile77

    Joined:
    Jul 1, 2014
    Posts:
    230
    Above solution works perfectly. Writing the question made me realise how easy the solution was. ^^