- Home /
How to switch lightmaps in a scene in Unity 5.3.5
I am try to switch from day to night in a scene in Unity that uses lightmaps.
I tried this
LightmapData[] lightmapData;
void Start (){
lightmapData = new LightmapData[1];
}
#region Publics
public void SetToDay(){
lightmapData[0] = new LightmapData();
lightmapData[0].lightmapFar = Resources.Load( "Level3/Lightmap-0_comp_light", typeof(Texture2D)) as Texture2D;
LightmapSettings.lightmaps = lightmapData;
}
public void SetToNight(){
lightmapData[0] = new LightmapData();
lightmapData[0].lightmapFar = Resources.Load( "Level3Night/Lightmap-0_comp_light", typeof(Texture2D)) as Texture2D;
LightmapSettings.lightmaps = lightmapData;
}
Anyone get switching between two different lightmaps to work in Unity 5?
I don't see how can you share same lightmaps between day and night. That would be completely different lighting, no?
For my purposes I've simply duplicated a scene. The objects such as houses or walls remain the same. Terrain asset is automatically shared when you duplicated the scene asset. I've assigned new lights and skybox to the scenes manually. When you edit some piece of scene, for example buildings or trees, simply copy "CTRL-C" an element, switch scene, delete existing and paste "CTRL-V" new one. Lightbake both scenes. Voila.
He doesn't try to share the same lightmap, on the contrary, he wants to use the same scene for day and night, with a lightmap for day and another lightmap for night. When the game switches from day to night, lightmap setting should be switched to night lightmap.
This is exactly what I'm trying to achieve. Anyone succeeded doing this on Unity 5.3+ ??
(The code $$anonymous$$eithDuck shared would probably work on Unity 4.x)
I wrote a simple script and tutorial for this in this post: https://forum.unity3d.com/threads/how-to-create-lightmap-for-day-and-for-night-and-switch-them-in-runtime.319920/#post-2873800
Answer by tomekkie2 · Dec 01, 2016 at 06:30 PM
Suppose you have scene L1, bake lightmaps in it, then duplicate it, name the duplicate scene as "L2", setup new lighting and bake the L2 scene. Unity will create folder L2 containing the lightmaps for the L2 scene. Then create a Resources folder and move the L2 folder to it.
Then the script with function to swap the lightmaps could look very similar to one in the initial question, just like below:
using UnityEngine;
using System.Collections;
public class LightmapsSwap : MonoBehaviour {
void Start () {
SwapLightmaps();
}
void SwapLightmaps () {
LightmapData[] sceneLightmaps = LightmapSettings.lightmaps;
foreach (LightmapData lmd in sceneLightmaps)
{
lmd.lightmapFar = Resources.Load("L2/" + lmd.lightmapFar.name) as Texture2D;
lmd.lightmapNear = Resources.Load("L2/" + lmd.lightmapNear.name) as Texture2D;
}
LightmapSettings.lightmaps = sceneLightmaps;
}
}
If these were just normal textures, you wouldn't have to move them to "Resources" and use Resources.Load; you could just use the WWW class.
I have tested it and it works.
Your answer
Follow this Question
Related Questions
How to unwrap for light mapping 1 Answer
Lightmaps baking problem 3 Answers
SWAPPING BAKED LIGHTMAPS 0 Answers
Server for baking lights \w Enlighten 0 Answers
realtime light breaks baked GI 1 Answer