publicvoidStart () { if(Application.isPlaying) { LightmapSettings.lightmapsMode = mode; int l1 = (lightmapFar == null) ? 0 : lightmapFar.Length; int l2 = (lightmapNear == null) ? 0 : lightmapNear.Length; int l = (l1 < l2) ? l2 : l1;
LightmapData[] lightmaps = null; if (l > 0) { lightmaps = new LightmapData[l]; for (int i = 0; i < l; i++) { lightmaps[i] = new LightmapData(); if (i < l1) lightmaps[i].lightmapFar = lightmapFar[i]; if (i < l2) lightmaps[i].lightmapNear = lightmapNear[i]; } } LightmapSettings.lightmaps = lightmaps;
if (LightmapSettings.lightmaps != null && LightmapSettings.lightmaps.Length > 0) { int l = LightmapSettings.lightmaps.Length; lightmapFar = new Texture2D[l]; lightmapNear = new Texture2D[l]; for (int i = 0; i < l; i++) { lightmapFar[i] = LightmapSettings.lightmaps[i].lightmapFar; lightmapNear[i] = LightmapSettings.lightmaps[i].lightmapNear; } }
MeshLightmapSetting[] savers = GameObject.FindObjectsOfType<MeshLightmapSetting>(); foreach(MeshLightmapSetting s in savers) { s.SaveSettings(); } } #endif }
ps. 这里额外提一个坑的地方:一开始我想避免使用MonoBehavior这种比较笨重的东西来保存数据,然后参考了Unity官方博客里的ScriptableObject。结果尝试了一下午发现博客中的代码压根不起作用,感觉就是从头就写错了…