Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change Scene View Toggles (Gizmos, Scene Visibility, etc) Via Script

Discussion in 'Scripting' started by Selzier, Dec 26, 2020.

  1. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
    Is there anyway to check the state of this button, or change it's state, via script?


    I was using SceneVisibilityManager.instance.Hide(...) to hide a gameobject but it wasn't working. Now I've found out why, but need to control this option via script.

    With no luck, I searched if it's possible to change the Gizmo's button via script... didn't find any solution for that.

    Is there anyway to change these buttons or at least check their state via script?
     
  2. Selzier

    Selzier

    Joined:
    Sep 23, 2014
    Posts:
    652
    I guess this is it?

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Reflection;
    5. using UnityEditor;
    6.  
    7. public class SceneViewGizmos {
    8.     private static bool _globalGizmosOn;
    9.  
    10.  
    11.     [MenuItem("Quick Fingers/Scene View/Toggle Gizmos &%g")] private static void ToggleAllSceneGizmos() {
    12.         _globalGizmosOn = !_globalGizmosOn;
    13.         ToggleGizmos(_globalGizmosOn);
    14.     }
    15.  
    16.     [MenuItem("Quick Fingers/Scene View/Disable All Gizmos")] private static void DisableAllSceneGizmos() {
    17.         _globalGizmosOn = false;
    18.         ToggleGizmos(_globalGizmosOn);
    19.     }
    20.  
    21.     [MenuItem("Quick Fingers/Scene View/Enable All Gizmos")] private static void EnableAllSceneGizmos() {
    22.         _globalGizmosOn = true;
    23.         ToggleGizmos(_globalGizmosOn);
    24.     }
    25.  
    26.     private static void ToggleGizmos(bool gizmosOn) {
    27.         int val = gizmosOn ? 1 : 0;
    28.         Assembly asm = Assembly.GetAssembly(typeof(Editor));
    29.         Type type = asm.GetType("UnityEditor.AnnotationUtility");
    30.         if (type != null) {
    31.             MethodInfo getAnnotations = type.GetMethod("GetAnnotations", BindingFlags.Static | BindingFlags.NonPublic);
    32.             MethodInfo setGizmoEnabled = type.GetMethod("SetGizmoEnabled", BindingFlags.Static | BindingFlags.NonPublic);
    33.             MethodInfo setIconEnabled = type.GetMethod("SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic);
    34.             var annotations = getAnnotations.Invoke(null, null);
    35.             foreach (object annotation in (IEnumerable)annotations) {
    36.                 Type annotationType = annotation.GetType();
    37.                 FieldInfo classIdField = annotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance);
    38.                 FieldInfo scriptClassField = annotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance);
    39.                 if (classIdField != null && scriptClassField != null) {
    40.                     int classId = (int)classIdField.GetValue(annotation);
    41.                     string scriptClass = (string)scriptClassField.GetValue(annotation);
    42.                     setGizmoEnabled.Invoke(null, new object[] { classId, scriptClass, val });
    43.                     setIconEnabled.Invoke(null, new object[] { classId, scriptClass, val });
    44.                 }
    45.             }
    46.         }
    47.     }
    48. }
    49.