Quantcast
Channel: Answers for "How the heck can I include Lightmapping data in unity 5 asset bundles???"
Viewing all articles
Browse latest Browse all 6

Answer by eyalfx

$
0
0
I won't be able to try the new version since I'm heading out to vacation for a week and NOT taking my laptop :) But I did get it to work with the following scripts: First I made a new class that save the index and coordinates of the lightmap. public class LightmapParam : MonoBehaviour { public int lightmapIndex; public Vector4 lightmapScaleOffset; } I build the asset using this script, making sure to add a LIghtmapParam to each object that has a renderer. And manually adding the lightmaps to the assetBundle private void BuildAssetBundle(){ GameObject assetParent = new GameObject(); assetParent.name = assetName; GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType() ; foreach(GameObject obj in allObjects){ if(assetParent != obj){ obj.transform.SetParent(assetParent.transform, true); Renderer r = obj.GetComponent(); if(r != null){ LightmapParam lmp = obj.GetComponent(); if(lmp == null) lmp = obj.AddComponent(); lmp.lightmapIndex = r.lightmapIndex; lmp.lightmapScaleOffset = r.lightmapScaleOffset; } } } string path = "Assets/AssetPrefabs/" + assetName + ".prefab"; PrefabUtility.CreatePrefab(path, assetParent, ReplacePrefabOptions.ReplaceNameBased); Debug.Log("prefab " + path); //AssetBundleBuild[] abb = new AssetBundleBuild[1]; //abb[0].assetBundleName = assetName; //abb[0].assetNames = new string[]{ path}; //BuildPipeline.BuildAssetBundles ("Assets/AssetBundles", abb); string curScene = EditorApplication.currentScene; string[] parts = curScene.Split('/', '\\'); string sceneName = parts[parts.Length - 1].Split('.')[0]; string lightmapPath = Path.GetDirectoryName(curScene) + "/" + sceneName + "/"; List assetList = new List(); for(int i = 0; i < LightmapSettings.lightmaps.Length; i++){ LightmapData lmd = LightmapSettings.lightmaps[i]; string lmp = lightmapPath + "Lightmap-" + i + "_comp_light.exr"; assetList.Add(lmp ); Debug.Log(lmp + " " + File.Exists(lmp)); } Debug.Log(assetList[0] + " " + assetList.Count); //string path = "Assets/AssetPrefabs/" + assetName + ".prefab"; assetList.Add(path); AssetBundleBuild[] abb = new AssetBundleBuild[1]; // *************************************** lightmaps 2 abb[0].assetNames = assetList.ToArray();// new string[]{ path}; // create asset bundle from this prefab // build mac Debug.Log("make osx bundle"); abb[0].assetBundleName = assetName + "_osx.unity3d"; BuildPipeline.BuildAssetBundles ("Assets/AssetBundles", abb, BuildAssetBundleOptions.None, BuildTarget.StandaloneOSXUniversal); } Then when I'm loading the assetBundle I'm looking for the lightmapParam and assigning the values. and assigning the actual maps to the lightmapSettings. AssetBundle bundle = www.assetBundle; Object[] objects = bundle.LoadAllAssets(); List lmdList = new List(); foreach(Object obj in objects){ if (obj.GetType () == typeof(Texture2D)) { if (obj.name.Contains ("Lightmap")) { LightmapData lmd = new LightmapData(); Texture2D tex = obj as Texture2D; lmd.lightmapFar = tex; lmdList.Add(lmd); } } } LightmapSettings.lightmaps = lmdList.ToArray(); currentScene = Instantiate(bundle.LoadAsset(currentAsset.objectId)) as GameObject; Renderer[] rndrs = GameObject.FindObjectsOfType(); foreach(Renderer rnd in rndrs){ LightmapParam lmp = rnd.GetComponent(); if(lmp != null){ //Debug.Log(rnd.name + " " + lmp.lightmapIndex + " " + lmp.lightmapScaleOffset); rnd.lightmapIndex = lmp.lightmapIndex; rnd.lightmapScaleOffset = lmp.lightmapScaleOffset; rnd.gameObject.isStatic = true; } } Hope this works for you as well. The one gatcha that I found is that the lightmaps coming in from the bundle are not tagged Lightmaps, and I couldn't find a way to change that , so not all shaders work with it. I use legacy>lightmap>diffuse and it works Cheers

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>