- Home /
change lightmaps at run time?
I know there has been several posts, but I still can't get the lightmaps immediately displayed on the screen. I use only one lightmap at one time, and the lightmap is updated and stored in the same name. I attach this script to the object which I baked the lightmaps to. Thanks very much!
public class RoomLightmap : MonoBehaviour {
private LightmapData[] lightmapdata = new LightmapData[1];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
lightmapdata[0] = new LightmapData();
lightmapdata[0].lightmapFar = Resources.Load("Final_LightmapFar", typeof(Texture2D)) as Texture2D;
LightmapSettings.lightmaps = lightmapdata;
renderer.lightmapIndex = 0;
renderer.enabled = true;
}
}
if your projet is in text mode ( Edit/Project Settings/Editor) can you say me what is the value of lightmapIndex in your RoomLightmap prefab ?
It must look like that in the file :
Renderer:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 100000}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_LightmapIndex: 255
m_LightmapTilingOffset: {x: 1, y: 1, z: 0, w: 0}
m_$$anonymous$$aterials:
- {fileID: 10302, guid: 0000000000000000e000000000000000, type: 0}
m_SubsetIndices:
m_StaticBatchRoot: {fileID: 0}
m_UseLightProbes: 0
m_LightProbeAnchor: {fileID: 0}
m_ScaleInLightmap: 1
--- !u!33 &3300000
If the index is not set to 0 I don't think if you do this :
renderer.lightmapIndex = 0;
can work.
Hi liszto,
Thanks very much for replying. I tried to remove the renderer.lightmapIndex = 0; and it didn't work too. Is that because if I overwrite the lightmap in the same name and load it, Unity does not work?
Actually I tried to name the lightmaps in different name, then it showed lightmap immediately, but seems that the lightmap is loaded as "Texture". So now the problem is that I don't know how to change the texture type of the lightmap to "Lightmap" at runtime. Do you have any idea? Thanks!
Answer by liszto · Feb 15, 2013 at 08:37 PM
You can look this Unity Answer topic, maybe this can help you ?
==> http://answers.unity3d.com/questions/49135/how-i-can-change-lightmaps-at-runtime.html#answer-359345