Unity clone 场景渲染的灯光贴图异位问题
问题
需要将一个场景clone 一份保存到本地
当克隆完成后,副本场景的灯光贴图异位了,与原场景存在较大的差别
问题原因
场景被clone 后,场景的灯光渲染数据不能共用,即Lightmapping.lightingSettings
两场景可以公用,但 Lightmapping.lightingDataAsset
数据不能共用。
解决办法
场景被clone 后,重新调用unity 的Bake方法,对新场景重新灯光烘焙。
附上相关代码:
EditorSceneManager.SaveScene(scene, newPath, false);
//bake new copy scene, when clone a scene, the reference of the light data will be broken.
if (Lightmapping.lightingSettings != null && Lightmapping.lightingDataAsset != null)
{
//EditorSceneManager.OpenScene(scene.path, OpenSceneMode.Single);
var sourceLightingSettingPath = AssetDatabase.GetAssetPath(Lightmapping.lightingSettings);
var newLightingsettingPath = Path.Combine(Path.GetDirectoryName(newPath), Path.GetFileNameWithoutExtension(newPath) + ".lighting");
File.Copy(sourceLightingSettingPath, newLightingsettingPath, true);
AssetDatabase.ImportAsset(newLightingsettingPath);
var lightingSetting = AssetDatabase.LoadAssetAtPath<LightingSettings>(newLightingsettingPath);
EditorSceneManager.OpenScene(newPath, OpenSceneMode.Single);
Lightmapping.lightingSettings = lightingSetting;
Lightmapping.lightingSettings.autoGenerate = false;
Lightmapping.Bake();
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), newPath);
}
```