- Home /
Access use lightmap property using script
Is there a way to access use Lightmap Property of lightmaps in unity 3d using script?
In a runtime script or an editor script ? Cause at runtime you can't do this there are only editor properties of Unity.
LightmapData lmData = new LightmapData();
lmData.lightmapFar = UnityEngine.Resources.Load("C:/Users/a/Documents/New Unity Project 3/Assets/lightmapping scene df/LightmapFar-1", Texture2D) as Texture2D;
I am trying to use this but it is showing error
Look my answer in this topic : http://answers.unity3d.com/questions/49135/how-i-can-change-lightmaps-at-runtime.html#answer-359345
Answer by liszto · Dec 05, 2012 at 12:33 PM
The lightmapData array is an array define in Unity you need to use it if you used my method. After you can do something like this :
LightmapData[] lightmapData = new LightmapData[2];
for(int i = 0 ; i < 2 ; i++ )
lightmapData[i] = new LightmapData();
for( int i = 0 ; i < 2 ; i++ )
{
lightmapData[i].lightmapFar = Resources.Load( "LightmapsCZC/" + trackName + "/LightmapFar-" + i.ToString(), typeof(Texture2D)) as Texture2D;
//lightmapData[i].lightmapNear = Resources.Load(trackName + "/LightmapNear-" + i.ToString(), typeof(Texture2D)) as Texture2D;
}
Resources.Load must used a path which starts in the Assets folder. With this method your code can work on multiple computer and not only on yours.
So don't use this :
Blockquote"C:/Users/a/Documents/New Unity Project 3/Assets/lightmapping scene df/"
But
"lightmapping scene df/"
Then use a naming convention as I used in my example. If I need to change my lightmaps I just used my level name and I search the lightmaps in the folder with this name in my resources folder. I must do this cause all the game is displayed in only one scene for me so I need to know which level is loaded to displayed the correct lightmap.
Thank you very much. I will try it and get back to you soon.
i am making a dummy project just to get the idea on how to use the switching of lighmapping to use in my actual game project. so there is no loading of levels in this one. and please tell me what is this "trackName"...???
The trackName is a prefab name and this prefab contains all my level informations/assets.
But for you, if you just have 1 scene, you can just do this :
Resources.Load( "YourLightmapFolder/LightmapFar-" + i.ToString(), typeof(Texture2D)) as Texture2D;
And in my case, I just use Single lightmap not dual, this is why I comment the second line with the LightmapNear field.
Edit : A little script for you : http://pastebin.com/23FdBnRU
Thanks liszto I tried this but the current lightmap disappears but the lightmap in the folder does not apply on my plane. Here is my code
void Start () { InitLightmaps(); }
private void InitLightmaps( ) { LightmapData[] lightmapData = new LightmapData[2]; for(int i = 0 ; i < 2 ; i++ ) lightmapData[i] = new LightmapData(); for( int i = 0 ; i < 2 ; i++ ) { Debug.Log("scene12/LightmapFar-" + i.ToString() ); lightmapData[i].lightmapFar = Resources.Load( "scene12/LightmapFar-" + i.ToString(), typeof(Texture2D)) as Texture2D; } LightmapSettings.lightmaps = lightmapData; }
Hello liszto I still not able to do this . $$anonymous$$y exact requirement is I want a code that somehow disable or hide the lightmap for few seconds after that it will be applied back. I have a terrain full of trees. I have generated a lightmap in 5 $$anonymous$$utes for an ios game. But I need some script that can cancel lightmap for few seconds and after then again enable the light map. Need Help Urgently
Answer by liszto · Dec 06, 2012 at 07:39 AM
So first of all, move your folder from "Assets" to "Resources".
When this is done, your script must work better :).
Then, if you want disable lightmap during a short period, you can for example do something like this :
i am sending you my simple project where I am trying to implement this but its not working . Please download it and Check where I am doing wrong. http://www.fileconvoy.com/dfl.php?id=g5afac46c9229e952999178090a1353bc08ec060fc
Please Open Scene 1 and Script nbh.cs
So accourding to you . We have to replace our lightmap if we want to disable it. So If our terrain is large in size and its Lightmap is arround 50 mb so This process will take a lot of time to switch lightmap. So is there any way so that we can just turn the lightmap on/off.
I answer you at 1.00pm CET+2 I'm at the offices right now. But I promise I check this ;)
I can't grab your file :/. It seems not be on the server :(
Your answer
Follow this Question
Related Questions
Lightmapping AND real time shadows/lights? 1 Answer
Weird Artifacts in my game lightmaps 1 Answer
Progressive light mapping is acting funky and making my models eyes too bright in certain scenarios 1 Answer
Baked Lightmaps never finish baking in Unity - what is a minimum "test bake" light setting? 0 Answers
Can't edit lightmaps. 0 Answers