【工具】增加自动替换UI材质工具
parent
cb0f4fc69d
commit
d91e1830d8
|
|
@ -18,31 +18,21 @@ namespace GameEditor.UIMaterialReplacer
|
|||
_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;
|
||||
if (!texture.isReadable)
|
||||
{
|
||||
return HasTransparencyFromFile(texture);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var pixels = texture.GetPixels32();
|
||||
foreach (var pixel in pixels)
|
||||
{
|
||||
if (pixel.a < 255) // 发现非完全不透明的像素
|
||||
if (pixel.a < 255 && pixel.a > 0) // 发现非完全不透明的像素
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
@ -55,7 +45,58 @@ namespace GameEditor.UIMaterialReplacer
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
private static void _TryReplace(GameObject go)
|
||||
{
|
||||
// 替换材质
|
||||
|
|
@ -65,7 +106,7 @@ namespace GameEditor.UIMaterialReplacer
|
|||
Debug.LogError("加载材质失败: " + _matPath);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Debug.Log($"开始处理 {go.name}");
|
||||
var isModify = false;
|
||||
var allImages = go.GetComponentsInChildren<UnityEngine.UI.Image>(true);
|
||||
|
|
@ -86,20 +127,20 @@ namespace GameEditor.UIMaterialReplacer
|
|||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
img.material = loadMat;
|
||||
Debug.Log($"替换材质: {img.gameObject.name} 贴图: {texture.name}");
|
||||
isModify = true;
|
||||
}
|
||||
|
||||
|
||||
if (isModify)
|
||||
{
|
||||
EditorUtility.SetDirty(go);
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
|
||||
|
||||
Debug.Log($"处理完成 {go.name}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue