網(wǎng)站域名備案誰來做太原seo顧問
Unity材質(zhì)球自動遍歷所需貼圖
文章目錄
- Unity材質(zhì)球自動遍歷所需貼圖
- 一、原理
- 二、用法
- 1.代碼:
- 2.使用方法
一、原理
例如一個材質(zhì)球名為:Decal_Text_Cranes_01_Mat ,
然后從全局遍歷出:Decal_Text_Cranes_01_Albedo賦值給材質(zhì)球的BaseMap,
全局遍歷出Decal_Text_Cranes_01_MAODS 賦值給材質(zhì)球MetallicMap通道,
全局遍歷出Decal_Text_Cranes_01_Normal 給材質(zhì)球NormalMap通道,
**規(guī)律:**材質(zhì)球名字:Decal_Text_Cranes_01_Mat 把后面Mat換成通道名稱,就是該材質(zhì)球的通道貼圖
二、用法
1.代碼:
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEditor;public class AutoAssignTextureMaps : MonoBehaviour
{public List<Material> targetMaterials; // 在Inspector中指定目標(biāo)材質(zhì)列表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.把所需的材質(zhì)球添加到集合列表中。
3.點右上角三個點,進(jìn)行調(diào)用腳本中的方法。