Unity 5 lightmapped scene assetbundle problem

Hey there,

i am having troubles loading an scene-assetbundle with lightmaps. Everything else works (loading bundles), but i can’t find a solution to see lightmaps on my objects. I am building one assetbundle per scene, and load it with Scenemanager.loadAsync.

This is what it should look like (and how it looks if i load the scene the normal way, without assetbundles):

and this is what i get if i load my assetbundle:

the lightmaps somehow seem to be bundled in the assetbundle, if i load the bundle-scene in the editor and look at the lightmap-settings, i see the lightmaps are there - Unity just does not apply them to the objects.

Is this a bug or am i doing something wrong/do i need to do anything “special” to apply the lightmaps?
Thanks in advance!

Using Unity 5.6.5p4 (64-bit)

Edit → Project Settings → Graphics → Shader Stripping → Lightmap Modes → Set them to MANUAL and select the modes you use.
Worked for us!

Credits go to:

After a lot of tests and googeling around i found a working solution, although i am not totally happy with it:

If you store the lightmaps and the LightmapSettings for each renderer in a custom script, you can load the lightmaps on runtime. Look at this forum post and the sample project provided there, this is how i got it to work:

https://forum.unity3d.com/threads/problems-with-instantiating-baked-prefabs.324514/

The “bad” thing is, that now i need to have a duplicate of the lightmaps stored in my custom script - whenever i try to assign the lightmaps which are “in the scene” (and also after loading the assetbundle, at least in the editor i can see them), they somehow are not beeing loaded correcty by the renderers and i get wrong lightmap positions. So i have to store the lightmaps as extra texture and add them to the lightmap array to use them.

EDIT:
The problems with the wrong index were just some indices i messed up - now i got it working the way i want it to, without duplicate Lightmaps. Here is the working script, just place it on the parent object of whatever is lightmapped in your scene you want to use as an assetbundle:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SceneAssetBundleLightmaps : MonoBehaviour{
	[System.Serializable]
	struct RendererInfo{
		public Renderer 	renderer;
		public int 			lightmapIndex;
		public Vector4 		lightmapOffsetScale;
	}

	[SerializeField]
	RendererInfo[]	m_RendererInfo;

	void Awake (){
		if (m_RendererInfo == null || m_RendererInfo.Length == 0)
			return;
		ApplyRendererInfo(m_RendererInfo);
	}
		
	static void ApplyRendererInfo (RendererInfo[] infos){
		for (int i=0;i<infos.Length;i++){
			var info = infos*;*
  •  	info.renderer.lightmapIndex = info.lightmapIndex;*
    
  •  	info.renderer.lightmapScaleOffset = info.lightmapOffsetScale;*
    
  •  }*
    
  • }*

#if UNITY_EDITOR

  • [UnityEditor.MenuItem(“Assets/Assign Scene Assetbundle Lightmaps”)]*

  • static void GenerateLightmapInfo (){*

  •  if (UnityEditor.Lightmapping.giWorkflowMode != UnityEditor.Lightmapping.GIWorkflowMode.OnDemand){*
    
  •  	Debug.LogError("ExtractLightmapData requires that you have baked you lightmaps and Auto mode is disabled.");*
    
  •  	return;*
    
  •  }*
    
  •  //UnityEditor.Lightmapping.Bake();*
    
  •  SceneAssetBundleLightmaps[] prefabs = FindObjectsOfType<SceneAssetBundleLightmaps>();*
    
  •  foreach (var instance in prefabs){*
    
  •  	var gameObject = instance.gameObject;*
    
  •  	var rendererInfos = new List<RendererInfo>();*
    
  •  	var lightmaps = new List<Texture2D>();*
    
  •  	GenerateLightmapInfo(gameObject, rendererInfos, lightmaps);*
    
  •  	instance.m_RendererInfo = rendererInfos.ToArray();*
    
  •  }*
    
  • }*

  • static void GenerateLightmapInfo (GameObject root, List rendererInfos, List lightmaps){*

  •  var renderers = root.GetComponentsInChildren<MeshRenderer>();*
    
  •  foreach (MeshRenderer renderer in renderers){*
    
  •  	if (renderer.lightmapIndex != -1)*
    
  •  	{*
    
  •  		RendererInfo info = new RendererInfo();*
    
  •  		info.renderer = renderer;*
    
  •  		info.lightmapOffsetScale = renderer.lightmapScaleOffset;*
    
  •  		info.lightmapIndex = renderer.lightmapIndex;*
    
  •  		rendererInfos.Add(info);*
    
  •  	}*
    
  •  }*
    
  • }*
    #endif

}