Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Official Scene Visibility Tools - Info and Discussion Thread

Discussion in 'World Building' started by gabrielw_unity, Dec 5, 2018.

  1. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    786
    Hi and thanks for your reply. The video I made is from only one opened scene in the scene view. Therefore I am not sure my case can be attributed to a multi scene setup. But I do expand and collapse the hierarchy tree often (who doesn't :D). I also had this error today in a multi scene setup and even "Shift + H" couldn't help me there :-/
     
    awesomedata likes this.
  2. awesomedata

    awesomedata

    Joined:
    Oct 8, 2014
    Posts:
    1,415
    This is where I think it happens -- as a sort of 'trickle-down' effect (which is why I think it appears 'random' to us).

    The only way to get things back to normal after what you described above is closing/reopening the Unity Editor itself (to prevent the bug from repeating later on). If you don't close your whole project, including the Editor, it continues to appear in weird ways, until something simply either won't hide/show or be selectable again anymore. Did your issue persist without closing the Editor, or were you able to continue normally without ever shutting the editor itself down (i.e. to get to that single-scene project)?
     
  3. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    786
    Yes, that's also my experience. I have to restart Unity to solve it.
     
    awesomedata likes this.
  4. dumoulinantoine

    dumoulinantoine

    Joined:
    Jun 10, 2021
    Posts:
    2
  5. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    178
    Hi, I want to ask, is there an access from an editor script to this scene visibility toggle?
    upload_2021-10-16_16-55-54.png
    SceneVisibilityManager.instance.IsHidden() does not take the toggle into consideration and SceneView exposes all other settings but not this one.
     
    _geo__ likes this.
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    786
    It seems the SceneVisibilityManager started to register UNDO events on the undo stack in the latest LTS releases (tested with: Unity 2019.4.31f1). Is there a way to stop it from doing that?
    I am making a lot of custom show/hides via scripts which are then polluting the undo stack.
     
  7. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    786
    Found a solution. Caveat: I used reflections.
    @Epsilon_Delta : this also includes the reflection code to get that on/off setting you have been asking about.

    Here is the code:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. public static class VisibilityStateInternals
    3. {
    4.     // Thanks SceneVis hidden APIs, *sigh*
    5.     #region reflection
    6.  
    7.     // reflection cache (speeds up code execution)
    8.     private static Type[] _refEditorTypes;
    9.     private static Type _refSceneVisibilityStateType;
    10.     private static System.Reflection.MethodInfo _refSceneVisibilityState_GetInstanceMethod;
    11.     private static System.Reflection.MethodInfo _refSceneVisibilityState_SetGameObjectHiddenMethod;
    12.     private static System.Reflection.PropertyInfo _refSceneVisibilityState_VisibilityActiveProperty;
    13.  
    14.     private static void buildReflectionCache()
    15.     {
    16.         try
    17.         {
    18.             if (_refEditorTypes != null)
    19.                 return;
    20.              
    21.             _refEditorTypes = typeof(Editor).Assembly.GetTypes();
    22.             if (_refEditorTypes != null && _refEditorTypes.Length > 0)
    23.             {
    24.                 _refSceneVisibilityStateType = _refEditorTypes.FirstOrDefault(t => t.Name == "SceneVisibilityState");
    25.                 if (_refSceneVisibilityStateType != null)
    26.                 {
    27.                     _refSceneVisibilityState_GetInstanceMethod = _refSceneVisibilityStateType.GetMethod(
    28.                         "GetInstance",
    29.                         System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
    30.  
    31.                     _refSceneVisibilityState_SetGameObjectHiddenMethod = _refSceneVisibilityStateType.GetMethod(
    32.                         "SetGameObjectHidden",
    33.                         System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
    34.  
    35.                     _refSceneVisibilityState_VisibilityActiveProperty = _refSceneVisibilityStateType.GetProperty("visibilityActive");
    36.                 }
    37.             }
    38.         }
    39.         catch (Exception)
    40.         {
    41.             // fail silently
    42.         }
    43.     }
    44.  
    45.     /// <summary>
    46.     /// Based on the info found here:
    47.     /// https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/Mono/SceneView/SceneVisibilityState.bindings.cs#L20
    48.     /// and here:
    49.     /// https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/Mono/SceneVisibilityManager.cs
    50.     /// </summary>
    51.     /// <returns></returns>
    52.     public static UnityEngine.Object GetSceneVisibilityStateViaReflection()
    53.     {
    54.         try
    55.         {
    56.             buildReflectionCache();
    57.             return (UnityEngine.Object) _refSceneVisibilityState_GetInstanceMethod.Invoke(null, new object[] { });
    58.         }
    59.         catch (Exception)
    60.         {
    61.             // fail silently
    62.             return null;
    63.         }
    64.     }
    65.  
    66.     /// <summary>
    67.     /// Based on the info found here:
    68.     /// https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/Mono/SceneView/SceneVisibilityState.bindings.cs#L20
    69.     /// </summary>
    70.     /// <param name="gameObject"></param>
    71.     /// <param name="isHidden"></param>
    72.     /// <param name="includeChildren"></param>
    73.     /// <returns>True if the reflection code has executed without exceptions.</returns>
    74.     public static bool SetGameObjectHiddenNoUndoViaReflection(GameObject gameObject, bool isHidden, bool includeChildren)
    75.     {
    76.         try
    77.         {
    78.             buildReflectionCache();
    79.             var state = GetSceneVisibilityStateViaReflection();
    80.             if (state != null)
    81.             {
    82.                 _refSceneVisibilityState_SetGameObjectHiddenMethod.Invoke(state, new object[] { gameObject, isHidden, includeChildren });
    83.             }
    84.             return true;
    85.         }
    86.         catch (Exception)
    87.         {
    88.             return false;
    89.         }
    90.     }
    91.  
    92.     /// <summary>
    93.     /// Return true is visiblity is active, otherwise false<br />
    94.     /// Notice: It will return false if reflection failed.
    95.     /// </summary>
    96.     /// <returns></returns>
    97.     public static bool IsVisibilityActiveViaReflection()
    98.     {
    99.         try
    100.         {
    101.             buildReflectionCache();
    102.             var state = GetSceneVisibilityStateViaReflection();
    103.             if (state != null)
    104.             {
    105.                 return (bool)_refSceneVisibilityState_VisibilityActiveProperty.GetValue(state, null);
    106.             }
    107.             return false;
    108.         }
    109.         catch (Exception)
    110.         {
    111.             return false;
    112.         }
    113.     }
    114.  
    115.     #endregion
    116. }
    117. #endif
    118.  
    It is based on the code sources found here:
    https://github.com/Unity-Technologi...ceneView/SceneVisibilityState.bindings.cs#L20
    https://github.com/Unity-Technologi...efd1761/Editor/Mono/SceneVisibilityManager.cs

    @ Unity: Please expose those APIs. We need them ;-)
    Cheers!
     
    StarTwinkle and Epsilon_Delta like this.
  8. UnlimitedK

    UnlimitedK

    Joined:
    Aug 16, 2021
    Posts:
    2
    hello, now it can be used?
    i found this web,but i still dont know how to use this api.
    Unity - Scripting API: SceneVisibilityManager (unity3d.com)
    i want put a gameobject in this api,and then the gameobject can be hidden in sceneview.like ths "H"/"SHIFT+H".
    thx~ need reply.
     
  9. UnlimitedK

    UnlimitedK

    Joined:
    Aug 16, 2021
    Posts:
    2
    ok,fine,i found example
    1. var sv= SceneVisibilityManager.instance;
    2. sv.DisablePicking(go,false);
    like this ,now i know that.

    but i still dont know how to enter Isolation Mode.
    the api only show me how to exit Isolation Mode.
    ExitIsolation();

    ok, i know ,
    Isolate(a, true);
     
    Last edited: Dec 7, 2021
  10. MOARNial

    MOARNial

    Joined:
    Mar 19, 2019
    Posts:
    3