From 4d29e0b33e1b9608a1de07feca3ad39dccb085a0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 8 Oct 2026 14:26:26 +0200 Subject: [PATCH] WireMines: Substance materials fall back to the project's plain materials of the same finish Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/Editor/WireMines.cs | 43 +++++++++++++++++++----------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/Editor/WireMines.cs b/Assets/Scripts/Editor/WireMines.cs index 8de62c218..2c1158fda 100644 --- a/Assets/Scripts/Editor/WireMines.cs +++ b/Assets/Scripts/Editor/WireMines.cs @@ -16,12 +16,14 @@ public static class WireMines private const long MeshFileId = -5495902117074765545; private const string BigExplosionGuid = "e1b08032bec16ba4eb1ae23cbd572664"; - private static readonly (string guid, long fileId)[] Materials = + // The Substance archives (.sbsar) import nothing in this Unity, so their materials fall back to the project's + // plain material of the same finish (red car paint, magenta car paint, and Steel for titanium). + private static readonly (string guid, long fileId, string fallback)[] Materials = { - ("504c7bfe0d50ff64a88b8eac2ceb46f8", 3720744386649352057), - ("a1d39ea984432bf44b4a0686afe2bf1a", 3720744386649352057), - ("e879c37f3520bf14a8a6075581faf7b2", 2100000), - ("58e16b7dbc1c31f4585e11544cdce127", 9178725544925813344), + ("504c7bfe0d50ff64a88b8eac2ceb46f8", 3720744386649352057, "Assets/Materials/Car Paint Red.mat"), + ("a1d39ea984432bf44b4a0686afe2bf1a", 3720744386649352057, "Assets/Materials/Car Paint Magenta.mat"), + ("e879c37f3520bf14a8a6075581faf7b2", 2100000, null), + ("58e16b7dbc1c31f4585e11544cdce127", 9178725544925813344, "Assets/Materials/Steel.mat"), }; [MenuItem("Aetheria/Wire Mines")] @@ -45,7 +47,7 @@ public static class WireMines { root.AddComponent().sharedMesh = Load(MeshGuid, MeshFileId); var meshRenderer = root.AddComponent(); - meshRenderer.sharedMaterials = Materials.Select(m => Load(m.guid, m.fileId)).ToArray(); + meshRenderer.sharedMaterials = Materials.Select(m => MaterialFor(m.guid, m.fileId, m.fallback)).ToArray(); var mine = root.AddComponent(); mine.MeshRenderer = meshRenderer; @@ -73,18 +75,29 @@ public static class WireMines } } + private static Material MaterialFor(string guid, long fileId, string fallback) + { + var path = AssetDatabase.GUIDToAssetPath(guid); + if (!string.IsNullOrEmpty(path)) + { + var loaded = AssetDatabase.LoadAllAssetsAtPath(path).OfType().FirstOrDefault(m => IsLocal(m, fileId)); + if (loaded != null) return loaded; + } + var substitute = fallback == null ? null : AssetDatabase.LoadAssetAtPath(fallback); + if (substitute == null) throw new System.InvalidOperationException($"No material for {guid} and no fallback loads"); + Debug.Log($"WireMines: {guid} did not load; using {fallback}"); + return substitute; + } + + private static bool IsLocal(Object asset, long fileId) => + AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out _, out long id) && id == fileId; + private static T Load(string guid, long fileId) where T : Object { var path = AssetDatabase.GUIDToAssetPath(guid); if (string.IsNullOrEmpty(path)) throw new System.InvalidOperationException($"No asset for guid {guid}"); - // A Substance archive's material is regenerated on import under another local id, so a path holding - // exactly one T is that T whatever its id. - var assets = AssetDatabase.LoadAllAssetsAtPath(path).OfType().ToArray(); - foreach (var asset in assets) - if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(asset, out _, out long id) && id == fileId) - return asset; - if (assets.Length == 1) return assets[0]; - throw new System.InvalidOperationException($"No {typeof(T).Name} with file id {fileId} in {path}; it holds " + - string.Join(", ", AssetDatabase.LoadAllAssetsAtPath(path).Select(a => $"{a.GetType().Name} {a.name}"))); + foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(path).OfType()) + if (IsLocal(asset, fileId)) return asset; + throw new System.InvalidOperationException($"No {typeof(T).Name} with file id {fileId} in {path}"); } }