Search Unity

hideFlags = HideFlags.DontSave being saved to prefab

Discussion in 'Editor & General Support' started by Guedez, May 9, 2019.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    I have a script that fuses all skinned mesh renderers into a single one and delete some vertices (clothing over the skin, so delete the skin to avoid clipping issues, etc).
    The issue is that the generated mesh is being saved to the scene file. So i've tried about everything I could to make the mesh stop saving to the scene, even having the skinned mesh renderer be generated into a separate object that is flagged to not save. The issue then is that the prefab don't give a damn and saves the object anyways, then the scene saves the object with the mesh.

    It's 20 mega per character and 5 seconds of save/load if the mesh is saved to the scene, meanwhile, it's 0.1 seconds to generate the mesh on the fly.

    The object is being generated during
    Undo.undoRedoPerformed
    or
    private void OnValidate
    . I am pretty sure one of those run when you serialize a object to a prefab (drag from scene to assets folder). I am also pretty sure the issue will be solved if I can detect when it's being run as the object is being saved as a prefab.

    Deleting the object after the prafab is generated does nothing, as it's instantly recreated when I save

    This somewhat fixed it for a "while", but really didn't. Eventually the object ended up being saved to the prefab even though it's actually not (it appears in the scene view, but if I open the prafab it's not there).
    Code (CSharp):
    1.             if (Environment.StackTrace.Contains("Prefab")) {
    2.                 return;
    3.             }
    Even adding
    HideFlags.HideAndDontSave
    is not working at all, the object is still visible in the editor when it ends up being somehow added to the prefab

    Is there some way to make sure a object NEVER gets added to the prefab?
     
  2. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    I somehow managed, you need this before any code that creates objects attached to prefabs on OnValidate and the undo handles. Otherwise the editor will randomly add the created object to the prefab but at the same time not really. Very buggy behaviour
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.             if (Environment.StackTrace.Contains("Prefab") || Environment.StackTrace.Contains("Drag") || Environment.StackTrace.Contains("MenuItem")) {
    3.                 return;
    4.             }
    5.             if ((PrefabUtility.GetCorrespondingObjectFromSource(gameObject) == null && PrefabUtility.GetPrefabInstanceHandle(gameObject) != null)) {
    6.                 return;
    7.             }
    8. #endif
    There might be more instances where the editor might execute the script and end up saving the new object to the prefab, but I know none of