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.
  2. Dismiss Notice

HideFlags not saving to prefab

Discussion in 'Editor & General Support' started by CDF, Nov 26, 2014.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,281
    Hi, not sure if this is by design, but if I set some hideFlags on a scene object then make it a prefab, the hideFlags are lost, they become "None"

    can I prevent this from happening somehow?

    Thanks
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,281
    or is there a way I can detect when something becomes a prefab/prefab Instance and modify the hideFlags then?
     
  3. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,281
    well, this seems to work so far:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [AddComponentMenu(""), ExecuteInEditMode]
    5. public class MyClass : MonoBehaviour {
    6.  
    7.     void OnEnable() {
    8.  
    9.         SetHideFlags();
    10.     }
    11.  
    12.     private void SetHideFlags() {
    13.  
    14.         if (!Application.isPlaying) {
    15.  
    16.             gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
    17.  
    18. #if UNITY_EDITOR
    19.             GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(gameObject) as GameObject;
    20.  
    21.             if (prefab != null) {
    22.  
    23.                 prefab.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
    24.             }
    25. #endif
    26.         }
    27.         else {
    28.  
    29.             gameObject.SetActive(false);
    30.         }
    31.     }
    32. }
    Make the component ExecuteInEditMode, in OnEnable modify the hideFlags. This works fine for the instanced version of your object. But as soon as it becomes a prefab things need to get dirty. Enter UnityEditor code inside UnityEngine code courtesy of #UNITY_EDITOR Compiler flag. Find the prefab, set the hideFlags.

    Couldn't find any other way. No way to detect if prefab has been made, not even through PostProcessor :(
    CustomEditors don't fire unless the object is selected, and even then, if the object is a prefab, you have to call AssetDatabase.ImportAsset to reload the hideFlags.

    Endless Editor Hacks
     
  4. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    384
  5. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    551
    Ditto: Endless Editor Hacks

    :(
     
  6. SolidAlloy

    SolidAlloy

    Joined:
    Oct 21, 2019
    Posts:
    58
    I also struggled to save the changes to hideFlags in a prefab. Apparently, the only way to make them persistent is to access hideFlags through serializedObject:

    Code (CSharp):
    1. var hideFlagsProp = serializedObject.FindProperty("m_ObjectHideFlags");
    2. hideFlagsProp.intValue = (int) HideFlags.HideInHierarchy;
    3. serializedObject.ApplyModifiedProperties();
    When you change a scene gameObject's hideFlags this way, then make it a prefab, hideFlags will be saved as well.