• 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 bibbinator · May 31, 2011 at 04:19 AM · gizmopickondrawgizmos

How to pick OnDrawGizmos gizmo?

I have an editor extension creates empty game object nodes. I show these in the scene view by using OnDrawGizmos event to Gizmos.DrawSphere at the correct location.

The documentation states that:

"Implement this OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn.

This allows you to quickly pick important objects in your scene."

But how do I actually detect that the sphere gizmo was picked?

Comment
Add comment · 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 yoyo · May 31, 2011 at 04:44 AM 0
Share

I'm not certain, but can you use Event.current in OnDrawGizmos to get mouse clicks and position in the scene view?

avatar image bibbinator ♦♦ · May 31, 2011 at 05:18 AM 0
Share

I tried trapping the On$$anonymous$$ouseDown event and it doesn't fire. I also tried looking at the Event during the OnDrawGizmos but there isn't any mouse event.

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by bibbinator · Jun 02, 2011 at 07:31 AM

After much digging around, I think I figured out how this works. Hopefully this will save somebody else some pain.

The OnDrawGizmos is only available to MonoBehavior objects. Unity therefore selects whichever game object that the component containing the OnDrawGizmos method is attached to. If you draw a number of gizmos within that OnDrawGizmos method, it is up to you to figure out which specific gizmo was picked, but that can't be done inside the OnDrawGizmos because you don't have access to the editor functions you need to make it work.

For me, that means I need to first attach a collider to the same object that the OnDrawGizmos method is attached to. Set the parameters to zero so they don't interfere with normal processing. E.g. if you're using a sphere, set the radius to zero.

Then, in my OnSceneGUI editor script:

  1. Check for Event.current.type == EventType.MouseDown

  2. Make a ray for the editor. E.g. Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition)

  3. Loop through each gizmo that was drawn.

  4. Set the collider to the same position and radius (in the case of a sphere).

  5. Call collider.Raycast using the editor camera ray to see if you hit it.

Comment
Add comment · Show 1 · 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 testure · Sep 16, 2012 at 01:55 AM 0
Share

Nice solution, saved me a bunch of time co$$anonymous$$g up with a workaround! I added an extra step to handle depth testing- I cached off any hits that were found and then checked each one against Camera.current to use whichever successful hit was closest.

avatar image
1

Answer by Jorhoto · May 28, 2021 at 07:49 AM

Fast and easy way is to just assign an icon (which can be an invisible texture) to the gameobject rendering your gizmo. This way you will select it on click.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Permanent handles (when object is not selected) 1 Answer

Mouse click in edit mode. ✱✺✸❁ 1 Answer

Equivalent of Awake() for Gizmos? 0 Answers

Constantly Update Scene View 1 Answer

Instantiating droppable items on destroy game object 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