Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[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:
    229
    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:
    229
    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:
    229
    Above solution works perfectly. Writing the question made me realise how easy the solution was. ^^