2025-09-16 13:05:02 +08:00
|
|
|
|
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<UnityEngine.GameObject>(SelectionMode.DeepAssets);
|
|
|
|
|
|
foreach (var selectedObject in selectedObjects)
|
|
|
|
|
|
{
|
|
|
|
|
|
_TryReplace(selectedObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-16 13:50:51 +08:00
|
|
|
|
|
2025-09-16 13:05:02 +08:00
|
|
|
|
private static bool HasTransparency(Texture2D texture)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 方法2: 检查像素数据(需要纹理可读)
|
2025-09-16 13:50:51 +08:00
|
|
|
|
if (!texture.isReadable)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HasTransparencyFromFile(texture);
|
|
|
|
|
|
}
|
2025-09-16 13:05:02 +08:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var pixels = texture.GetPixels32();
|
|
|
|
|
|
foreach (var pixel in pixels)
|
|
|
|
|
|
{
|
2025-09-16 13:50:51 +08:00
|
|
|
|
if (pixel.a < 255 && pixel.a > 0) // 发现非完全不透明的像素
|
2025-09-16 13:05:02 +08:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// 无法读取像素数据,回退到格式检查
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-16 13:50:51 +08:00
|
|
|
|
|
|
|
|
|
|
private static bool HasTransparencyFromFile(Texture2D texture)
|
|
|
|
|
|
{
|
|
|
|
|
|
string assetPath = AssetDatabase.GetAssetPath(texture);
|
|
|
|
|
|
if (string.IsNullOrEmpty(assetPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
return true; // 无法获取路径,保守返回true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 使用Unity的ImageConversion加载原始图片数据
|
|
|
|
|
|
byte[] fileBytes = System.IO.File.ReadAllBytes(assetPath);
|
|
|
|
|
|
|
|
|
|
|
|
// 创建临时纹理来加载数据
|
|
|
|
|
|
Texture2D tempTexture = new Texture2D(2, 2);
|
|
|
|
|
|
if (tempTexture.LoadImage(fileBytes))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 检查加载后的像素数据
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var pixels = tempTexture.GetPixels32();
|
|
|
|
|
|
foreach (var pixel in pixels)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (pixel.a < 255 && pixel.a > 0) // 发现非完全不透明的像素
|
|
|
|
|
|
{
|
|
|
|
|
|
Object.DestroyImmediate(tempTexture);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Object.DestroyImmediate(tempTexture);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
Object.DestroyImmediate(tempTexture);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Object.DestroyImmediate(tempTexture);
|
|
|
|
|
|
return true; // 无法加载,保守返回true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning($"通过IO检查透明信息失败 {assetPath}: {e.Message}");
|
|
|
|
|
|
return true; // 出错时保守返回true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-16 13:05:02 +08:00
|
|
|
|
private static void _TryReplace(GameObject go)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 替换材质
|
|
|
|
|
|
var loadMat = AssetDatabase.LoadAssetAtPath<Material>(_matPath);
|
|
|
|
|
|
if (loadMat == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("加载材质失败: " + _matPath);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-09-16 13:50:51 +08:00
|
|
|
|
|
2025-09-16 13:05:02 +08:00
|
|
|
|
Debug.Log($"开始处理 {go.name}");
|
|
|
|
|
|
var isModify = false;
|
|
|
|
|
|
var allImages = go.GetComponentsInChildren<UnityEngine.UI.Image>(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;
|
|
|
|
|
|
}
|
2025-09-16 13:50:51 +08:00
|
|
|
|
|
2025-09-16 13:05:02 +08:00
|
|
|
|
img.material = loadMat;
|
|
|
|
|
|
Debug.Log($"替换材质: {img.gameObject.name} 贴图: {texture.name}");
|
|
|
|
|
|
isModify = true;
|
|
|
|
|
|
}
|
2025-09-16 13:50:51 +08:00
|
|
|
|
|
2025-09-16 13:05:02 +08:00
|
|
|
|
if (isModify)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorUtility.SetDirty(go);
|
|
|
|
|
|
AssetDatabase.SaveAssets();
|
|
|
|
|
|
}
|
2025-09-16 13:50:51 +08:00
|
|
|
|
|
2025-09-16 13:05:02 +08:00
|
|
|
|
Debug.Log($"处理完成 {go.name}");
|
|
|
|
|
|
}
|
2025-09-16 13:50:51 +08:00
|
|
|
|
|
2025-09-16 13:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|