private static int addLightmap(string scenePath, string resourcePath, int originalLightmapIndex, Texture2D lightmapFar, Texture2D lightmapNear) { for (int i = 0; i < sceneLightmaps.Count; i++) { if (sceneLightmaps[i].OriginalLightmapIndex == originalLightmapIndex) { return(i); } } RemapTexture2D remapTex = new RemapTexture2D(); remapTex.OriginalLightmapIndex = originalLightmapIndex; remapTex.OrginalLightmap = lightmapFar; string fileName = scenePath + "Lightmap-" + originalLightmapIndex; remapTex.LightmapFar = getLightmapAsset(fileName + "_comp_light.exr", resourcePath + "_light", originalLightmapIndex); if (lightmapNear != null) { remapTex.LightmapNear = getLightmapAsset(fileName + "_comp_dir.exr", resourcePath + "_dir", originalLightmapIndex); } sceneLightmaps.Add(remapTex); return(sceneLightmaps.Count - 1); }
private static int addLightmap(string scenePath, string resourcePath, int originalLightmapIndex, Texture2D lightmapFar, Texture2D lightmapNear) { for (int i = 0; i < sceneLightmaps.Count; i++) { if (sceneLightmaps[i].OriginalLightmapIndex == originalLightmapIndex) { return(i); } } RemapTexture2D remapTex = new RemapTexture2D(); remapTex.OriginalLightmapIndex = originalLightmapIndex; remapTex.OrginalLightmap = lightmapFar; string fileName = scenePath; remapTex.LightmapFar = getLightmapAsset(fileName + lightmapFar.name, resourcePath + "-" + originalLightmapIndex.ToString(), false); if (lightmapNear != null) { remapTex.LightmapNear = getLightmapAsset(fileName + lightmapNear.name, resourcePath + "-" + originalLightmapIndex.ToString(), true); } sceneLightmaps.Add(remapTex); return(sceneLightmaps.Count - 1); }
//according to the current platform, update all the lightmap that assets refrence private static void UpdateSceneLightmap() { PrefabLightmapData[] pldArr = GameObject.FindObjectsOfType <PrefabLightmapData>(); if (pldArr == null || pldArr.Length <= 0) { EditorUtility.DisplayDialog("提示", "没有找到必要的脚本PrefabLightmapData,请检查场景", "OK"); return; } // Lightmapping.Bake(); sceneLightmaps.Clear(); string path = Path.Combine(Application.dataPath, LightMapsDir); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } Scene curScene = EditorSceneManager.GetActiveScene(); sceneName = Path.GetFileNameWithoutExtension(curScene.name); //#if UNITY_ANDROID // afterFix = "_ANDROID"; //#elif UNITY_STANDALONE_WIN // afterFix = "_WIN"; //#elif UNITY_IOS // afterFix = "_IOS"; //#endif string resourcesPath = Path.GetDirectoryName(curScene.path) + "/" + sceneName + afterFix + "_lightmap/"; //if (!Directory.Exists(Path.GetDirectoryName(curScene.path) + "/" + sceneName + afterFix + "_lightmap")) //{ // EditorUtility.DisplayDialog("提示", "没有相关平台的lightmap", "OK"); // return; //} List <string> farNames = new List <string>(); List <string> nearNames = new List <string>(); foreach (PrefabLightmapData pld in pldArr) { GameObject gObj = pld.gameObject; List <RendererInfo> renderers = new List <RendererInfo>(); List <Texture2D> lightmapFars = new List <Texture2D>(); List <Texture2D> lightmapNears = new List <Texture2D>(); RemapTexture2D remapTex = new RemapTexture2D(); //remapTex.OriginalLightmapIndex = originalLightmapIndex; //remapTex.OrginalLightmap = lightmapFar; var originFars = pld.lightmapFars; var originNears = pld.lightmapNears; if (originFars != null && originFars.Length > 0) { foreach (var tex in originFars) { if (tex == null) { continue; } string fullpath = AssetDatabase.GetAssetPath(tex); int lastVal = fullpath.LastIndexOf("_ANDROID"); if (lastVal > 0) { fullpath = fullpath.Replace("_ANDROID", afterFix); } else if (fullpath.LastIndexOf("_IOS") > 0) { fullpath = fullpath.Replace("_IOS", afterFix); } else { fullpath = fullpath.Replace("_WIN", afterFix); } string fileName = fullpath; Debug.Log(fileName); if (farNames.Contains(fileName)) { continue; } else { farNames.Add(fileName); } remapTex.LightmapFar = UpdateLightmapAsset(fileName); lightmapFars.Add(remapTex.LightmapFar); } } if (originNears != null && originNears.Length > 0) { foreach (var tex in originNears) { if (tex == null) { continue; } string fullpath = AssetDatabase.GetAssetPath(tex); int lastVal = fullpath.LastIndexOf("_ANDROID"); if (lastVal > 0) { fullpath = fullpath.Replace("_ANDROID", afterFix); } else if (fullpath.LastIndexOf("_IOS") > 0) { fullpath = fullpath.Replace("_IOS", afterFix); } else { fullpath = fullpath.Replace("_WIN", afterFix); } string fileName = fullpath; if (nearNames.Contains(fileName)) { continue; } else { nearNames.Add(fileName); } remapTex.LightmapNear = UpdateLightmapAsset(fileName); lightmapNears.Add(remapTex.LightmapNear); } } sceneLightmaps.Add(remapTex); //pld.mRendererInfos = renderers.ToArray(); pld.lightmapFars = lightmapFars.ToArray(); pld.lightmapNears = lightmapNears.ToArray(); GameObject targetPrefab = PrefabUtility.GetPrefabParent(gObj) as GameObject; if (targetPrefab != null) { //自定义存放的路径 PrefabUtility.ReplacePrefab(gObj, targetPrefab); } else { //默认路径 // string prefabPath = Path.GetDirectoryName(curScene.path) + "/" + sceneName + ".prefab"; string prefabPath = Path.GetDirectoryName(curScene.path) + "/" + sceneName + "/" + gObj.name + ".prefab"; PrefabUtility.CreatePrefab(prefabPath, gObj, ReplacePrefabOptions.ConnectToPrefab); } //改变当前场景中的光照贴图信息 PrefabLightmapData.ApplyLightmaps(pld.rendererInfos, pld.lightmapFars, pld.lightmapNears); } Debug.Log("*******************update finish*****************************"); }