Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

Request for a new editor callback (PostProcessPrefab)

Discussion in 'Prefabs' started by liortal, Sep 23, 2018.

  1. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,494
    Not entirely related to the new prefab workflows but still this would be super useful:

    Right now, the editor exposes a PostProcessScene attribute that i can use to receive a callback when a scene is processed; we are using this to perform build-time modifications to scenes (e.g: stripping certain objects, adding debug stuff, etc).

    In the past, i asked for the matching PostProcessPrefab attribute, since we'd like to perform the same "patching" at build time for prefabs as well (but to not persist this back to the project).

    I think i was told back then that this is probably not possible due to technical reasons (no way to roll back changes after the build?)

    Is this something that may be possible now with the new prefab system in place? i believe it can be super useful for other developers as well!
     
    davidrochin and ph_ like this.
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,461
    Since prefabs are now an imported asset, couldn't this be done with OnPostprocessAllAssets?

    I'd love there to be a PostProcessPrefab, as OnPostprocessAllAssets seems to be really slow, but it should at least work.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,494
    I want this to run during builds so that I can "patch" certain prefabs but leave them unchanged in my project.
     
    Deeeds likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,461
    Oh, I understand now. Yeah, that makes a lot of sense, it would be a very nice feature.
     
  5. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    27,132
    I ended up moving some work to scriptable objects for the patching you describe, then referencing that. I'm not sure if applicable, but could be an idea in the meantime.
     
  6. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    This would make an interesting workaround for async loading of prefabs, too... I think. Could put this on each massive prefab, and wait until they're all received before assuming all prefabs have been loaded and setup.

    Not efficient, would still bog the main thread, but at least it'd be possible to treat huge prefabs as scenes and know when they're ready.
     
  7. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,494
    Could u elaborate a bit on how your solution works?
     
  8. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    553
  9. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    605
  10. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,494
    We have custom build code for building asset bundles for our projects.
    Some of this process involves making "modifications" to prefabs and scenes as they are built.

    For scenes, we can use the PostProcessScene callback - it will be fired when building the scene, but i guess it is done on a copy of the real scene asset file, so any changes i make will not be saved to my project.

    For prefabs, we don't have that option, so what our build code does is create copies of prefabs and modify them. After the build completes it will "clean up" and make sure everything returns to the state it was before the build.

    It could be a lot easier to achieve this if i had a PostProcessPrefab callback that allowed me to process a prefab just as it gets built, but those changes won't get saved to the project.
     
  11. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,494
    Do you have any comments on how this can be supported @SteenLund ?
     
  12. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    925
    I would also really be interested in this feature, there's no way to remove any components from prefabs in build only at this stage in Unity I believe.
     
  13. James_FallenTreeGames

    James_FallenTreeGames

    Joined:
    Sep 30, 2016
    Posts:
    16
    We'd love this feature too. We are using the PostProcessScene to remove unneeded components from objects in the scene, but would love to be able to do this to our prefabs too at build time, so we can strip unneeded components but leave them untouched in the project.
     
  14. dsisianu

    dsisianu

    Joined:
    Dec 22, 2018
    Posts:
    1
    Bump.
    This would be a really great feature for optimizing some stuff for the build.
     
  15. EdwinLyons

    EdwinLyons

    Joined:
    Oct 4, 2012
    Posts:
    66
    I would also like this feature. I assume it doesn't exist as there may be many thousands of prefabs in a game and this could slow down builds quite a bit.
     
  16. ProdiggaPA

    ProdiggaPA

    Joined:
    Nov 29, 2019
    Posts:
    11
    This would be really useful. It would even be ok if we could post process the 'flattened' prefabs. ie Dont worry about nested prefabs/inheritance/etc, which is what I imagine introduces most of the complexity.

    Flatten everything in scenes, post process scenes. (PostProcessScene). Then flatten all prefabs, process those flattened prefabs (PostProcessPrefab).

    A 'quirk' here is that if SomePrefab is in SomeScene, your post process changes to SomePrefab wont appear in SomeScene, since SomeScene was flattened and all prefab information was lost. I think this is fine,

    We have our own hooks to perform some cleanup/etc at build time for scripts in scene. OnPostProcessScene we do something like "Find all <IBakeable> components in scene, call Bake on those components". We would just run the same logic in OnPostProcessPrefab. (GetComponentsInChildren<IBakeable> and call Bake on each".

    Thoughts, @SteenLund ?
     
    Last edited: Feb 23, 2021
  17. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    170
    We do a lot of edits to the game while building; I just revert them all in the source control after build is done.
     
  18. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    27,132
    Seems a bit painful.
     
  19. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    170
    Right now it's just the scene files, so I don't mind, but I guess I could commit the changes by mistake if I'm not careful. I didn't know about the OnPostProcessScene, I'm gonna try it to see if it work with our pipeline.
     
  20. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    27,132
    What Unity actually does when it builds a project or you enter play mode is *copy* the entire scene. So the preprocessor or processor lets you work on the *copied* scene, you aren't actually working on the safely stored scene.

    So you can go mad and nuke half of it in code, and it'll be fine...

    That's the right way to do this stuff.
     
    ProdiggaPA and mahdi_jeddi like this.
  21. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,200
    I actually needed this feature the other day. We remove some components at build time. But using the PostProcessScene only removes them from instances in scenes. They stil exist on prefabs.

    We do only use these prefabs for static scene objects so it works because we dont instance them in runtime. But still. Would have been more robust to remove them from prefabs too
     
    ProdiggaPA likes this.
  22. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    141
    +1, this would be extremely useful.
     
  23. davidrochin

    davidrochin

    Joined:
    Dec 17, 2015
    Posts:
    64
  24. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    133
    mahdi_jeddi, MDADigital and Baste like this.
  25. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,200
  26. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    605
    @MDADigital

    That callback is called during import of the prefab but not during the build process.

    I have spoken to the build pipeline team and they say that currently there is no way to do what you would like to do.

    Ideally Unity would have a staging area for assets where you would be allowed to modify the assets, without modifying the source assets, before they are included in the build
     
    ProdiggaPA likes this.
  27. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,200
    Allright, then the answer to @Mads-Nyholm is that it does not help in this case.

    edit: We want to remove design time stuff at build. Here is good example, https://github.com/microsoft/ProjectAcoustics/issues/33
     
  28. ProdiggaPA

    ProdiggaPA

    Joined:
    Nov 29, 2019
    Posts:
    11
    Yeah, this is it. We basically have this for scenes, but not for anything else. It is immensely useful for scenes. A staging area feature would be amazing.
     
  29. paulbettner

    paulbettner

    Joined:
    Jan 13, 2014
    Posts:
    14
    +1 for an asset staging area during builds - would give me exactly what we need!
     
unityunity