- Home /
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?
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 });
}
}
}
}
}
A big thank you to meat5000 showing me the link ;)
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 });
}
}
}
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
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 } );
}