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


相关推荐
lrh30254 小时前
Custom SRP 12 - HDR
3d·unity·srp·render pipeline
霜绛5 小时前
Unity:Json笔记——Json文件格式、JsonUtlity序列化和反序列化
学习·unity·json·游戏引擎
阿幸软件杂货间12 小时前
Blender硬面建模灯光渲染材质修改器纹理烘焙插件 Rantools And P-Cutter All-In-One Addon V3.3.10
blender·材质
TYayyyyy16 小时前
unity 事件、委托
unity
L X..17 小时前
Unity反射调用 ReactiveProperty<T>(泛型类型)内部方法时崩溃
unity·c#·游戏引擎·.net
开发游戏的老王17 小时前
虚幻引擎虚拟制片入门教程 之 3D渲染基础知识:模型、材质、贴图、UV等
3d·虚幻·材质·模型·着色器·uv
向宇it1 天前
【推荐100个unity插件】将您的场景渲染为美丽的冬季风景——Global Snow 2
unity·游戏引擎·风景
浅丿忆十一1 天前
关于unity一个场景中存在多个相机时Game视图的画面问题
unity·游戏引擎
WLJT1231231231 天前
方寸之间见天地:新兴高端印章的当代破局与价值重构
unity·游戏引擎
软件黑马王子1 天前
2025Unity中的核心数学工具(三)四元数(穿插Unity实战相关案例)
unity·游戏引擎