Unity材质球自动遍历所需贴图
创始人
2024-12-18 01:06:25
0

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 targetMaterials; // 在Inspector中指定目标材质列表     private Dictionary textureMapNames = new Dictionary     {         { "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(assetPath);         }         return null;     } }  

2.使用方法

在这里插入图片描述

1.将脚本挂载到一个空物体:
2.把所需的材质球添加到集合列表中。
3.点右上角三个点,进行调用脚本中的方法。


相关内容

热门资讯

一分钟教你!广东雀神外 挂(一... 一分钟教你!广东雀神外 挂(一贯真的是有挂)详细透视辅助教程1.广东雀神外 挂 ai辅助创建新账号,...
微扑克辅助器ios!微扑克网页... 微扑克辅助器ios!微扑克网页版辅助,微扑克真的有挂存在(都是真的是有挂);无聊就玩这款微扑克真的有...
重大来袭!都莱罗松(本来真的是... 重大来袭!都莱罗松(本来真的是有挂)详细透视辅助教程1、打开软件启动之后找到中间准星的标志长按。2、...
微扑克辅助软件!微扑克有挂(透... 微扑克辅助软件!微扑克有挂(透明挂)好像是有挂1、微扑克系统规律教程、微扑克辅助透视等服务,为用户提...
玩家必知教程!金州水鱼辅助工具... 玩家必知教程!金州水鱼辅助工具(一贯是真的有挂)详细辅助教程所有人都在同一条线上,像星星一样排成一排...
最新技巧!!福建众娱软件有没有... 最新技巧!!福建众娱软件有没有辅助(透明挂)透明挂透视辅助脚本(2023已更新)(哔哩哔哩);福建众...
科普分享!心悦填大坑总输怎么回... 科普分享!心悦填大坑总输怎么回事(确实有挂)详细透视辅助教程1、心悦填大坑总输怎么回事系统规律教程、...
分享认知!柳州天天爱麻将有挂吗... 分享认知!柳州天天爱麻将有挂吗(透视)透明挂透视辅助脚本(2023已更新)(哔哩哔哩);1、金币登录...
记者揭秘!衢州都莱十三道辅助器... 记者揭秘!衢州都莱十三道辅助器(切实是有挂)详细辅助教程1、衢州都莱十三道辅助器ai机器人多个强度级...
玩家必知教程!!卡农斗牛辅助最... 玩家必知教程!!卡农斗牛辅助最新版本(透视)透视脚本辅助插件(2021已更新)(哔哩哔哩)运卡农斗牛...