- Home /
How to swap lightmaps?
My guess was that it should be enough with something like this:
var myLM : Texture2D;
LightmapSettings.lightmaps[i].lightmapFar = myLM;
Being i a valid index, of course.
It doesn't work as I expected.
In fact, when printing the .name of that lightmapFar, before and after the assignation, the same original name is given. What surprises me most is that, while in play mode, I can assign the texture manually in the lightmapper interface and it gets correctly updated.
Anyone knows how should I do it in order to make it work in runtime from a script?
Thanks!
Answer by Memige · Nov 13, 2012 at 01:14 AM
Hi there! I just got such a thing working the other week through assetBundles, below is an adaptation that should work for you.
var myLMs : Texture2D[] = //populate your custom lightmaps
var lightmaparray : LightmapData[] = LightmapSettings.lightmaps;
for (var i=0; i < lightmaparray.Length; i++) {
var mapdata : LightmapData = new LightmapData();
mapdata.lightmapFar = myLMs[i]
lightmaparray[i] = mapdata;
mapdata = null;
}
LightmapSettings.lightmaps = lightmaparray;
I'm actually a C# guy, so please forgive if I've made any mistakes in translating to js, but I believe this should work.
Interesting... It works! I tried something similar before, but never created and replaced a new complete array. I believe that's the key thing. Thank you $$anonymous$$emige!
Not to sound like a complete noob, but how do I populate my lightmaps? As in how do I link the folder I have my other lightmaps in to the myL$$anonymous$$s variable?
$$anonymous$$raegin: You would do this by dragging and dropping your lightmaps from their folder in the project view onto the slots in the array in the inspector.
This is a very common way of working in Unity. Create a public variable to hold a reference or data in your script and populate that variable with a reference or data in the inspector.
i'v tried your solution which seams great but i have the follow error message :
"get_lightmaps can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function"
any idea ? :)
Your answer
Follow this Question
Related Questions
Swapping lightmaps during runtime? 1 Answer
Can I create lightmaps at runtime? 3 Answers
How to add new baked lightmaps to lightmap array? 2 Answers
Blending between 2 lightmaps 0 Answers
change lightmaps at run time? 1 Answer