Unity3d 场景从HDRP转换到URP

 以下代码功能是把材质从HDRP转换到URP。具体根据需要添加字段

代码附加到一个物体上,先右键执行初始化。然后右键选择转换。

using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
[System.Serializable]

public class TextureMap
{
    public string source;
    public string target;
    [HideInInspector]
    public Texture tex;
}
[System.Serializable]
public class ShaderPair
{
    public Shader source;
    public Shader target;
}
public class HdrpToUrpConverter : MonoBehaviour
{
    // Start is called before the first frame update
    [Header("指定文件夹,默认是根目录")]
    public string root;
    [Header("要替换的贴图")]
    public List mapProperties;
      public List shaderpairs;

    private string[] kvs = new string[]
    {
        "_MainTex", "_BaseMap", "_BaseColorMap", "_BaseMap", "_NormalMap", "_BumpMap", "_AlphaCutoffEnabled",
        "_AlphaClip"
    };

    private string[] shadderKvs = new[]
    {
        "HDRP/Lit", "Universal Render Pipeline/Lit", "HDRP/Unlit", "Universal Render Pipeline/Unlit"
    };

    [ContextMenu("初始化工具")]
    private void ManualValidate()
    {
        mapProperties = new();
        for (var i = 0; i < kvs.Length; i+=2)
        {
            mapProperties.Add(new TextureMap()
            {
                source = kvs[i],
                target = kvs[i+1]
            });
        }

        shaderpairs = new List();
        for (var index = 0; index < shadderKvs.Length; index+=2)
        {
            var s = shadderKvs[index];
            var t = shadderKvs[index +1];
            shaderpairs.Add(new ShaderPair()
            {
                source = Shader.Find(s),
                target = Shader.Find(t)
            });
        }
    }

    private void Start()
    {
       
    }

    public Shader GetURPShader(Shader source)
    {
        foreach (var s in shaderpairs)
        {
            if (s.source == source) return s.target;
        }
        return Shader.Find("Universal Render Pipeline/Lit");
    }
    public Shader GetHDRPShader(Shader target)
    {
        foreach (var s in shaderpairs)
        {
            if (s.target == target) return s.source;
        }
        return Shader.Find("HDRP/Lit");
    }
    public void ReplaceToUrp(Material mat,Shader s)
    {

        foreach (var p in mapProperties)
        {
            if (mat.HasProperty(p.source))
            {
                p.tex = mat.GetTexture(p.source);
            }
        }

        mat.shader = s;
        foreach (var p in mapProperties)
        {
            if (mat.HasProperty(p.target))
            {
                mat.SetTexture(p.target,p.tex );
            }
        }
    }
    public void ReplaceToHDRP(Material mat,Shader s)
    {

        foreach (var p in mapProperties)
        {
            if (mat.HasProperty(p.target))
            {
                p.tex = mat.GetTexture(p.target);
            }
        }

        mat.shader = s;
        foreach (var p in mapProperties)
        {
            if (mat.HasProperty(p.source))
            {
                mat.SetTexture(p.source,p.tex );
            }
        }
    }
    public void  WalkthroughMaterials(string folder,Action replace,FuncgetShader)
    {
        folder = "Assets/"+folder;
        string[] allFiles = Directory.GetFiles(folder, "*.mat", SearchOption.AllDirectories);

        foreach (string file in allFiles)
        {
            Material mat = AssetDatabase.LoadAssetAtPath(file);
            if (mat != null)
            {
                var s = getShader(mat.shader);
                replace(mat,s);
                // Do something with the material
            }
        }
    }
   
    [ContextMenu("HDRP到URP")]
    void Hdrp2Urp()
    {
        WalkthroughMaterials(root,ReplaceToUrp,GetURPShader);
    }
    [ContextMenu("URP到HDRP")]
    void Urp2Hdrp()
    {
        WalkthroughMaterials(root,ReplaceToHDRP,GetHDRPShader);
    }
}

你可能感兴趣的:(c#)