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. Dismiss Notice

Draw Selection Rectangle in SceneView in OnSceneGUI()

Discussion in 'Scripting' started by sebj, Feb 28, 2018.

  1. sebj

    sebj

    Joined:
    Dec 4, 2013
    Posts:
    70
    Hi,

    How do I draw a rectangle over the scene in screen coordinates in OnSceneGUI()?

    I am creating an Editor Component to allow the selection of particles with the mouse.

    Neither Handles, GUILayout, Gizmos or Debug draws anything to the screen. I have tried HandlesUtility.Repaint(). Even drawing a fixed line similar to the example in the documentation does not work. Perhaps it is something to do with Event::Use?

    Related (unanswered) question: https://answers.unity.com/questions/619226/draw-rect-in-scene-view-from-editorwindow.html

    public void OnSceneGUI()
    {
    if (Event.current.isMouse)
    {
    int controlId = GUIUtility.GetControlID(FocusType.Passive);
    var mouseposition = new Vector2(Event.current.mousePosition.x, Screen.height - Event.current.mousePosition.y);
    var rect = new Rect(Vector2.Min(rect_start, mouseposition), Vector2.Max(rect_start, mouseposition) - Vector2.Min(rect_start, mouseposition));

    if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
    {
    rect_start = mouseposition;
    rect_drawing = true;
    }
    if (Event.current.type == EventType.MouseUp && Event.current.button == 0)
    {
    if (rect_drawing)
    {
    rect_drawing = false;
    ClothEngineDiagnostics component = (ClothEngineDiagnostics)target;
    component.Highlight(SceneView.currentDrawingSceneView.camera.WorldToScreenPoint, rect);
    }
    }
    if(Event.current.type == EventType.MouseDrag && rect_drawing)
    {
    //
    // What goes here?
    //
    }
    if (rect_drawing)
    {
    GUIUtility.hotControl = controlId;
    Event.current.Use();
    }
    }
    }
     
    sean244 likes this.
  2. KimberleyC

    KimberleyC

    Joined:
    Mar 22, 2021
    Posts:
    20