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


相关推荐
★YUI★19 小时前
学习游制作记录(背包UI以及各种物品的存储)8.12
学习·游戏·ui·unity·c#
☆平常心☆21 小时前
Unity数据可视化图表插件XCharts
unity·信息可视化
21 小时前
Unity 遮挡显示效果 Shader
unity·游戏引擎
SmalBox1 天前
【渲染流水线】[几何阶段]-[曲面细分]以UnityURP为例
unity·渲染
渲吧-云渲染1 天前
破译真实感:渲染参数进阶指南——告别塑料感,唤醒材质生命力
材质
向宇it1 天前
【unity实战】在Unity中实现不规则模型的网格建造系统(附项目源码)
游戏·3d·unity·c#·游戏引擎
郝学胜-神的一滴2 天前
Horse3D引擎研发笔记(四):在QtOpenGL下仿three.js,封装EBO绘制四边形
c++·3d·unity·游戏引擎·godot·图形渲染·虚幻
郝学胜-神的一滴2 天前
游戏引擎(Unreal Engine、Unity、Godot等)大对比:选择最适合你的工具
程序人生·unity·游戏引擎·godot·虚幻·unreal engine
玩代码2 天前
Unity插件DOTween使用
unity·游戏引擎