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
2
Question by PlutDem · Jan 05, 2015 at 11:49 AM · c#editoreventhandles

Draw a handle in editor when key is pressed

Hello, how can i draw a handle in editor when certain key is pressed? I'am currently using this code in OnSceneGUI, but it's not working.

 if (Event.current.isKey && Event.current.keyCode == KeyCode.H)
 {
     Handles.DotCap(0, new Vector3(), Quaternion.identity, 1);
 }

Although, this is working fine

 if (Event.current.shift)
 {
     Handles.DotCap(0, new Vector3(), Quaternion.identity, 1);
 }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ocimum · Oct 21, 2015 at 09:37 AM

Before drawing anything, you have to start the Handles context so that your Handles methods can be executed.

 Handles.BeginGUI();
 // Do your drawing stuff
 Handles.EndGUI();

If you will have any problems to get access to your mouse events while the Editor script is enabled try to get the clicks like this:

 void OnSceneGUI (){

     Event e = Event.current;
     int controlID = GUIUtility.GetControlID (FocusType.Passive);

     switch (e.GetTypeForControl (controlID)) {

     case EventType.MouseDown:
         GUIUtility.hotControl = controlID;
     
         CheckForPositions(e.mousePosition);
         e.Use ();

         break;

     case EventType.MouseUp:
         GUIUtility.hotControl = 0;
         e.Use();
         break;

     case EventType.MouseDrag:
         GUIUtility.hotControl = controlID;
         CheckForPositions(e.mousePosition);
 
         e.Use ();
         break;

     case EventType.KeyDown:
         if( e.keyCode == KeyCode.Escape ){
             // Do something on pressing Escape
         }

         if( e.keyCode == KeyCode.Space ){
             // Do something on pressing Spcae
         }

         if( e.keyCode == KeyCode.S ){
             // Do something on pressing S
         }

         break;
     }

     AllDrawingStuff();
 }

And here the drawing

 void AllDrawingStuff(){
     Handles.BeginGUI();

     DrawCircleOnMousePositions();
     DrawLine();
 
     Handles.EndGUI();
 }

Here is a small tutorial which helped me to understand all the stuff!

Comment
Add comment · Show 2 · 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 ifdef_ben · Sep 19, 2016 at 03:38 PM 0
Share

Based on Unity's doc, BeginGUI creates a 2D block in a 3D environment. It's used to draw 2D UIs like buttons and labels.. I have the same issue and putting things in BeginGUI block didn't solved this... this might be a bug..

avatar image Bunny83 ifdef_ben · Sep 19, 2016 at 04:20 PM 1
Share

No, it's most likely not a bug, it's most the time not a bug.

You can't draw something during a mouse or key event. You can only draw things during the repaint event. Therefore you have to place all your drawing stuff somewhere, where it's always executed for every event. The drawing methods will automatically only react to the repaint event. If you call a drawing method only during a keydown event it won't do anything.

If you want to enable / disable the drawing of the handle you have to use some sort of flag (boolean, enum, int) which you check before you draw it.

Also keep in $$anonymous$$d that the editor is completely event driven unlike the runtime. If you want the scene to be redrawn you have to request that redraw with SceneView.RepaintAll();.

avatar image
0

Answer by ifdef_ben · Sep 19, 2016 at 04:58 PM

Thanks to Bunny83's comment I was able to figure out the proper way to do this. You have to set a flag outside OnSceneGUI() and update the flag with the KeyDown & KeyUp event. This would do something like this.

 [CustomEditor(typeof(Test))]
 public class TestInspector : Editor {
    private bool bDraw;
 
    void OnSceneGUI (){
         if (Event.current.type == EventType.keyDown && Event.current.keyCode == KeyCode.H)
             bDraw= true;
         else if (Event.current.type == EventType.keyUp && Event.current.keyCode == KeyCode.H)
             bDraw= false;

         if (bDraw)
             Handles.CubeCap(0, Vector3.zero, Quaternion.identity, 1);
    }
 }
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

6 People are following this question.

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

Related Questions

Left Click Up Event in Editor 4 Answers

Detecting moment when GameObiect is created in hierarchy. 1 Answer

How can an editor script know when another script was removed from the project? 1 Answer

Painting regions of a mesh segmentation in editor 1 Answer

Listen to Keyboard Events on EditorWindow 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