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?
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.