Files
virtual-university-tour/sources/Assets/Editor/ConvertStandardToUrpLit.cs
T

151 lines
5.1 KiB
C#
Raw Normal View History

2026-07-12 18:53:05 +04:00
using System.Text;
using UnityEditor;
using UnityEngine;
public static class ConvertStandardToUrpLit
{
[MenuItem("Tools/Convert Standard (All) To URP Lit")]
private static void Convert()
{
var report = new StringBuilder();
Object selected = Selection.activeObject;
string folder = selected != null ? AssetDatabase.GetAssetPath(selected) : "";
report.AppendLine("=== Standard -> URP Lit (Full Mode) ===");
report.AppendLine($"Selected: {(selected ? selected.name : "NULL")}");
report.AppendLine($"Path: {folder}");
report.AppendLine();
if (!AssetDatabase.IsValidFolder(folder))
{
report.AppendLine("[ERROR] Select a folder.");
Debug.LogError(report.ToString());
return;
}
Shader urpLit = Shader.Find("Universal Render Pipeline/Lit");
if (urpLit == null)
{
report.AppendLine("[ERROR] URP/Lit shader not found.");
Debug.LogError(report.ToString());
return;
}
string[] guids = AssetDatabase.FindAssets("t:Material", new[] { folder });
report.AppendLine($"Materials found: {guids.Length}");
report.AppendLine();
int converted = 0;
int skipped = 0;
int failed = 0;
foreach (var guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);
if (mat == null)
{
failed++;
report.AppendLine($"[FAIL] {path} | null material");
continue;
}
string shader = mat.shader != null ? mat.shader.name : "NULL";
report.AppendLine($"[FOUND] {path}");
report.AppendLine($" Shader: {shader}");
bool isStandard =
shader == "Standard" ||
shader == "Standard (Specular setup)";
if (!isStandard)
{
skipped++;
report.AppendLine(" Action: SKIP (not Standard)");
report.AppendLine();
continue;
}
// --- cache ---
Texture mainTex = mat.GetTexture("_MainTex");
Color color = mat.HasProperty("_Color") ? mat.GetColor("_Color") : Color.white;
Texture bumpMap = mat.GetTexture("_BumpMap");
float bumpScale = mat.HasProperty("_BumpScale") ? mat.GetFloat("_BumpScale") : 1f;
Texture emissionMap = mat.GetTexture("_EmissionMap");
Color emissionColor = mat.HasProperty("_EmissionColor") ? mat.GetColor("_EmissionColor") : Color.black;
float gloss = mat.HasProperty("_Glossiness") ? mat.GetFloat("_Glossiness") : 0.5f;
Texture specGloss = mat.GetTexture("_SpecGlossMap");
Color specColor = mat.HasProperty("_SpecColor") ? mat.GetColor("_SpecColor") : Color.white;
try
{
mat.shader = urpLit;
// Base
if (mat.HasProperty("_BaseMap"))
mat.SetTexture("_BaseMap", mainTex);
if (mat.HasProperty("_BaseColor"))
mat.SetColor("_BaseColor", color);
// Normal
if (mat.HasProperty("_BumpMap"))
mat.SetTexture("_BumpMap", bumpMap);
if (mat.HasProperty("_BumpScale"))
mat.SetFloat("_BumpScale", bumpScale);
// Smoothness
if (mat.HasProperty("_Smoothness"))
mat.SetFloat("_Smoothness", gloss);
// Specular (only used if URP supports spec workflow)
if (mat.HasProperty("_SpecColor"))
mat.SetColor("_SpecColor", specColor);
if (mat.HasProperty("_SpecGlossMap"))
mat.SetTexture("_SpecGlossMap", specGloss);
// Emission
if (emissionMap != null && mat.HasProperty("_EmissionMap"))
{
mat.EnableKeyword("_EMISSION");
mat.SetTexture("_EmissionMap", emissionMap);
mat.SetColor("_EmissionColor", emissionColor);
}
EditorUtility.SetDirty(mat);
converted++;
report.AppendLine(" Action: CONVERTED");
}
catch (System.Exception e)
{
failed++;
report.AppendLine(" Action: FAILED");
report.AppendLine($" Error: {e.Message}");
}
report.AppendLine();
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
report.AppendLine("=== SUMMARY ===");
report.AppendLine($"Converted: {converted}");
report.AppendLine($"Skipped: {skipped}");
report.AppendLine($"Failed: {failed}");
report.AppendLine($"Total: {guids.Length}");
Debug.Log(report.ToString());
}
}