当前位置: 首页 > news >正文

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

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


文章目录

  • Unity材质球自动遍历所需贴图
  • 一、原理
  • 二、用法
    • 1.代码:
    • 2.使用方法


一、原理

例如一个材质球名为: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.点右上角三个点,进行调用脚本中的方法。


http://www.lryc.cn/news/411691.html

相关文章:

  • C++那些事之结构化绑定
  • ECRS工时分析软件:工业工程精益生产的智慧引擎
  • 大语言模型的核心岗位及其要求
  • 【屏驱MCU】RT-Thread 文件系统接口解析
  • 进程管理工具top ps
  • 2年社招冲击字节,一天三面斩获offer
  • oppo,埃科光电25届秋招,快手25届技术人才专项计划等几千家企业岗位内推
  • 【Vulnhub系列】Vulnhub Lampiao-1 靶场渗透(原创)
  • MySQL:ORDER BY 排序查询
  • UML类图 详解
  • 【IEEE出版 | 高录用率 | 快速检索 | 有ISBN号!】2024年智能计算与数据挖掘国际学术会议 (ICDM 2024,9月20-22)
  • DaoCloud配置不同环境的流水线(Q)
  • 基础的Shell命令
  • 量子仿真speedUp的经验
  • 电测量数据交换DLMS∕COSEM组件第61部分:对象标识系统(OBIS)(下)
  • 【Java】重生之String类再爱我一次---练习题(012)
  • NSSCTF-GDOUCTF 2023新生赛
  • 论文解析——Character Region Awareness for Text Detection,字符级文本检测CRAFT算法
  • 基于飞腾平台的Kafka移植与安装
  • 【Python数据结构与算法】递归----算24
  • TOSHIBA东芝代理商--芯智雲城,提供订货、报价、选型等服务!
  • sdwan
  • Linux: network: 建立socket以及设置nonblock/opt所需的时间
  • git使用及代码规范
  • 职业教育大数据实验实训室建设应用案例
  • 【Academy】反序列化漏洞Insecure deserialization
  • 【轨物推荐】康波、世界体系与创新范式:中国如何引爆新一轮产业革命
  • [HCTF 2018]admin1
  • 【Qwen-Audio部署实战】Qwen-Audio-Chat模型之对话机器人部署测试
  • 第一百八十五节 Java XML教程 - Java DOM简介