Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by Zwer99 · Dec 10, 2014 at 04:30 PM · editor-scriptingsceneviewgizmosgizmo

How to hide Gizmos by script

Hey there!

In the scene view there's a drop down menu where you can enable/disable Gizmos of a specific type (for example PolygonCollider2D). How can I access these Gizmo settings by an EditorScript?

I have an Editor-Script with two buttons to enable/disable my custom SceneView-Editor:

     private void StartEditing()
     {
         Tools.current = Tool.None;
         editing = true;
 
         SceneView sv = EditorWindow.GetWindow<SceneView>();
         sv.maximized = true;
 
         //DISABLE GIZMOS
         //sv.???
 
         SceneView.RepaintAll();
     }
 
 
     private void StopEditing()
     {
         Tools.current = Tool.Move;
         editing = false;
 
         SceneView sv = EditorWindow.GetWindow<SceneView>();
         sv.maximized = false;
 
         //ENABLE GIZMOS AGAIN
         //sv.???
 
         SceneView.RepaintAll();
     }

Is there any possibility?

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 meat5000 ♦ · Dec 10, 2014 at 04:29 PM 0
Share

Start here

Search Gizmos in the Script API

avatar image Zwer99 · Dec 10, 2014 at 05:02 PM 0
Share

Thank's! It works ;)

4 Replies

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

Answer by Zwer99 · Dec 10, 2014 at 05:43 PM

Just to be able to check my question as answered: Here's my solution, which worked.

 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Reflection;
 using System.Collections;
 
 public class CustomEditorUtilities
 {
     public static void ToggleGizmos(bool gizmosOn)
     {
         int val = gizmosOn ? 1 : 0;
         Assembly asm = Assembly.GetAssembly(typeof(Editor));
         Type type = asm.GetType("UnityEditor.AnnotationUtility");
         if (type != null)
         {
             MethodInfo getAnnotations = type.GetMethod("GetAnnotations", BindingFlags.Static | BindingFlags.NonPublic);
             MethodInfo setGizmoEnabled = type.GetMethod("SetGizmoEnabled", BindingFlags.Static | BindingFlags.NonPublic);
             MethodInfo setIconEnabled = type.GetMethod("SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic);
             var annotations = getAnnotations.Invoke(null, null);
             foreach (object annotation in (IEnumerable)annotations)
             {
                 Type annotationType = annotation.GetType();
                 FieldInfo classIdField = annotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance);
                 FieldInfo scriptClassField = annotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance);
                 if (classIdField != null && scriptClassField != null)
                 {
                     int classId = (int)classIdField.GetValue(annotation);
                     string scriptClass = (string)scriptClassField.GetValue(annotation);
                     setGizmoEnabled.Invoke(null, new object[] { classId, scriptClass, val });
                     setIconEnabled.Invoke(null, new object[] { classId, scriptClass, val });
                 }
             }
         }
     }
 }

Source of the script

A big thank you to meat5000 showing me the link ;)

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 meat5000 ♦ · Dec 10, 2014 at 05:44 PM 0
Share

$$anonymous$$ost Welcome.

avatar image sean244 · Oct 28, 2018 at 02:03 AM 0
Share

Thank you for this. It helped me as well :)

avatar image GoodGabriel · Oct 15, 2020 at 05:21 PM 0
Share

Unity reported me one runtime error with this function, I searched in the github repository and then i modified the script as follow. I added a boolean at the end of the setgizmoenabled invoke setting it false ( It is the addTo$$anonymous$$ostRecentChanged parameter ).

This is the link to the repository: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/$$anonymous$$ono/Annotation/AnnotationUtility.bindings.cs

 void ToggleGizmos(bool gizmosOn)
         {
             int val = gizmosOn ? 1 : 0;
             Assembly asm = Assembly.GetAssembly(typeof(Editor));
             
             Type type = asm.GetType("UnityEditor.AnnotationUtility");
 
             if (type != null)
             {
                 $$anonymous$$ethodInfo getAnnotations = type.Get$$anonymous$$ethod("GetAnnotations", BindingFlags.Static | BindingFlags.NonPublic);
                 $$anonymous$$ethodInfo setGizmoEnabled = type.Get$$anonymous$$ethod("SetGizmoEnabled", BindingFlags.Static | BindingFlags.NonPublic);
                 $$anonymous$$ethodInfo setIconEnabled = type.Get$$anonymous$$ethod("SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic);
                 
                 var annotations = getAnnotations.Invoke(null, null);
                 
                 foreach (object annotation in (IEnumerable)annotations)
                 {
                     Type annotationType = annotation.GetType();
                     FieldInfo classIdField = annotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance);
                     FieldInfo scriptClassField = annotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance);
                     
                     if (classIdField != null && scriptClassField != null)
                     {
                         int classId = (int)classIdField.GetValue(annotation);
                         string scriptClass = (string)scriptClassField.GetValue(annotation);
                         setGizmoEnabled.Invoke(null, new object[] { classId, scriptClass, val, false});
                         setIconEnabled.Invoke(null, new object[] { classId, scriptClass, val });
                     }
                 }
             }











avatar image
1

Answer by Bodrp · Mar 23 at 05:41 PM

If the goal is to toggle ON or OFF all gizmos, these lines should suffice:

 // to toggle OFF gizmos:
 SceneView.lastActiveSceneView.drawGizmos = false;
 
 // to toggle ON gizmos:
 SceneView.lastActiveSceneView.drawGizmos = true;

It is equivalent to toggling the "Gizmos" button in the upper-right corner of the scene view window.

More information about the SceneView API can be found here: https://docs.unity3d.com/ScriptReference/SceneView.html

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 Grizmu · Aug 06, 2019 at 08:59 PM

If you need an out of the box solution I've released an asset some time ago called VoltGizmos, which has a scripting api where you can hide and show gizmos for given script types, and much more.


You can check it out 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 Acegikmo · Apr 25, 2020 at 12:02 PM

If you want to toggle only a specific MonoBehaviour scene view icon, here's a lil snippet!


 static MethodInfo setIconEnabled;
 static MethodInfo SetIconEnabled => setIconEnabled = setIconEnabled ??
 Assembly.GetAssembly( typeof(Editor) )
 ?.GetType( "UnityEditor.AnnotationUtility" )
 ?.GetMethod( "SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic );
 public static void SetGizmoIconEnabled( Type type, bool on ) {
     if( SetIconEnabled == null ) return;
     const int MONO_BEHAVIOR_CLASS_ID = 114; // https://docs.unity3d.com/Manual/ClassIDReference.html
     SetIconEnabled.Invoke( null, new object[] { MONO_BEHAVIOR_CLASS_ID, type.Name, on ? 1 : 0 } );
 }





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

7 People are following this question.

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

Related Questions

Component on SceneView camera has wrong gameObject In OnDrawGizmos. 0 Answers

Select object by selecting gizmo or handle? 1 Answer

Manually Update/Redraw Scene View? 7 Answers

Is it possible to change the implementation for the built-in move gizmo? 0 Answers

Gizmo question: How do I create a field of view using gizmos? 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