- Home /
The question is answered, right answer was accepted
Editor GUI Drag Selection Box
I'm working on a script where you would drag a selection box around textures in an editor window.
The concept I'm working with is to check for a mouse drag event, record the mouse position on start drag, and draw a texture with one corner at the start drag coordinates and the other corner at the current mouse position.
It seems that OnGUI doesn't update enough to draw a draggable selection box like this. What do you think is the most efficient way to tackle this problem?
Edit: Here is the code I'm using
private bool isDragging = false;
Vector2 mouseDragStart;
...
void OnGUI() {
if(Event.current.type == EventType.mouseDrag) {
if(!isDragging) {
mouseDragStart = Event.current.mousePosition;
isDragging = true;
}
}
if(Event.current.type == EventType.mouseUp && isDragging) isDragging = false;
GUI.Box (new Rect(mouseDragStart.x,mouseDragStart.y,100f,100f),"test"); // testing, box moving with mouse drag
}
OnGUI is updated every frame, what seems to be the effect you are getting? Are you using the event for the mouse buttons, positions etc rather than Input?
The box's position only updates every few seconds. I updated the question with some code.
Answer by whydoidoit · May 27, 2012 at 01:39 AM
It looks to me like you aren't updating the mouse position you draw at unless isDragging = false! I don't think that's what you meant.
Presumably you want to draw the box from mouseDragStart to the current mouse position.
That would have your last line as:
if(Event.current.type==EventType.mouseDrag) {
currentMousePosition = Event.current.mousePosition;
}
GUI.Box(new Rect(mouseDragStart.x, mouseDragStart.y, currentMousePosition.x, currentMousePosition.y), "test");
And add a class level variable called currentMousePosition : Vector2
EDIT
The problem was more complicated than this first solution suggests. When writing an EditorWindow that reacts to mouse events you should make sure that it has its wantsMouseMove = true and then you should consume the events that you use, which makes Unity send them to you more frequently. To use an event you do something like this:
var e = Event.current;
if(e.isMouse)
{
e.Use();
}
I can't seem to get this to work right. It just doesn't refresh fast enough!
This is super smooth for me:
pragma strict
var dragging : boolean = false;
var startPos : Vector2;
var currentPos : Vector2;
function OnGUI ()
{
if (Event.current.type == EventType.mouseDrag) {
currentPos = Event.current.mousePosition;
if (!dragging) {
dragging = true;
startPos = currentPos;
}
}
if(Event.current.type == EventType.mouseUp) {
dragging = false;
}
if(dragging)
GUI.Box (Rect (startPos.x, startPos.y, currentPos.x - startPos.x, currentPos.y - startPos.y), "test");
}
bool dragging = false;
Vector2 startPos;
Vector2 currentPos;
void OnGUI ()
{
if (Event.current.type == EventType.mouseDrag) {
currentPos = Event.current.mousePosition;
if (!dragging) {
dragging = true;
startPos = currentPos;
}
}
if (Event.current.type == EventType.mouseUp) {
dragging = false;
}
if (dragging)
GUI.Box (new Rect (startPos.x, startPos.y, currentPos.x - startPos.x, currentPos.y - startPos.y), "test");
}
Ok, first an apology - I didn't try it as an EditorWIndow last night - dumb, just missed that as the very first word of the title. Ugh, sorry.
Ok so you need to set your editor window to .wants$$anonymous$$ouse$$anonymous$$ove = true and then you also need to use the event (then you get them more quickly again!)
Add this line to your OnGUI:
if(Event.current.is$$anonymous$$ouse) Event.current.Use();
Follow this Question
Related Questions
Drag GameObject onto EditorWindow 0 Answers
Using PositionHandle inside an EditorWindow 2 Answers
How to get an array inside a simple class at Editor window and change arraysize? 0 Answers
Handling NullReferenceException in EditorWindow OnGUI() 0 Answers
How to resize preference window to fit gui contents. 1 Answer