Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Prefab · Jun 22, 2016 at 02:25 AM · selectgizmodeselect

How to draw a gizmo only when an object is NOT selected?

I am using OnDrawGizmo to draw the trigger of an object in the scene view. I have several of these and so would like to see the bounds of the triggers without needing to select them. The problem is that using OnDrawGizmo basically hides the trigger, since it is drawn over the top of it. Therefore I would like for the gizmo to only draw when the object is NOT selected. I have tried setting the gizmo color to clear in OnDrawGizmoSelected but this does not seem to do anything. What do I need to change?

 void OnDrawGizmos() {
         Gizmos.color = Color.blue;
         Gizmos.DrawWireCube(transform.position, new Vector3(transform.lossyScale.x, transform.lossyScale.y, transform.lossyScale.z));
 }
 void OnDrawGizmosSelected() {
         Gizmos.color = Color.clear;
 }
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

3 Replies

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

Answer by Addyarb · Jun 22, 2016 at 04:44 AM

So this is totally un-tested code - but if this doesn't give you the solution, hopefully it will at least point you in the right direction.

On line 2, put this:

  if (UnityEditor.Selection.activeGameObject != this.gameObject) return;

The UnityEditor namespace has a handy GameObject array of all of the selected GameObjects. For instance, if you select ALL of the GameObjects in your hierarchy, the UnityEditor.Selection.gameObjects array will contain each one of those objects.

What the code line above is doing is 'breaking out of' the method if the gameObject that said script is attached to is a selected gameObject. Thus, not allowing the rest of the code to run and effectively disabling the gizmo by not letting the method get to the code that renders said gizmo.

If you have any questions about any part of this post, please feel free to comment back.

Resource:

UnityEditor.Selection.gameObjects

Comment
Add comment · Show 3 · 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 Prefab · Jun 22, 2016 at 07:29 AM 0
Share

That didn't work. I have tried using Selection.activeGameObject but that didn't work either.

avatar image Addyarb · Jun 22, 2016 at 02:31 PM 1
Share

Sorry for the confusion, I forgot to add 'gameObject' into the mix.

The revised code looks like this. Tested this time!

Good luck.

  if (UnityEditor.Selection.activeGameObject != gameObject) return;
         Gizmos.DrawWireCube(transform.position, new Vector3(transform.lossyScale.x, transform.lossyScale.y, transform.lossyScale.z));
avatar image Prefab Addyarb · Jun 23, 2016 at 02:30 AM 0
Share

Thanks for that, but I think that you meant "==" since "!=" draws the gizmo for only the selected gameobject whereas "==" draws only the non selected ones.

avatar image
1

Answer by TSWessel · Feb 14, 2020 at 12:25 PM

The simplest way to do this is in a custom gizmo drawer class using the DrawGizmo attribute. In your case it would look something like this:

 using UnityEditor;
 
 public class MyObjectGizmoDrawer
 {
     [DrawGizmo(GizmoType.NotInSelectionHierarchy)]
     private static void DrawGizmo(MyObject source, GizmoType type)
     {
         //  Do your gizmo drawing here
     }
 }
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
avatar image
0

Answer by jonc_io · Oct 30, 2018 at 06:08 PM

For me I had an editor-only bool, "isSelected", and toggled that on/off to change behaviour. Like so:

 #if UNITY_EDITOR
 private bool isSelected;
 
 void OnDrawGizmos()
 {
     // Draw always-visible gizmos here
 
     if (isSelected)
     {
         isSelected = false; // Will change back to true if still selected
        return;
     }
 
     // Draw unselected-only gizmos here
 }
 
 void OnDrawGizmosSelected()
 {
     isSelected=true;
 
     // Draw selected gizmos
 }
 #endif
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

60 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to deselect dropdown ? 0 Answers

Why is this gizmo (specifically from the AudioSource Component) appearing in my game window on play? 1 Answer

Disable yellow lines ? 0 Answers

Input Field not getting selected? 0 Answers

How do I create a scene view in an editor window WITH the gizmo activated ? 0 Answers


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