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


相关推荐
心之所向,自强不息3 小时前
【Unity Shader编程】之让画面动起来
unity·游戏引擎
不伤欣1 天前
游戏设计模式 - 子类沙箱
游戏·unity·设计模式
快乐觉主吖1 天前
Unity的日志管理类
android·unity·游戏引擎
WarPigs2 天前
Unity性能优化笔记
笔记·unity·游戏引擎
T.D.C2 天前
【业务框架】3C-相机-Cinemachine
unity
Tiger Z2 天前
R 语言科研绘图第 55 期 --- 网络图-聚类
开发语言·r语言·贴图
高德技术2 天前
全流程开源!高德3D贴图生成系统,白模一键生成真实感纹理贴图
3d·贴图
Clank的游戏栈2 天前
Unity基于GraphView的可视化关卡编辑器开发指南
unity·编辑器·游戏引擎
海尔辛3 天前
Unity UI 性能优化--Sprite 篇
ui·unity·性能优化
技术小甜甜3 天前
【Blender Texture】【游戏开发】高质感 Blender 4K 材质资源推荐合集 —— 提升场景真实感与美术表现力
blender·游戏开发·材质·texture