Making a custom selection box with DrawRect
I call the following during OnSceneGui in my custom editor to draw a custom selection box. Everything works as it should, as far as the debugger tells me - the proportions and position of the rectangle are correct, and are adjusted as you'd expect when the mouse moves, but the rectangle will not draw. What am I getting wrong?
Rect boundingRect = new Rect(0,0,0,0);
void CheckForBoundingBox()
{
Event e = Event.current;
HandleUtility.AddDefaultControl( GUIUtility.GetControlID( FocusType.Passive ) );
if( e.button == 0 )
{
if( boundingRect.min != Vector2.zero )
{
boundingRect.size = e.mousePosition - boundingRect.min;
EditorGUI.DrawRect( boundingRect, Color.red );
Debug.Log( "Dragging continues "+boundingRect );
}
if( e.type == EventType.MouseUp )
{
e.Use();
Debug.Log( "Dragging stopped" );
boundingRect.min = Vector2.zero;
}
else if( e.type == EventType.MouseDown )
{
e.Use();
boundingRect.min = e.mousePosition;
boundingRect.size = Vector2.zero;
Debug.Log( "Dragging started" );
}
}
}
Answer by unity_3KAIcHNkHzTZvw · Aug 21, 2021 at 05:51 PM
This is a solution to my problem, not an answer to my question, but instead of EditorGUI.DrawRect I used Handles. There might be something wrong with this solution, but it works. Here's that part as it stands now:
if( boundingRect.min != Vector2.zero && e.type == EventType.Repaint )
{
boundingRect.size = e.mousePosition - boundingRect.min;
Ray ray = HandleUtility.GUIPointToWorldRay( boundingRect.min );
Vector3 posA = ray.origin + ray.direction;
ray = HandleUtility.GUIPointToWorldRay( boundingRect.min + (boundingRect.width*Vector2.right) );
Vector3 posB = ray.origin + ray.direction;
ray = HandleUtility.GUIPointToWorldRay( boundingRect.max );
Vector3 posC = ray.origin + ray.direction;
ray = HandleUtility.GUIPointToWorldRay( boundingRect.min + (boundingRect.height*Vector2.up) );
Vector3 posD = ray.origin + ray.direction;
Handles.DrawDottedLine( posA, posB, 5 );
Handles.DrawDottedLine( posB, posC, 5 );
Handles.DrawDottedLine( posC, posD, 5 );
Handles.DrawDottedLine( posD, posA, 5 );
Debug.Log( "Dragging continues "+boundingRect );
}
Your answer
Follow this Question
Related Questions
Do Unity Editor GUI Utilities (Handles.DrawLine & EditorGUI.DrawRect) have limitations? 2 Answers
EditorGUILayout.EnumPopup shows always the same selected option but works as programmed 1 Answer
Editor Int sliders affecting each other 0 Answers
i want to make notification when push build button 0 Answers