Unity Baked Lightmap on new prefab. Solved!
VRx NextGen Institution > Unity Baked Lightmap on new prefab. Solved!
LightMapScript
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LightMapScript : MonoBehaviour
{
public bool FillData;
void OnDrawGizmos()
{
if (FillData)
{
Tiling[] Script=GameObject.FindObjectsOfType<Tiling>();
List<RendererInfo> rendererInfos = new List<RendererInfo>();
for(int i=0;i<Script.Length;i++)
{
RendererInfo info = new RendererInfo();
info.renderer = Script[i].gameObject.GetComponent<Renderer>();
info.lightmapOffsetScale = Script[i].LightMapOffset;
info.lightmapIndex =Script[i].LightMapIndexNumber;
rendererInfos.Add(info);
}
J_renderer = rendererInfos.ToArray();
FillData=false;
}
}
[System.Serializable]
struct RendererInfo
{
public Renderer renderer;
public int lightmapIndex;
public Vector4 lightmapOffsetScale;
}
[SerializeField]
RendererInfo[] J_renderer;
[SerializeField]
Texture2D[] J_Lightmap;
void Awake ()
{
if (J_renderer == null || J_renderer.Length == 0)
return;
var lightmaps = LightmapSettings.lightmaps;
var combinedLM = new LightmapData[lightmaps.Length + J_Lightmap.Length];
lightmaps.CopyTo(combinedLM, 0);
for (int i = 0; i < J_Lightmap.Length;i++)
{
combinedLM[i+lightmaps.Length] = new LightmapData();
combinedLM[i+lightmaps.Length].lightmapColor = J_Lightmap[i];
}
ApplyLights(J_renderer, lightmaps.Length);
LightmapSettings.lightmaps = combinedLM;
}
static void ApplyLights (RendererInfo[] infos, int lightmapOffsetIndex)
{
for (int i=0;i<infos.Length;i++)
{
var info = infos[i];
info.renderer.lightmapIndex = info.lightmapIndex + lightmapOffsetIndex;
info.renderer.lightmapScaleOffset = info.lightmapOffsetScale;
}
}
}
Tiling
using UnityEngine;
using System.Collections;
publicclassTiling : MonoBehaviour
{
//attach this script to all child that has renderer
//after your Data set than remove this script from all object
public int LightMapIndexNumber;
public Vector4 LightMapOffset;
void OnDrawGizmos()
{
LightMapIndexNumber = gameObject.GetComponent<Renderer> ().lightmapIndex;
LightMapOffset = gameObject.GetComponent<Renderer> ().lightmapScaleOffset;
}
}