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 /
avatar image
0
Question by yveris · Nov 18, 2013 at 03:54 AM · c#editoreditorguidrawcallsrect

[Answered]How to instantly draw rect in editor GUI for 2D tilemapper

I have written a custom editor script that displays a sprite map texture on the inspector and I am attempting to have this tool allow the user to click and drag over portions of the sprite map he wants to use for an object to speed up the level creation for my 2D game.

As such I have the editorGUI draw a rect over the texture. My problem is that I want it to draw during a mouse drag event yet it will only draw the rectangle 2-3 seconds after I let go of the button.

I created a portion of the script that drew a rectangle of increasing size that reset itself and even that had large latency problems. But I started quickly clicking on the editor in a gray area and it started drawing the aforementioned growing rectangle every time I clicked. I am unsure how to interpret this behavior.

Here is how my script currently gets the coordinates:

         private void DrawCropAreaGUI() {
             //Cache the event
             Event evt = Event.current;
 
             switch(evt.type){
                 case EventType.MouseDown:
                 case EventType.MouseDrag:
                 case EventType.MouseUp:
                     //If the click is outside of the sprite sheet
                     if(!spriteSheetRect.Contains( evt.mousePosition ))
                         break;
 
                     //We're working with the initial click
                     if(evt.type == EventType.MouseDown) {
                         mouseDown = evt.mousePosition;
                     }
 
                     //We haven't finished yet we're just drawing the outline
                     if(evt.type == EventType.MouseDrag || evt.type == EventType.MouseUp) {
                         int left, top, right, bottom; //The extents of our rectangle of selection
                         //The drag is moving to the left
                         if(mouseDown.x > evt.mousePosition.x){
                             left = (int)evt.mousePosition.x;
                             right = (int)mouseDown.x;
                         }
                         //The drag is moving to the right
                         else{
                             left = (int)mouseDown.x;
                             right = (int)evt.mousePosition.x;
                         }
                         //The drag is moving up
                         if(mouseDown.y > evt.mousePosition.y){
                             top = (int)evt.mousePosition.y;
                             bottom = (int)mouseDown.y;
                         }
                         //The drag is going down
                         else{
                             top = (int)mouseDown.y;
                             bottom = (int)evt.mousePosition.y;
                         } 
 
                         selectionRect = new Rect( left, top , right - left, bottom - top );
                     }
                     break;
 
             }
         }


Then this is the OnInspectorGUI function:

         public override void OnInspectorGUI() {
             //Grab the attributes from the object
             m_Object.Update();
             
             //Overall settings label
             GUILayout.Label("Selection Settings", EditorStyles.boldLabel);
             
             //Set up the fields
             EditorGUILayout.PropertyField( t_width );
             EditorGUILayout.PropertyField( t_height );
 
             //If the width value is less than 0
             if(t_width.intValue < 0)
                 //We can't select less than 0 pixels
                 t_width.intValue = 0;
             //If the height value is less than 0
             if(t_height.intValue < 0)
                 //We can't select less than 0 pixels
                 t_height.intValue = 0;
 
             //The sprite map label
             GUILayout.Label( "Sprite Map", EditorStyles.boldLabel );
             Color c = new Color(200, 200, 0, 0.5f);
            
             if(t_texture.objectReferenceValue != null) {
                 spriteSheetRect = GUILayoutUtility.GetRect( 0.0f, 100.0f, GUILayout.ExpandWidth( true ), GUILayout.ExpandHeight(true) );
                 DrawCropAreaGUI();
                 //Draw our texturefor the user to eselect from
                 EditorGUI.DrawTextureTransparent(spriteSheetRect , (Texture2D) t_texture.objectReferenceValue );
                 Debug.Log( "spriteshseet " + spriteSheetRect + " selection " + selectionRect );     
                 EditorGUI.DrawRect( selectionRect, c );
                  //ShadowAndOutline.DrawOutline( selectionRect, "", new GUIStyle(), Color.red, Color.gray, 10 );
             }
             DropAreaGUI();
             //Apply the attributes to the object once done 
             m_Object.ApplyModifiedProperties();
            
         }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by yveris · Nov 18, 2013 at 04:10 AM

I figured it out.

Apparently, when creating event handler functions the programmer is required to end the function with Event.current.Use(). So as to notify unity the current event has been used and change the event.type. This will cause other gui elements to ignore this event. If you don't do this manually then I assume Unity has some sort of clock that handles the functions that does this but you won't get the response time you're looking for.Reference for event.use and the Unity GUI event class

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

EditorApplication.projectWindowItemOnGUI and sub assets 1 Answer

Any way to attach a bit of code to individual UI Windows [Editor] 1 Answer

Multiple Cars not working 1 Answer

Move Editor Button 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