Net.Like.Xue.Tokyo/Packages-Local/Net.BITKit.Bounds.Unity/CustomShaderToURPLitConvert...

108 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEditor;
using UnityEngine;
public class CustomShaderToURPLitConverter : EditorWindow
{
private Shader customShader;
[MenuItem("Tools/转换材质为 URP/Lit")]
public static void ShowWindow()
{
GetWindow(typeof(CustomShaderToURPLitConverter));
}
void OnGUI()
{
GUILayout.Label("自定义 Shader → URP/Lit 材质转换器", EditorStyles.boldLabel);
customShader = EditorGUILayout.ObjectField("原始自定义 Shader", customShader, typeof(Shader), false) as Shader;
if (GUILayout.Button("开始转换"))
{
if (customShader == null)
{
Debug.LogError("请先指定自定义 Shader");
return;
}
ConvertMaterials(customShader);
}
}
[MenuItem("Tools/检查 URP 材质异常设置")]
public static void CheckLitMaterial()
{
foreach (var guid in AssetDatabase.FindAssets("t:Material"))
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var mat = AssetDatabase.LoadAssetAtPath<Material>(path);
if (mat.shader == null || !mat.shader.name.Contains("Universal Render Pipeline/Lit")) continue;
Debug.Log($"🧪 检查: {mat.name}");
if (mat.IsKeywordEnabled("_MASKMAP"))
{
if (!mat.HasProperty("_MaskMap") || mat.GetTexture("_MaskMap") == null)
Debug.LogWarning($"🔴 {mat.name} 启用了 _MASKMAP 但贴图为空");
}
if (mat.IsKeywordEnabled("_NORMALMAP"))
{
if (!mat.HasProperty("_BumpMap") || mat.GetTexture("_BumpMap") == null)
Debug.LogWarning($"🔴 {mat.name} 启用了 _NORMALMAP 但贴图为空");
}
}
}
static void ConvertMaterials(Shader sourceShader)
{
string[] guids = AssetDatabase.FindAssets("t:Material");
int convertedCount = 0;
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);
if (mat.shader != sourceShader) continue;
Debug.Log($"转换材质: {mat.name}");
// 备份原始属性
Texture baseMap = mat.GetTexture("_Base_Map");
Texture maskMap = mat.GetTexture("_Maskmap");
Texture normalMap = mat.GetTexture("_Normal");
Color baseColor = mat.HasProperty("_BaseColor") ? mat.GetColor("_BaseColor") : Color.white;
float normalStrength = mat.HasProperty("_Normal_Strength") ? mat.GetFloat("_Normal_Strength") : 1f;
// 替换 Shader
mat.shader = Shader.Find("Universal Render Pipeline/Lit");
// 设置贴图
if (baseMap) mat.SetTexture("_BaseMap", baseMap);
mat.SetColor("_BaseColor", baseColor);
if (normalMap)
{
mat.SetTexture("_BumpMap", normalMap);
mat.SetFloat("_BumpScale", normalStrength);
mat.EnableKeyword("_NORMALMAP");
}
if (maskMap)
{
mat.SetTexture("_OcclusionMap", maskMap);
mat.SetFloat("_OcclusionStrength", 1.0f); // 默认强度
mat.EnableKeyword("_OCCLUSIONMAP");
}
EditorUtility.SetDirty(mat);
convertedCount++;
}
AssetDatabase.SaveAssets();
Debug.Log($"转换完成,共处理材质:{convertedCount} 个");
}
}