Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 05, 2017 at 11:57 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by absameen · May 27, 2012 at 01:24 AM · drageditorwindowongui

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
 }
Comment
Comments Locked · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · May 27, 2012 at 01:26 AM 0
Share

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?

avatar image absameen · May 27, 2012 at 01:33 AM 0
Share

The box's position only updates every few seconds. I updated the question with some code.

1 Reply

  • Sort: 
avatar image
3
Best Answer

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();
 }
Comment
Comments Locked · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image absameen · May 27, 2012 at 02:10 AM 0
Share

I can't seem to get this to work right. It just doesn't refresh fast enough!

avatar image whydoidoit · May 27, 2012 at 02:30 AM 0
Share

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");
 }
avatar image whydoidoit · May 27, 2012 at 02:32 AM 0
Share

I'm going mad aren't I - you wanted C# :(

avatar image whydoidoit · May 27, 2012 at 02:36 AM 1
Share
 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");
 }
avatar image whydoidoit · May 27, 2012 at 10:58 AM 2
Share

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();
avatar image YarivAtias whydoidoit · Jun 05, 2017 at 10:34 PM 0
Share

That helps very well. thanks

Show more comments

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges