Search Unity

Running a script on each scene during a build? (PostProcessSceneAttribute?)

Discussion in 'Editor & General Support' started by bluescrn, Jan 30, 2019.

  1. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    Hi,

    Is there any way to run an editor script on each scene built at build time?

    I have some editor code that generates meshes, and I can do this at runtime while running in the editor, but for a final build, I'd like these meshes 'pre-baked' for performance, and I'd like to ensure that editor-only data (a load of GameObjects needed only to generate those meshes) is excluded from the build.

    (Yes, I could add a 'bake' button to my inspector and do it manually. But there's quite a lot of scenes involved, so I'd rather ensure that it's done by an automated process that always happens when doing a build, to make sure the data is up-to-date and in the latest format if I change bake script)

    PostProcessSceneAttribute sounds almost like what I want... but the docs say 'Add this attribute to a method to get a notification just after building the Scene'? - which doesn't seem very useful? - Is there an event just *before* building the scene but after loading it?

    Then again, I haven't tried it yet - there's a comment from 2016 claiming it runs before building the scene - which could be just what I need... I'd rather not rely on undocumented behaviour though?

    I guess the other option is an editor script that iterates through all scenes before the build (and then starts a build) - but just loading and unloading each scene could have a not-significant effect on the overall build time?