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.代码:

csharp 复制代码
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.点右上角三个点,进行调用脚本中的方法。


相关推荐
xcLeigh9 小时前
Unity基础:Instantiate实例化——在运行时动态创建游戏物体
游戏·unity·游戏引擎·教程·instantiate
脆性薄片搬运谢工9 小时前
同厚度不同材质(硅、玻璃、蓝宝石、碳化硅),脆性薄片搬运允许的最大弯曲挠度分别是多少?
材质·脆性薄片搬运专机·伯努利吸盘·超薄玻璃搬运·非接触式上料
流浪打工人1 天前
Unity3d TimeLine中audio track的使用
unity
淡海水1 天前
02-07-原理篇-GC暂停时间的本质
开发语言·unity·c#·gc·暂停时间
WarPigs1 天前
UI路径配置SO>字典json工作流
ui·unity
三棵树园艺2 天前
行业复盘|2026商用景观软装技术迭代与市场分析:仿真绿植替代真绿植的底层逻辑与落地实践
创业创新·材质
淡海水2 天前
02-06-原理篇-增量式GC与并发GC
算法·unity·c#·并发·gc·增量
阿松爱学习2 天前
【Unity开发】Aspose.PDF的使用
unity·pdf·c#
淡海水2 天前
02-04-原理篇-GC Roots与对象图遍历
算法·unity·c#·图·gc·遍历·roots
三棵树园艺6 天前
室内仿真假山流水工程避坑指南|2026行业数据+材质测评+标准化施工全方案
材质