140 lines
5.5 KiB
C#
140 lines
5.5 KiB
C#
|
using UnityEngine;
|
|||
|
using UnityEditor;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
public class HDRPToURPMaterialConverter : EditorWindow
|
|||
|
{
|
|||
|
private List<Material> hdrpMaterials = new List<Material>();
|
|||
|
private List<bool> selected;
|
|||
|
private Vector2 scrollPos;
|
|||
|
|
|||
|
[MenuItem("工具/材质/HDRP转URP批量转换器")]
|
|||
|
public static void ShowWindow()
|
|||
|
{
|
|||
|
GetWindow<HDRPToURPMaterialConverter>("HDRP转URP材质");
|
|||
|
}
|
|||
|
|
|||
|
private void OnEnable()
|
|||
|
{
|
|||
|
RefreshHDRPMaterials();
|
|||
|
}
|
|||
|
|
|||
|
private void RefreshHDRPMaterials()
|
|||
|
{
|
|||
|
hdrpMaterials = AssetDatabase.FindAssets("t:Material")
|
|||
|
.Select(guid => AssetDatabase.GUIDToAssetPath(guid))
|
|||
|
.Select(path => AssetDatabase.LoadAssetAtPath<Material>(path))
|
|||
|
.Where(mat => mat != null && mat.shader != null && mat.shader.name.StartsWith("HDRP/"))
|
|||
|
.ToList();
|
|||
|
selected = new List<bool>(new bool[hdrpMaterials.Count]);
|
|||
|
}
|
|||
|
|
|||
|
private void OnGUI()
|
|||
|
{
|
|||
|
EditorGUILayout.Space();
|
|||
|
if (GUILayout.Button("刷新HDRP材质列表"))
|
|||
|
{
|
|||
|
RefreshHDRPMaterials();
|
|||
|
}
|
|||
|
EditorGUILayout.Space();
|
|||
|
if (hdrpMaterials.Count == 0)
|
|||
|
{
|
|||
|
EditorGUILayout.HelpBox("未检测到HDRP材质。", MessageType.Info);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
EditorGUILayout.BeginHorizontal();
|
|||
|
if (GUILayout.Button("全选"))
|
|||
|
{
|
|||
|
for (int i = 0; i < selected.Count; i++) selected[i] = true;
|
|||
|
}
|
|||
|
if (GUILayout.Button("全不选"))
|
|||
|
{
|
|||
|
for (int i = 0; i < selected.Count; i++) selected[i] = false;
|
|||
|
}
|
|||
|
EditorGUILayout.EndHorizontal();
|
|||
|
|
|||
|
EditorGUILayout.LabelField($"检测到HDRP材质数量: {hdrpMaterials.Count}");
|
|||
|
EditorGUILayout.Space();
|
|||
|
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(300));
|
|||
|
for (int i = 0; i < hdrpMaterials.Count; i++)
|
|||
|
{
|
|||
|
EditorGUILayout.BeginHorizontal();
|
|||
|
selected[i] = EditorGUILayout.Toggle(selected[i], GUILayout.Width(20));
|
|||
|
EditorGUILayout.ObjectField(hdrpMaterials[i], typeof(Material), false);
|
|||
|
EditorGUILayout.EndHorizontal();
|
|||
|
}
|
|||
|
EditorGUILayout.EndScrollView();
|
|||
|
EditorGUILayout.Space();
|
|||
|
|
|||
|
if (GUILayout.Button("转换选中材质"))
|
|||
|
{
|
|||
|
ConvertSelectedMaterials();
|
|||
|
}
|
|||
|
if (GUILayout.Button("一键全部转换"))
|
|||
|
{
|
|||
|
for (int i = 0; i < selected.Count; i++) selected[i] = true;
|
|||
|
ConvertSelectedMaterials();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ConvertSelectedMaterials()
|
|||
|
{
|
|||
|
int count = 0;
|
|||
|
for (int i = 0; i < hdrpMaterials.Count; i++)
|
|||
|
{
|
|||
|
if (selected[i])
|
|||
|
{
|
|||
|
if (ConvertMaterial(hdrpMaterials[i]))
|
|||
|
count++;
|
|||
|
}
|
|||
|
}
|
|||
|
AssetDatabase.SaveAssets();
|
|||
|
EditorUtility.DisplayDialog("转换完成", $"成功转换 {count} 个材质。", "确定");
|
|||
|
RefreshHDRPMaterials();
|
|||
|
}
|
|||
|
|
|||
|
private bool ConvertMaterial(Material mat)
|
|||
|
{
|
|||
|
if (mat == null) return false;
|
|||
|
Undo.RecordObject(mat, "HDRP材质转URP");
|
|||
|
// 只处理HDRP Lit
|
|||
|
if (mat.shader.name == "HDRP/Lit")
|
|||
|
{
|
|||
|
Shader urpShader = Shader.Find("Universal Render Pipeline/Lit");
|
|||
|
if (urpShader == null)
|
|||
|
{
|
|||
|
Debug.LogError("未找到URP Lit Shader,请确保已安装URP。");
|
|||
|
return false;
|
|||
|
}
|
|||
|
// 备份常用属性
|
|||
|
Color baseColor = mat.HasProperty("_BaseColor") ? mat.GetColor("_BaseColor") : Color.white;
|
|||
|
Texture baseMap = mat.HasProperty("_BaseColorMap") ? mat.GetTexture("_BaseColorMap") : null;
|
|||
|
float metallic = mat.HasProperty("_Metallic") ? mat.GetFloat("_Metallic") : 0f;
|
|||
|
float smoothness = mat.HasProperty("_Smoothness") ? mat.GetFloat("_Smoothness") : 0.5f;
|
|||
|
Texture maskMap = mat.HasProperty("_MaskMap") ? mat.GetTexture("_MaskMap") : null;
|
|||
|
Texture normalMap = mat.HasProperty("_NormalMap") ? mat.GetTexture("_NormalMap") : null;
|
|||
|
float normalScale = mat.HasProperty("_NormalScale") ? mat.GetFloat("_NormalScale") : 1f;
|
|||
|
Texture emissionMap = mat.HasProperty("_EmissiveColorMap") ? mat.GetTexture("_EmissiveColorMap") : null;
|
|||
|
Color emissionColor = mat.HasProperty("_EmissiveColor") ? mat.GetColor("_EmissiveColor") : Color.black;
|
|||
|
|
|||
|
mat.shader = urpShader;
|
|||
|
// 还原属性
|
|||
|
if (mat.HasProperty("_BaseMap")) mat.SetTexture("_BaseMap", baseMap);
|
|||
|
if (mat.HasProperty("_BaseColor")) mat.SetColor("_BaseColor", baseColor);
|
|||
|
if (mat.HasProperty("_Metallic")) mat.SetFloat("_Metallic", metallic);
|
|||
|
if (mat.HasProperty("_Smoothness")) mat.SetFloat("_Smoothness", smoothness);
|
|||
|
if (mat.HasProperty("_BumpMap")) mat.SetTexture("_BumpMap", normalMap);
|
|||
|
if (mat.HasProperty("_BumpScale")) mat.SetFloat("_BumpScale", normalScale);
|
|||
|
if (mat.HasProperty("_EmissionMap")) mat.SetTexture("_EmissionMap", emissionMap);
|
|||
|
if (mat.HasProperty("_EmissionColor")) mat.SetColor("_EmissionColor", emissionColor);
|
|||
|
// Metallic/Smoothness贴图(MaskMap)可选迁移到Metallic Map
|
|||
|
if (maskMap != null && mat.HasProperty("_MetallicGlossMap"))
|
|||
|
mat.SetTexture("_MetallicGlossMap", maskMap);
|
|||
|
EditorUtility.SetDirty(mat);
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|