Created
July 21, 2024 16:25
-
-
Save thelebaron/4fdc9737be0b9fd9ad80dc157c848c71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEngine.SceneManagement; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace Junk.Hotfixes | |
{ | |
/// <summary> | |
/// Bug with Unity 6000.0.11f1 which incorrectly sets the wrong texturetype | |
/// </summary> | |
[InitializeOnLoad] | |
public class LightmappingTextureType | |
{ | |
static LightmappingTextureType() | |
{ | |
Lightmapping.bakeCompleted += OnLightmappingCompleted; | |
} | |
private static void OnLightmappingCompleted() | |
{ | |
var lightmapPaths = GetLightmapPathsForOpenScenes(); | |
Debug.Log("Lightmapping completed. New lightmap textures:"); | |
foreach (var path in lightmapPaths) | |
{ | |
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter; | |
if (importer != null) | |
{ | |
if (IsDirectionalLightmap(path) && importer.textureType != TextureImporterType.DirectionalLightmap) | |
{ | |
Debug.Log($"{path} (Type: {importer.textureType}) - Expected DirectionalLightmap and set to DirectionalLightmap"); | |
importer.textureType = TextureImporterType.DirectionalLightmap; | |
importer.SaveAndReimport(); | |
} | |
else if (!IsDirectionalLightmap(path) && importer.textureType != TextureImporterType.Lightmap) | |
{ | |
Debug.Log($"{path} (Type: {importer.textureType}) - Expected Lightmap and set to Lightmap"); | |
importer.textureType = TextureImporterType.Lightmap; | |
importer.SaveAndReimport(); | |
} | |
} | |
} | |
AssetDatabase.Refresh(); | |
} | |
private static string[] GetLightmapPathsForOpenScenes() | |
{ | |
// Get the names of all open scenes | |
var openScenes = Enumerable.Range(0, SceneManager.sceneCount) | |
.Select(i => SceneManager.GetSceneAt(i).name) | |
.ToList(); | |
// Get all texture assets in the project | |
string[] guids = AssetDatabase.FindAssets("t:Texture2D", new[] { "Assets" }); | |
return guids | |
.Select(guid => AssetDatabase.GUIDToAssetPath(guid)) | |
.Where(path => IsLightmapTexture(path, openScenes)) | |
.ToArray(); | |
} | |
private static bool IsLightmapTexture(string path, System.Collections.Generic.List<string> openScenes) | |
{ | |
string fileName = Path.GetFileNameWithoutExtension(path); | |
// Check if the texture name matches the Lightmap-x, Lightmap-xx, or Lightmap-xxxxxx convention | |
if (Regex.IsMatch(fileName, @"^Lightmap-\d+(_comp_light|_comp_dir)?$")) | |
{ | |
// Ensure the texture is used in one of the open scenes | |
foreach (var sceneName in openScenes) | |
{ | |
if (path.Contains(sceneName)) | |
{ | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
private static bool IsDirectionalLightmap(string path) | |
{ | |
string fileName = Path.GetFileNameWithoutExtension(path); | |
// Check if the texture name contains _comp_dir | |
return fileName.Contains("_comp_dir"); | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment