laurenth-personal / lightmap-switching-tool

Tool that allows switching different baked lightmap sets on a unity scene at runtime.
MIT License
657 stars 88 forks source link

Workaround for Build and Store #13

Open markvitapoly opened 6 years ago

markvitapoly commented 6 years ago

Hi, I am using Unity 2017.2.0f3 and the lightmaps are messed up after baking. I see this comment:

The "Build" and the "Store" steps have not been merged because in some builds ( seen in 5.5.0f1 ) the lightmaps don't load correctly after baking

I have a few lightmaps set up for a particular scene so I modified the script to build and store them all overnight. Is there a workaround to this existing issue? I've tired something like this:

    Debug.Log("Building Lights: " + ScenarioName);
    yield return BuildLightingAsync(ScenarioName);

    EditorSceneManager.CloseScene(EditorSceneManager.GetSceneByPath(BaseScenePath + "/" + ScenarioName + ".unity"), true);

    Debug.Log("Enter Play Mode");
    EditorApplication.ExecuteMenuItem("Edit/Play");

    ///////// The below code doesn't get executed anymore once Play mode starts. Workarounds?
    Debug.Log("Waiting 30 seconds");
    yield return new WaitForSeconds(30);              

    Debug.Log("Exit Play Mode");
    EditorApplication.ExecuteMenuItem("Edit/Play");

    Debug.Log("Storing Light");
    StoreLightmapInfos(storeIndex);

Thanks!

laurenth-personal commented 6 years ago

Hi ! As this is not an officially supported unity workflow, I can't really ask for a fix on unity side. Storing right after build however works on SOME unity builds, but I believe in the latest ones it's consistently broken, so I am interested in providing a reliable workaround. I'll try what you suggest and see if I can make it work. Thanks for the suggestion

markvitapoly commented 6 years ago

Just want to let you know that I was able to make it work with some hacks. I set some PlayerPref variables indicating that it's light building mode that triggered the play event. Then when in Play mode, it will wait a bit and then disable play mode updating the PlayerPref variables. In the editor script, I am also listening to EditorApplication.playModeStateChanged so I can catch when Unity stops playing and store the lightmaps. It'll continue to do this until all the lightmaps are finished.

It works, but it's pretty hacky. So, I think Unity core should officially support your project because using this project was how I got assetbundles to dynamically load lightmaps properly.

laurenth-personal commented 6 years ago

Trying a similar workaround ^^ if it works I'll put it in a branch. In the meantime I'll see if I can reproduce the bug with messed up lightmaps without using the script so I can report it.