当前位置: 代码网 > it编程>游戏开发>unity > Unity材质球自动遍历所需贴图

Unity材质球自动遍历所需贴图

2024年08月03日 unity 我要评论
*规律:**材质球名字:Decal_Text_Cranes_01_Mat 把后面Mat换成通道名称,就是该材质球的通道贴图。全局遍历出Decal_Text_Cranes_01_MAODS 赋值给材质球MetallicMap通道,然后从全局遍历出:Decal_Text_Cranes_01_Albedo赋值给材质球的BaseMap,全局遍历出Decal_Text_Cranes_01_Normal 给材质球NormalMap通道,例如一个材质球名为:Decal_Text_Cranes_01_Mat ,

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

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


(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com