载入lightmap
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PrefabLightmapData : MonoBehaviour
{
[System.Serializable]
struct RendererInfo
{
public Renderer renderer;
public int lightmapIndex;
public Vector4 lightmapOffsetScale;
}
[UnityEngine.SerializeField]
Texture2D[] lightmapTexs; //当前场景的灯光贴图
[UnityEngine.SerializeField]
Texture2D[] lightmapDir; //当前场景的灯光贴图
[UnityEngine.SerializeField]
Texture2D[] lightmapMask; //当前场景的灯光贴图
[UnityEngine.SerializeField]
RendererInfo[] rendererList;
[UnityEngine.SerializeField]
Material skyBoxMaterial;
#if UNITY_EDITOR
public void SaveLightmap()
{
Renderer[] renders = GetComponentsInChildren<Renderer>(true);
RendererInfo rendererInfo;
rendererList = new RendererInfo[renders.Length];
int index = 0;
for (int r = 0, rLength = renders.Length; r < rLength; ++r)
{
if (renders[r].gameObject.isStatic == false) continue;
rendererInfo.renderer = renders[r];
rendererInfo.lightmapIndex = renders[r].lightmapIndex;
rendererInfo.lightmapOffsetScale = renders[r].lightmapScaleOffset;
rendererList[index] = rendererInfo;
++index;
}
//序列化光照贴图
LightmapData[] ldata = LightmapSettings.lightmaps;
lightmapTexs = new Texture2D[ldata.Length];
lightmapDir = new Texture2D[ldata.Length];
lightmapMask = new Texture2D[ldata.Length];
for (int t = 0, tLength = ldata.Length; t < tLength; ++t)
{
lightmapTexs[t] = ldata[t].lightmapColor;
lightmapDir[t] = ldata[t].lightmapDir;
lightmapMask[t] = ldata[t].shadowMask;
}
skyBoxMaterial = RenderSettings.skybox;
}
#endif
private void Awake()
{
this.LoadLightmap();
this.ChangeSkybox(skyBoxMaterial);
}
void OnEnable()
{
this.LoadLightmap();
this.ChangeSkybox(skyBoxMaterial);
}
void LoadLightmap()
{
if (null == rendererList || rendererList.Length == 0)
{
Debug.Log(gameObject.name + " 的 光照信息为空");
return;
}
Renderer[] renders = GetComponentsInChildren<Renderer>(true);
for (int r = 0, rLength = renders.Length,index=0; r < rLength;r++ )
{
if(index < rendererList.Length && renders[r].gameObject.name != rendererList[index].renderer.gameObject.name)
{
Debug.LogError("lightMap烘焙时,物体 " + renders[r].gameObject.name + " 未勾选static。");
break;
}
if (renders[r].gameObject.name.Contains("combineMesh_"))
{
renders[r].lightmapIndex = 0;
continue;
}
renders[r].lightmapIndex = rendererList[index].lightmapIndex;
renders[r].lightmapScaleOffset = rendererList[index].lightmapOffsetScale;
++index;
}
if (null == lightmapTexs || lightmapTexs.Length == 0)
{
return;
}
LightmapSettings.lightmapsMode = LightmapsMode.NonDirectional;
LightmapData[] ldata = new LightmapData[lightmapTexs.Length];
LightmapSettings.lightmaps = null;
for (int t = 0, tLength = lightmapTexs.Length; t < tLength; ++t)
{
ldata[t] = new LightmapData();
ldata[t].lightmapColor = lightmapTexs[t];
ldata[t].lightmapDir = lightmapDir[t];
ldata[t].shadowMask = lightmapMask[t];
}
LightmapSettings.lightmaps = ldata;
MeshRenderer[] meshRenderers = GetComponentsInChildren<MeshRenderer>();
GameObject[] gos = new GameObject[meshRenderers.Length];
for (int i = 0; i < meshRenderers.Length; i++)
{
if (meshRenderers[i].gameObject.name.Contains("combineMesh_"))
{
renders[i].lightmapIndex = 0;
continue;
}
gos[i] = meshRenderers[i].gameObject;
}
StaticBatchingUtility.Combine(gos, gameObject);
}
private void ChangeSkybox(Material newSkybox)
{
RenderSettings.skybox = newSkybox;
}
}