From 175cc68e84de8362898f54a7434385d567590d4f Mon Sep 17 00:00:00 2001 From: liutao <821580467@qq.com> Date: Tue, 16 Sep 2025 13:05:02 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=B7=A5=E5=85=B7=E3=80=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=87=AA=E5=8A=A8=E6=9B=BF=E6=8D=A2UI=E6=9D=90?= =?UTF-8?q?=E8=B4=A8=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/Editor/UIMaterialReplacer.meta | 3 + .../Editor/UIMaterialReplacer/UIReplacer.cs | 105 ++++++++++++++++++ .../UIMaterialReplacer/UIReplacer.cs.meta | 3 + 3 files changed, 111 insertions(+) create mode 100644 ProjectNLD/Assets/Editor/UIMaterialReplacer.meta create mode 100644 ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs create mode 100644 ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs.meta diff --git a/ProjectNLD/Assets/Editor/UIMaterialReplacer.meta b/ProjectNLD/Assets/Editor/UIMaterialReplacer.meta new file mode 100644 index 00000000000..26cf16d1761 --- /dev/null +++ b/ProjectNLD/Assets/Editor/UIMaterialReplacer.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 15b106af891a4e44ae742657a36267f2 +timeCreated: 1757998413 \ No newline at end of file diff --git a/ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs b/ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs new file mode 100644 index 00000000000..305d5de4ece --- /dev/null +++ b/ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs @@ -0,0 +1,105 @@ +using UnityEditor; +using UnityEngine; +namespace GameEditor.UIMaterialReplacer +{ + public class UIReplacer + { + + private static string _matPath = "Assets/Art_Out/UI/Material/gama_mat.mat"; + + [MenuItem("Assets/Editor/UIMaterialReplacer/Replace UI Material")] + [MenuItem("GameObject/Editor/UIMaterialReplacer/Replace UI Material", false, 10)] + public static void ReplaceUI() + { + // 获取选中的所有GameObject + var selectedObjects = Selection.GetFiltered(SelectionMode.DeepAssets); + foreach (var selectedObject in selectedObjects) + { + _TryReplace(selectedObject); + } + } + + private static bool HasTransparency(Texture2D texture) + { + // 方法1: 检查纹理格式 + var format = texture.format; + bool formatSupportsAlpha = format == TextureFormat.RGBA32 || + format == TextureFormat.ARGB32 || + format == TextureFormat.RGBA4444 || + format == TextureFormat.ARGB4444 || + format == TextureFormat.Alpha8 || + format == TextureFormat.ETC2_RGBA8 || + format == TextureFormat.DXT5 || + format == TextureFormat.BC7; + + if (!formatSupportsAlpha) return false; + + // 方法2: 检查像素数据(需要纹理可读) + if (!texture.isReadable) return true; + + try + { + var pixels = texture.GetPixels32(); + foreach (var pixel in pixels) + { + if (pixel.a < 255) // 发现非完全不透明的像素 + { + return true; + } + } + return false; + } + catch + { + // 无法读取像素数据,回退到格式检查 + return true; + } + } + + private static void _TryReplace(GameObject go) + { + // 替换材质 + var loadMat = AssetDatabase.LoadAssetAtPath(_matPath); + if (loadMat == null) + { + Debug.LogError("加载材质失败: " + _matPath); + return; + } + + Debug.Log($"开始处理 {go.name}"); + var isModify = false; + var allImages = go.GetComponentsInChildren(true); + foreach (var img in allImages) + { + if (img.sprite == null) + { + continue; + } + // 判断sprite是否包含透明信息 + var texture = img.sprite.texture; + if (texture == null) + { + continue; + } + + if (!HasTransparency(texture)) + { + continue; + } + + img.material = loadMat; + Debug.Log($"替换材质: {img.gameObject.name} 贴图: {texture.name}"); + isModify = true; + } + + if (isModify) + { + EditorUtility.SetDirty(go); + AssetDatabase.SaveAssets(); + } + + Debug.Log($"处理完成 {go.name}"); + } + + } +} diff --git a/ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs.meta b/ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs.meta new file mode 100644 index 00000000000..829416b03a0 --- /dev/null +++ b/ProjectNLD/Assets/Editor/UIMaterialReplacer/UIReplacer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cdd2e0e231984f3fb0283ea9475f0ff6 +timeCreated: 1757998429 \ No newline at end of file