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

Gizmos in render texture

Discussion in 'Editor & General Support' started by Phantom_X, Feb 26, 2021.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    313
    Hey,
    I am making a camera that renders into a render texture to create some effects. Problem is that the scene gizmos are rendering into the render texture. How can I make a specific camera not render the scene's gizmos?

    Thanks!

     
  2. MrSIiddes

    MrSIiddes

    Joined:
    Sep 6, 2018
    Posts:
    1
    Hi, not sure if this can help, but I have found a way to turn off the Gizmos via script. You could turn off the gizmos before rendering to your RenderTexture and then turning it back on after finishing rendering.

    Reference code: https://answers.unity.com/questions/851470/how-to-hide-gizmos-by-script.html

    ! The reference code didn't work for me out of the box, I had to add a extra parameter (false) to setGizmoEnabled.Invoke. Not sure what the bool stands for but false seems to do the trick.
    setGizmoEnabled.Invoke(null, new object[] { classId, scriptClass, val, false });


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System;
    5. using System.Reflection;
    6. using System.Collections;
    7.  
    8. public static void ToggleGizmos(bool gizmosOn)
    9. {
    10.     int val = gizmosOn ? 1 : 0;
    11.     Assembly asm = Assembly.GetAssembly(typeof(UnityEditor.Editor));
    12.     Type type = asm.GetType("UnityEditor.AnnotationUtility");
    13.     if(type != null)
    14.     {
    15.         MethodInfo getAnnotations = type.GetMethod("GetAnnotations", BindingFlags.Static | BindingFlags.NonPublic);
    16.         MethodInfo setGizmoEnabled = type.GetMethod("SetGizmoEnabled", BindingFlags.Static | BindingFlags.NonPublic);
    17.         MethodInfo setIconEnabled = type.GetMethod("SetIconEnabled", BindingFlags.Static | BindingFlags.NonPublic);
    18.         var annotations = getAnnotations.Invoke(null, null);
    19.         foreach(object annotation in (IEnumerable)annotations)
    20.         {
    21.             Type annotationType = annotation.GetType();
    22.             FieldInfo classIdField = annotationType.GetField("classID", BindingFlags.Public | BindingFlags.Instance);
    23.             FieldInfo scriptClassField = annotationType.GetField("scriptClass", BindingFlags.Public | BindingFlags.Instance);
    24.             if(classIdField != null && scriptClassField != null)
    25.             {
    26.                 int classId = (int)classIdField.GetValue(annotation);
    27.                 string scriptClass = (string)scriptClassField.GetValue(annotation);
    28.                 setGizmoEnabled.Invoke(null, new object[] { classId, scriptClass, val, false });
    29.                 setIconEnabled.Invoke(null, new object[] { classId, scriptClass, val });
    30.             }
    31.         }
    32.     }
    33. }
    34.