Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Disable baked lightmaps at runtime on mobiles

Discussion in 'Scripting' started by Durins-Bane, Jun 30, 2016.

  1. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Is it possible to turn off baked light maps for a scene while its being played?

    I want my game to be able to run on pretty much any device and up to now its working pretty well :D

    The phone I use for testing is right on the bottom end of the scale and without any light maps/realtime lighting it just about runs 25fps+ and is playable (most of the time depending on how many mobs are spawned)

    For people with better phones though that run my game easily with high fps I want to make it look a little bit nicer and more pleasing for them rather than unrealistic lighting and no shadows.
    I have settings so that they can change the quality in game which works perfectly.

    Is it possible to load my game and then enable/disable the light maps of a scene on mobile and have it affect performance depending on which quality setting are selected?

    I found this post from 2011/13
    http://forum.unity3d.com/threads/turn-use-lightmaps-off-via-script.75794/#post-485421

    Will this work with Unity5? Has anyone done this recently?
    Does this effect performance on mobiles rather than just hiding them?

    Im going to test it out tomorrow when I have time just wondering if anyone has tried it before on mobiles.
     
  2. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    If anyone is interested in trying to do this here is some code I found that works pretty well


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class lightmapData : MonoBehaviour {
    5.    
    6.     //reference to existing scene lightmap data.
    7.     LightmapData[] lightmap_data;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         // Save reference to existing scene lightmap data.
    12.         lightmap_data = LightmapSettings.lightmaps;}
    13.  
    14.     public void disableLightmaps(){
    15.         // Disable lightmaps in scene by removing the lightmap data references
    16.         LightmapSettings.lightmaps = new LightmapData[]{};}
    17.    
    18.     public void enableLightmaps(){
    19.         // Reenable lightmap data in scene.
    20.         LightmapSettings.lightmaps = lightmap_data;}}
    21.  
     
    Lisk, ddaleymont and Marcos-Elias like this.
  3. torreb1990

    torreb1990

    Joined:
    Apr 5, 2018
    Posts:
    1
    Thank you show much. I lost a lot of time looking for a solution and this one work perfectly :D
     
    Lisk likes this.
  4. secondimpactgames

    secondimpactgames

    Joined:
    Feb 24, 2014
    Posts:
    1
    This thread comes up top when googling to disable lightmaps in code - for anyone arriving here from a search like I did: the above approach no longer works. If you replace the lightmaps with an empty array Unity will pass a default (grey) texture to materials instead. This means that instead of turning off the baked lighting, you're swapping it for a uniformly lit version.

    I got around this by building a copy of the original lightmap data array, and replacing the lightmapColor texture on each LightmapData object with a black texture. As follows:

    Code (CSharp):
    1. LightmapData[] turnedOffLightmaps = new LightmapData[originalLightmaps.Length];
    2.  
    3. for(int i = 0; i < turnedOffLightmaps.Length; i++)
    4. {
    5.     var thisOriginalLightmap = originalLightmaps[i];
    6.     var thisTurnedOffLightmap = new LightmapData();
    7.  
    8.      thisTurnedOffLightmap.lightmapDir = thisOriginalLightmap.lightmapDir;
    9.     thisTurnedOffLightmap.shadowMask = thisOriginalLightmap.shadowMask;
    10.     thisTurnedOffLightmap.lightmapColor = blackTexture;
    11.  
    12.     turnedOffLightmaps[i] = thisTurnedOffLightmap;
    13. }
    14.  
    15. LightmapSettings.lightmaps = turnedOffLightmaps;
     
    th3z0d1ac likes this.
  5. th3z0d1ac

    th3z0d1ac

    Joined:
    May 14, 2014
    Posts:
    11
    Thanks for the suggestion/solution, really appreciate it!