Hey guys,
I know this is kind of weird, but I would like to have the following behaviour:
As soon as I click Generate Lighting I want a selection of GameObjects to toggle themselves static and - when Lightmapping ends - to untoggle themselves again.
I know there is events for the Lightmapping parts, but I have no idea about Editor Scripting.
Do I add a script to all my relevant gameobjects? Do I provide a list to a Editor Script?
Any help is greatly appreciated.
Thanks.
I’d approach this by exposing a callback receiver interface like this:
public interface ILightmapBakeCallbackReceiver
{
void OnLightmapBakeStarted();
void OnLightmapBakeCompleted();
}
And then implementing support for the interface by using [InitializeOnLoadMethod] to subscribe to the lightmapping events:
static class LightmapBakeCallbackManager
{
[InitializeOnLoadMethod]
static void InitializeCallbacks()
{
Lightmapping.bakeStarted += () => OnBakeStarted(SceneManager.GetActiveScene());
Lightmapping.bakeCompleted += () => OnBakeCompleted(SceneManager.GetActiveScene());
}
static void OnBakeStarted(Scene scene)
{
foreach (var receiver in EnumerateComponentsInScene<ILightmapBakeCallbackReceiver>(scene, false))
receiver.OnLightmapBakeStarted();
}
static void OnBakeCompleted(Scene scene)
{
foreach (var receiver in EnumerateComponentsInScene<ILightmapBakeCallbackReceiver>(scene, false))
receiver.OnLightmapBakeCompleted();
}
static IEnumerable<T> EnumerateComponentsInScene<T>(Scene scene, bool includeInactive)
{
foreach (GameObject root in scene.GetRootGameObjects())
{
foreach (T component in root.GetComponentsInChildren<T>(includeInactive))
{
yield return component;
}
}
}
}
With all of these pieces in place, you can then implement your desired functionality with a component:
class MakeStaticDuringLightmapBake : MonoBehaviour, ILightmapBakeCallbackReceiver
{
public void OnLightmapBakeStarted()
{
gameObject.isStatic = true;
}
public void OnLightmapBakeCompleted()
{
gameObject.isStatic = false;
}
}
2 Likes
Holy Mother, that’s way more support then I expected. Thanks so much. Greatly appreciated!
1 Like