HideFlags not saving to prefab

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

or is there a way I can detect when something becomes a prefab/prefab Instance and modify the hideFlags then?

well, this seems to work so far:

using UnityEngine;
using System.Collections;

[AddComponentMenu(""), ExecuteInEditMode]
public class MyClass : MonoBehaviour {

    void OnEnable() {

        SetHideFlags();
    }

    private void SetHideFlags() {

        if (!Application.isPlaying) {

            gameObject.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;

#if UNITY_EDITOR
            GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(gameObject) as GameObject;

            if (prefab != null) {

                prefab.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
            }
#endif
        }
        else {

            gameObject.SetActive(false);
        }
    }
}

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 :frowning:
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

3 Likes

:cry:

Ditto: Endless Editor Hacks

:frowning:

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:

var hideFlagsProp = serializedObject.FindProperty("m_ObjectHideFlags");
hideFlagsProp.intValue = (int) HideFlags.HideInHierarchy;
serializedObject.ApplyModifiedProperties();

When you change a scene gameObject’s hideFlags this way, then make it a prefab, hideFlags will be saved as well.