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


相关推荐
Yasin Chen3 小时前
C# Dictionary源码分析
算法·unity·哈希算法
深海潜水员11 小时前
【Behavior Tree】-- 行为树AI逻辑实现- Unity 游戏引擎实现
游戏·unity·c#
远离UE417 小时前
UE 材质 变体 概念
ue5·材质
Superstarimage17 小时前
【EGSR2025】材质+扩散模型+神经网络相关论文整理随笔
人工智能·神经网络·材质
Thomas_YXQ20 小时前
Unity3D游戏内存优化指南
游戏·unity·职场和发展·性能优化·蓝桥杯·游戏引擎·unity3d
chillxiaohan21 小时前
Unity接入Steamworks.NET实现通信功能
unity
枯萎穿心攻击1 天前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
X_StarX2 天前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
Thomas_YXQ2 天前
Unity URP法线贴图实现教程
开发语言·unity·性能优化·游戏引擎·unity3d·贴图·单一职责原则
徐子竣2 天前
[学习记录]Unity-Shader-几何着色器
unity·游戏引擎·着色器