152 lines
5.6 KiB
C#
152 lines
5.6 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
|
|
public class ReplaceStandardShaderWithURP : Editor
|
|
{
|
|
[MenuItem("Tools/一键转换Standard为URP材质")]
|
|
private static void ReplaceShadersInSelectedMaterials()
|
|
{
|
|
Material[] selectedMaterials = Selection.GetFiltered<Material>(SelectionMode.Assets);
|
|
|
|
if (selectedMaterials.Length == 0)
|
|
{
|
|
Debug.LogWarning("No materials selected. Please select materials in the Project window.");
|
|
return;
|
|
}
|
|
|
|
int replacedCount = 0;
|
|
|
|
foreach (Material material in selectedMaterials)
|
|
{
|
|
if (material.shader.name == "Standard" || material.shader.name == "Standard (Specular setup)")
|
|
{
|
|
// 保存原有属性
|
|
Texture albedoTexture = material.GetTexture("_MainTex");
|
|
Color albedoColor = material.GetColor("_Color");
|
|
Texture metallicMap = material.GetTexture("_MetallicGlossMap");
|
|
float metallic = material.GetFloat("_Metallic");
|
|
float smoothness = material.GetFloat("_Glossiness");
|
|
Texture normalMap = material.GetTexture("_BumpMap");
|
|
Texture emissionMap = material.GetTexture("_EmissionMap");
|
|
Color emissionColor = material.GetColor("_EmissionColor");
|
|
Texture occlusionMap = material.GetTexture("_OcclusionMap");
|
|
|
|
// 替换Shader
|
|
material.shader = Shader.Find("Universal Render Pipeline/Lit");
|
|
|
|
// 转换Albedo到BaseMap
|
|
if (albedoTexture != null)
|
|
{
|
|
material.SetTexture("_BaseMap", albedoTexture);
|
|
// 确保纹理导入设置正确
|
|
SetTextureImportSettings(albedoTexture);
|
|
}
|
|
|
|
material.SetColor("_BaseColor", albedoColor);
|
|
|
|
// 转换金属度和平滑度
|
|
if (metallicMap != null)
|
|
{
|
|
material.SetTexture("_MetallicGlossMap", metallicMap);
|
|
SetTextureImportSettings(metallicMap);
|
|
}
|
|
else
|
|
{
|
|
material.SetFloat("_Metallic", metallic);
|
|
}
|
|
|
|
material.SetFloat("_Smoothness", smoothness);
|
|
|
|
// 转换法线贴图
|
|
if (normalMap != null)
|
|
{
|
|
material.SetTexture("_BumpMap", normalMap);
|
|
SetTextureImportSettings(normalMap, true);
|
|
material.SetFloat("_BumpScale", 1.0f); // URP默认使用1.0的比例
|
|
}
|
|
|
|
// 转换自发光
|
|
if (emissionMap != null)
|
|
{
|
|
material.SetTexture("_EmissionMap", emissionMap);
|
|
material.SetColor("_EmissionColor", emissionColor);
|
|
material.EnableKeyword("_EMISSION");
|
|
SetTextureImportSettings(emissionMap);
|
|
}
|
|
|
|
// 转换环境光遮蔽
|
|
if (occlusionMap != null)
|
|
{
|
|
material.SetTexture("_OcclusionMap", occlusionMap);
|
|
SetTextureImportSettings(occlusionMap);
|
|
}
|
|
|
|
EditorUtility.SetDirty(material);
|
|
replacedCount++;
|
|
}
|
|
}
|
|
|
|
AssetDatabase.SaveAssets();
|
|
AssetDatabase.Refresh();
|
|
|
|
Debug.Log($"Replaced {replacedCount} materials with URP Lit shader.");
|
|
}
|
|
|
|
private static void SetTextureImportSettings(Texture texture, bool isNormalMap = false)
|
|
{
|
|
string path = AssetDatabase.GetAssetPath(texture);
|
|
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
|
|
|
|
if (importer != null)
|
|
{
|
|
importer.sRGBTexture = !isNormalMap; // 法线贴图应该禁用sRGB
|
|
importer.textureType = isNormalMap ? TextureImporterType.NormalMap : TextureImporterType.Default;
|
|
importer.mipmapEnabled = true;
|
|
importer.SaveAndReimport();
|
|
}
|
|
}
|
|
}
|
|
// public class ReplaceStandardShaderWithURP : Editor
|
|
// {
|
|
// [MenuItem("Tools/Replace Standard Shader with URP")]
|
|
// private static void ReplaceShadersInSelectedMaterials()
|
|
// {
|
|
// // 获取选中的材质文件
|
|
// Material[] selectedMaterials = Selection.GetFiltered<Material>(SelectionMode.Assets);
|
|
//
|
|
// if (selectedMaterials.Length == 0)
|
|
// {
|
|
// Debug.LogWarning("No materials selected. Please select materials in the Project window.");
|
|
// return;
|
|
// }
|
|
//
|
|
// int replacedCount = 0;
|
|
//
|
|
// foreach (Material material in selectedMaterials)
|
|
// {
|
|
// if (material.shader.name == "Standard" || material.shader.name == "Standard (Specular setup)")
|
|
// {
|
|
// // 保存Albedo贴图
|
|
// Texture albedoTexture = material.GetTexture("_MainTex");
|
|
//
|
|
// // 替换Shader为Universal Render Pipeline/Lit
|
|
// material.shader = Shader.Find("Universal Render Pipeline/Lit");
|
|
//
|
|
// // 将保存的Albedo贴图赋值给Base Map
|
|
// if (albedoTexture != null)
|
|
// {
|
|
// material.SetTexture("_BaseMap", albedoTexture);
|
|
// }
|
|
//
|
|
// // 保存材质
|
|
// EditorUtility.SetDirty(material);
|
|
// replacedCount++;
|
|
// }
|
|
// }
|
|
//
|
|
// AssetDatabase.SaveAssets();
|
|
// AssetDatabase.Refresh();
|
|
//
|
|
// Debug.Log($"Replaced {replacedCount} materials with URP Lit shader.");
|
|
// }
|
|
// } |