[Unity] URP에서 에셋 Material이 보라색으로 보일 때 해결법

muchan
muchan
∙∙조회 99
[Unity] URP에서 에셋 Material이 보라색으로 보일 때 해결법

개요

Unity에서 asset store에서 다운로드 받은 에셋을 import 하면 보라색으로 보이는 경우가 있다

보통은 이럴 때 Window - Rendering - Render Pipeline Conerter를 눌러서

주로 두번째 Material Upgrade를 누르고 Initialize Converters를 누르고 로딩 후 Convert Assets를 누르면

보통은 해결이 되는데 이렇게 안되는 경우가 있다.

클로드에게 물어보니 이런 이유라고 합니다..


해결해보기

아무튼 이것을 해결하기 위해 스크립트를 작성했습니다.

코드

csharp
using UnityEditor;
using UnityEngine;

public class FixQuirkyMaterials : EditorWindow
{
    [MenuItem("Tools/Fix Quirky Materials to URP")]
    static void FixMaterials()
    {
        // URP 셰이더 찾기
        Shader urpShader = Shader.Find("Shader Graphs/SoftSurfaceGraph");

        if (urpShader == null)
        {
            Debug.LogError("SoftSurfaceGraph 셰이더를 찾을 수 없어요!");
            return;
        }

        // 모든 머티리얼 찾기
        string[] guids = AssetDatabase.FindAssets("t:Material", new[] { "Assets/Quirky Series" });
        int count = 0;

        foreach (string guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);

            if (mat != null && mat.shader.name != "Shader Graphs/SoftSurfaceGraph")
            {
                mat.shader = urpShader;
                EditorUtility.SetDirty(mat);
                count++;
                Debug.Log($"변환됨: {path}");
            }
        }

        AssetDatabase.SaveAssets();
        Debug.Log($"총 {count}개 머티리얼 변환 완료!");
    }
}
  • URP 셰이더를 찾고
  • 특정 폴더의 Material을 전부 검색해서
  • 아직 안바뀐것만 골라
  • 셰이더를 교체하고 저장하는 에디터 자동화

하는 코드입니다.

나중에 경로를 수정해서 재활용 할 수 있다

그러면 Tools에 Fix Quirky Materials to URP가 나오는데 이걸 클릭하면

이렇게 보라색이던 동물들이 형태가 보이게 된다

추가

근데 살짝 어두워보여서 뭔가 했는데

이 Material 색이 살짝 회색이다.

csharp
using UnityEditor;
using UnityEngine;

public class FixQuirkyMaterials : EditorWindow
{
    [MenuItem("Tools/Fix Quirky Materials to URP")]
    static void FixMaterials()
    {
        Shader urpShader = Shader.Find("Shader Graphs/SoftSurfaceGraph");

        if (urpShader == null)
        {
            Debug.LogError("SoftSurfaceGraph 셰이더를 찾을 수 없어요!");
            return;
        }

        // 프로퍼티 이름 출력만 하고 종료
        int propCount = ShaderUtil.GetPropertyCount(urpShader);
        for (int i = 0; i < propCount; i++)
        {
            Debug.Log($"프로퍼티: {ShaderUtil.GetPropertyName(urpShader, i)}");
        }
    }
}

이렇게 수정을 하고 다시 Tools에 실행을 해보면

프로퍼티 항목이 나오는데 여기서 _Color를 수정해서 RGB값을 조절하면

csharp
using UnityEditor;
using UnityEngine;

public class FixQuirkyMaterials : EditorWindow
{
    [MenuItem("Tools/Fix Quirky Materials to URP")]
    static void FixMaterials()
    {
        Shader urpShader = Shader.Find("Shader Graphs/SoftSurfaceGraph");

        if (urpShader == null)
        {
            Debug.LogError("SoftSurfaceGraph 셰이더를 찾을 수 없어요!");
            return;
        }

        string[] guids = AssetDatabase.FindAssets("t:Material", new[] { "Assets/Quirky Series" });
        int count = 0;

        foreach (string guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);

            mat.shader = urpShader;
            mat.SetColor("_Color", new Color(188f / 255f, 188f / 255f, 188f / 255f));
            mat.SetFloat("_Emission", 0.5f);
            EditorUtility.SetDirty(mat);
            count++;
            Debug.Log($"변환됨: {path}");
        }

        AssetDatabase.SaveAssets();
        Debug.Log($"총 {count}개 머티리얼 변환 완료!");
    }
}

멋진 동물들이 나오게 된다