Search Unity

Turn "Use Lightmaps" off via script?

Discussion in 'Scripting' started by nm8shun, Jan 28, 2011.

  1. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    I've got a scene where I am using a flashlight to see for a while, but then the lights are flipped on. I've got Beast-generated lightmaps baked (which look great); but of course, I only want to see those when the lights have been turned on.

    Is there a way (via script) to turn all the lightmaps on/off in game?

    Thanks
     
  2. dup

    dup

    Joined:
    Mar 3, 2010
    Posts:
    36
  3. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Thanks for your response. I had actually read through that but (as is the case for a lot of Unity's scripting documentation), it was just a bit opaque - very difficult to figure out how to use exactly.

    Here's the solutions I currently am working with:
    Code (csharp):
    1.  
    2. var level : GameObject;
    3. var originalLightMapIndex : int;
    4.  
    5. function Start(){
    6.     originalLightMapIndex = level.renderer.lightmapIndex;
    7.     Debug.Log (level.renderer.lightmapIndex);
    8.     yield WaitForSeconds (3);
    9.    
    10.     level.renderer.lightmapIndex =-1;
    11.     Debug.Log (level.renderer.lightmapIndex);
    12.    
    13.     yield WaitForSeconds (3);
    14.     level.renderer.lightmapIndex = originalLightMapIndex;
    15.     Debug.Log (level.renderer.lightmapIndex);
    16. }
    17.        
    18.    
    The debug logs were just to help me try and understand what's going on there.

    Anyway, this works fine for that one object (level). But if I have 100 objects, this is really tough. Is there a way to globally switch the Use Lightmaps on and off?
     
    Last edited: Jan 28, 2011
  4. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    OK, so I'm able to turn all the lightmaps off using this:

    Code (csharp):
    1.  
    2. function TurnLightMapsOff (){
    3.     var children = level.GetComponentsInChildren (Renderer);
    4.     for (var child: Renderer in children){
    5.         originalLightMapIndex = child.renderer.lightmapIndex;
    6.         Debug.Log (child.renderer.lightmapIndex);
    7.         child.renderer.lightmapIndex = -1;
    8.     }
    9. }
    But I'm still unable to turn them all back on. It seems that since there is a global "Use Lightmaps" in the Editor, it should be accessible in the Scripting; am I missing something?
     
    Last edited: Jan 31, 2011
  5. fenderrio

    fenderrio

    Joined:
    Mar 2, 2013
    Posts:
    147
    I was looking at how to toggle lightmaps on/off recently, and found a solution which works.
    The following code will disable the lightmaps, and then reapply them.

    Code (csharp):
    1.  
    2. // Save reference to existing scene lightmap data.
    3. LightmapData[] lightmap_data = LightmapSettings.lightmaps;
    4.  
    5. // Disable lightmaps in scene by removing the lightmap data references
    6. LightmapSettings.lightmaps = new LightmapData[]{};
    7.  
    8. // Reenable lightmap data in scene.
    9. LightmapSettings.lightmaps = lightmap_data;
    10.  
     
  6. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    sry, little offtopic. Just because i'm curious :)

    Is it possible to have two (or more) lightmaps and toggle them via scripting?
     
  7. allenwp

    allenwp

    Joined:
    Sep 4, 2013
    Posts:
    46
    chelnok likes this.