Unity 批量替换物体shader

35 篇文章 4 订阅
该代码段展示了如何在Unity编辑器中创建一个窗口工具,用于批量选择对象并替换它们的Shader。同时,它迁移了相关颜色、纹理等属性,包括_BaseColor、_BaseMap、_MetallicGlossMap、_BumpMap、_OcclusionMap和_ParallaxMap。此工具对于更新项目中的Shader和材质属性非常有用。
摘要由CSDN通过智能技术生成

hastexture 这个api好像有问题 getTexture替代
HasProperty这个比较好

新版URP貌似已经有了

在这里插入图片描述

在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

public class ConvertURPLit : EditorWindow
{
    private static ConvertURPLit _myWindow;

    public Shader shader;


    [MenuItem("Tools/ConvertURPLit", false, priority = 20)]
    static void Init()
    {
        _myWindow = (ConvertURPLit) EditorWindow.GetWindow(typeof(ConvertURPLit), false, "替换shader", true); //创建窗口
        _myWindow.Show(); //展示
    }

    void OnGUI()
    {
        shader = EditorGUILayout.ObjectField("替换的shader", shader, typeof(Shader), false) as Shader;
        
        if (GUILayout.Button("转换"))
        {
            var objs = Selection.objects;
            if (objs.Length > 0 && shader)
            {
                foreach (var obj in objs)
                {
                    var renderers = obj.GetComponentsInChildren<Renderer>();
                    foreach (var renderer in renderers)
                    {
                        var mats = renderer.sharedMaterials;

                        for (int i = 0; i <  mats.Length; i++)
                        {
                            Undo.RecordObject(renderer.sharedMaterials[i], "shader ssss");
                            renderer.sharedMaterials[i].shader = shader;
                            
                            if (mats[i].HasColor("_Color"))
                            {
                                renderer.sharedMaterials[i].SetColor("_BaseColor",mats[i].GetColor("_Color"));
                            }
                            
                            if (mats[i].GetTexture("_Albedo"))
                            {
                                renderer.sharedMaterials[i].mainTexture = mats[i].GetTexture("_Albedo");
                                renderer.sharedMaterials[i].SetTexture("_BaseMap", mats[i].GetTexture("_Albedo"));
                                
                            }
                            
                            if (mats[i].GetTexture("_MainTex"))
                            {
                                renderer.sharedMaterials[i].mainTexture = mats[i].GetTexture("_MainTex");
                                renderer.sharedMaterials[i].SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
                            }

                            if (mats[i].HasTexture("_Occlusion"))
                            {
                                renderer.sharedMaterials[i].SetTexture("_OcclusionMap", mats[i].GetTexture("_Occlusion"));
                            }

                            if (mats[i].HasTexture("_Height"))
                            {
                                renderer.sharedMaterials[i].SetTexture("_ParallaxMap", mats[i].GetTexture("_Height")) ;
                            }

                        }
                    }
                  
                }

                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();
            }
        }
    }
}

不知为何会有问题

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;

public class ConvertURPLit : EditorWindow
{
    private static ConvertURPLit _myWindow;

    public Shader shader;


    [MenuItem("Tools/ConvertURPLit", false, priority = 20)]
    static void Init()
    {
        _myWindow = (ConvertURPLit) EditorWindow.GetWindow(typeof(ConvertURPLit), false, "替换shader", true); //创建窗口
        _myWindow.Show(); //展示
    }

    void OnGUI()
    {
        shader = EditorGUILayout.ObjectField("替换的shader", shader, typeof(Shader), false) as Shader;
        
        if (GUILayout.Button("转换"))
        {
            var objs = Selection.objects;
            if (objs.Length > 0 && shader)
            {
                foreach (var obj in objs)
                {
                    var renderers = obj.GetComponentsInChildren<Renderer>();
                    foreach (var renderer in renderers)
                    {
                        var mats = renderer.sharedMaterials;
                        var tempMats = new Material[renderer.sharedMaterials.Length];
                        tempMats = renderer.materials;

                        for (int i = 0; i <  mats.Length; i++)
                        {
                            Undo.RecordObject(renderer.sharedMaterials[i], "shader ssss");
                            renderer.sharedMaterials[i].shader = shader;
                            
                            if (mats[i].HasColor("_Color"))
                            {
                                tempMats[i].SetColor("_BaseColor",mats[i].GetColor("_Color"));
                            }
                            
                            if (mats[i].HasProperty("_Albedo"))
                            {
                                tempMats[i].mainTexture = mats[i].GetTexture("_Albedo");
                                tempMats[i].SetTexture("_BaseMap", mats[i].GetTexture("_Albedo"));
                                
                            }
                            
                            if (mats[i].HasTexture("_Metallic"))
                            {
                                tempMats[i].SetTexture("_MetallicGlossMap", mats[i].GetTexture("_Metallic"));
                                
                            }
                            
                            if (mats[i].HasProperty("_Normal"))
                            {
                                tempMats[i].SetTexture("_BumpMap", mats[i].GetTexture("_Normal"));
                                
                            }
                            
                            if (mats[i].HasProperty("_BumpMap"))
                            {
                                tempMats[i].SetTexture("_BumpMap", mats[i].GetTexture("_BumpMap"));
                                
                            }
                            
                            if (mats[i].HasProperty("_MainTex"))
                            {
                                // tempMats[i].mainTexture = mats[i].GetTexture("_MainTex");
                                // tempMats[i].SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
                                renderer.material.SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
                            }
                            
                            // if (mats[i].HasTexture("_MainTex"))
                            // {
                            //     tempMats[i].mainTexture = mats[i].GetTexture("_MainTex");
                            //     tempMats[i].SetTexture("_BaseMap", mats[i].GetTexture("_MainTex"));
                            // }

                            if (mats[i].HasTexture("_Occlusion"))
                            {
                                tempMats[i].SetTexture("_OcclusionMap", mats[i].GetTexture("_Occlusion"));
                            }

                            if (mats[i].HasTexture("_Height"))
                            {
                                tempMats[i].SetTexture("_ParallaxMap", mats[i].GetTexture("_Height")) ;
                            }

                        }

                        renderer.sharedMaterials = tempMats;
                    }
                    
                  
                }

                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值