Unity材质球自动遍历所需贴图

Unity材质球自动遍历所需贴图



一、原理

例如一个材质球名为:Decal_Text_Cranes_01_Mat ,
然后从全局遍历出:Decal_Text_Cranes_01_Albedo赋值给材质球的BaseMap,
全局遍历出Decal_Text_Cranes_01_MAODS 赋值给材质球MetallicMap通道,
全局遍历出Decal_Text_Cranes_01_Normal 给材质球NormalMap通道,
**规律:**材质球名字:Decal_Text_Cranes_01_Mat 把后面Mat换成通道名称,就是该材质球的通道贴图


二、用法

1.代码:

using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEditor;

public class AutoAssignTextureMaps : MonoBehaviour
{
    public List<Material> targetMaterials; // 在Inspector中指定目标材质列表
    private Dictionary<string, string> textureMapNames = new Dictionary<string, string>
    {
        { "Albedo", "_BaseMap" },   // Base Color
        { "MAODS", "_MetallicGlossMap" }, // Metallic and Smoothness
        { "Normal", "_BumpMap" }     // Normal Map
    };


    [ContextMenu("_AlphaMat后缀自动补全")]
    void AssignTextures1( )
    {
        foreach (Material material in targetMaterials)
        {
            string baseName = material.name.Replace("_AlphaMat", "");
            foreach (var pair in textureMapNames)
            {
                string textureName = baseName + "_" + pair.Key;
                Texture2D texture = FindTexture(textureName);
                if (texture != null)
                {
                    material.SetTexture(pair.Value, texture);
                    Debug.Log($"Assigned {textureName} to {pair.Value} for material {material.name}");
                }
                else
                {
                    Debug.LogError($"Could not find texture {textureName} for material {material.name}");
                }
            }
        }
    }

    [ContextMenu("_Mat后缀自动补全")]
    void AssignTextures2( )
    {
        foreach (Material material in targetMaterials)
        {
            string baseName = material.name.Replace("_Mat", "");
            foreach (var pair in textureMapNames)
            {
                string textureName = baseName + "_" + pair.Key;
                Texture2D texture = FindTexture(textureName);
                if (texture != null)
                {
                    material.SetTexture(pair.Value, texture);
                    Debug.Log($"Assigned {textureName} to {pair.Value} for material {material.name}");
                }
                else
                {
                    Debug.LogError($"Could not find texture {textureName} for material {material.name}");
                }
            }
        }
    }
    Texture2D FindTexture(string textureName)
    {
        string[] guids = AssetDatabase.FindAssets(textureName);
        if (guids.Length > 0)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guids[0]);
            return AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
        }
        return null;
    }
}

2.使用方法

在这里插入图片描述

1.将脚本挂载到一个空物体:
2.把所需的材质球添加到集合列表中。
3.点右上角三个点,进行调用脚本中的方法。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_济南橙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值