批量设置材质球中Main Maps部分贴图包含 Albedo,Metallic,Nomap,Occlusion
脚本存放位置:放在在Editor目录之下
点击 window 之下的AddMaterialBall
资源存放位置:所有的材质球和贴图上一层或是上上一层必须有Resources 的一个文件夹
执行顺序,1 2 3 4 依次点击
选择材质球路径(选择只包含材质目录):
选择贴图路径(选择只包含材质贴图目录):
查看对应的名称:
点击“材质球对应贴图添加到字典中去”和“配置贴图给材质球”
材质球和贴图的明明规则:
Comm 材质球名字
Comm_A= Albedo
Comm_M= Metallic
Comm_N= Nomap
Comm_AO= Occlusion
现阶段工程限有功能
更新功能:
新功能就是能够选择你要添加贴图的对象,当然是所有的
写的比较简陋,粘贴过去就行😀
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEngine.UI;
public class AddMaterialBall : EditorWindow {
Dictionary<string, Material> Materials = new Dictionary<string, Material>();
Dictionary<string, Texture> Textures = new Dictionary<string, Texture>();
List<string> MatNameStr;
List<string> TexNameStr;
bool Albedo=false, Metallic = false, NormalMap = false, HeightMap = false, Occlusion = false, DetailMask = false;
Texture NewTexture = new Texture();
static string MatPath_k;//总路径
static string TextPath_k;
string MatPathNmae;//分割后路径
string TextPathName;
string[] TEXTURE_TYPE = { "_MetallicGlossMap", "_BumpMap", "_ParallaxMap", "_OcclusionMap", "_DetailMask", "_DetailAlbedoMap", "_DetailNormalMap" };
[MenuItem("Window/AddMaterialBall")]
static void Init()
{
AddMaterialBall abt = EditorWindow.GetWindow<AddMaterialBall>();
abt.Show();//显示窗口
Debug.Log("什么时候执行");
}
private void OnGUI()
{
Debug.Log("什么时候执行————");
GUILayout.BeginVertical();
GUILayout.Label("材质路径和贴图路径要在 Resources 之下");
GUILayout.Label("贴图图片限支持:.png .jpg");
if (GUILayout.Button("选择材质球路径(选择只包含材质目录)"))
{
//根据Unity Editor 内置接口,我们打开夹获取文件夹
MatPath_k = EditorUtility.OpenFolderPanel("选择要打包的路径", "", "");
if (MatPath_k.Length > 10)
{
MatPathNmae = MatPath_k.Substring(MatPath_k.LastIndexOf("Resources") + 10);
}
else
{
Debug.LogError("材质球路径不正确");
}
}
GUILayout.Label("材质球放置路径:" + MatPathNmae);
if (GUILayout.Button("选择贴图路径(选择只包含材质贴图目录)"))
{
TextPath_k = EditorUtility.OpenFolderPanel("选择要打包的路径", "", "");
TextPathName=TextPath_k.Substring(MatPath_k.LastIndexOf("Resources") + 10);
if (TextPath_k.Length > 10)
{
TextPathName = TextPath_k.Substring(MatPath_k.LastIndexOf("Resources") + 10);
}
else
{
Debug.LogError("贴图路径不正确");
}
}
GUILayout.Label("对应贴图放置路径:" + TextPathName);
if (GUILayout.Button("材质球对应贴图添加到字典中去"))
{
AddMateril(MatPath_k, MatPathNmae);
AddMateril_Text(TextPath_k, TextPathName);
Debug.Log("执行添加Bool逻辑");
foreach (var item in Textures.Keys)
{
Debug.Log("材质球名称"+Textures[item].name);
}
}
GUILayout.Label("选择你要添加贴图的位置:");
GUILayout.BeginHorizontal();
if (GUILayout.Button("Albedo")) { Albedo= true; }
if (GUILayout.Button("Metallic")) { Metallic= true; }
if (GUILayout.Button("NormalMap")) { NormalMap= true; }
if (GUILayout.Button("HeightMap")) { HeightMap= true; }
if (GUILayout.Button("Occlusion")) { Occlusion= true; }
if (GUILayout.Button("DetailMask")) { DetailMask = true; }
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("清除选择选项"))
{
Albedo = false;
Metallic = false;
NormalMap = false;
HeightMap = false;
Occlusion = false;
DetailMask = false;
Debug.Log("所有的的都选择false");
}
if (GUILayout.Button("全选所有选项")) {
Albedo = true;
Metallic = true;
NormalMap = true;
HeightMap = true;
Occlusion = true;
DetailMask = true;
Debug.Log("所有的的都选择true");
}
GUILayout.EndHorizontal();
if (GUILayout.Button("配置贴图给材质球"))
{
Interflow();
Debug.Log("执行输出");
}
GUILayout.EndVertical();
GUILayout.Label("谨慎使用: ");
GUILayout.Label("谨慎使用: ");
GUILayout.Label("谨慎使用: ");
if (GUILayout.Button("批量清除材质球上的贴图"))
{
ClearAllTexture();
Debug.Log("清理贴图");
}
}
//
public Dictionary<string, Material> AddMateril(string MatPath_k,string MatPathNmae)
{
MatNameStr = new List<string>();
Material material= AssetDatabase.LoadMainAssetAtPath(MatPath_k) as Material;
if (Directory.Exists(MatPath_k))
{
DirectoryInfo direction = new DirectoryInfo(MatPath_k);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".mat"))
{
string a = files[i].Name.Substring(0,files[i].Name.IndexOf("."));
MatNameStr.Add(a);
}
//Debug.Log("Name:" + files[i].Name);
//Debug.Log("FullName:" + files[i].FullName);
//Debug.Log("DirectoryName:" + files[i].DirectoryName);
}
//Material加载进字典中去
}
for (int i = 0; i < MatNameStr.Count; i++)
{
if (!Materials.ContainsKey(MatNameStr[i]))
{
Materials.Add(MatNameStr[i], Resources.Load<Material>(MatPathNmae + "/" + MatNameStr[i]));
}
Debug.Log(Materials[MatNameStr[i]].name+":材质球加载出的名字");
}
return Materials;
}
public Dictionary<string, Texture> AddMateril_Text(string MatPath_k,string TextPathName)
{
TexNameStr = new List<string>();
Material material = AssetDatabase.LoadMainAssetAtPath(MatPath_k) as Material;
if (Directory.Exists(MatPath_k))
{
DirectoryInfo direction = new DirectoryInfo(MatPath_k);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".png") || files[i].Name.EndsWith(".jpg"))
{
string a = files[i].Name.Substring(0, files[i].Name.IndexOf("."));
// Debug.Log("名字" + a);
TexNameStr.Add(a);
}
//Debug.Log("Name:" + files[i].Name);
//Debug.Log("FullName:" + files[i].FullName);
//Debug.Log("DirectoryName:" + files[i].DirectoryName);
}
}
for (int i = 0; i < TexNameStr.Count; i++)
{
if (!Textures.ContainsKey(TexNameStr[i]))
{
Textures.Add(TexNameStr[i], Resources.Load<Texture>(TextPathName + "/" + TexNameStr[i]));
}
Debug.Log(TexNameStr[i] + ":材质贴图加载出的名字");
}
return Textures;
}
public void Interflow()
{
for (int i = 0; i < MatNameStr.Count; i++)
{
if (Albedo)
{
if (TexNameStr.Contains(MatNameStr[i] + "_A"))
{
Materials[MatNameStr[i]].mainTexture = Textures[MatNameStr[i] + "_A"];
}
else
{
Debug.Log("不包含在其中");
}
}
if (Metallic)
{
if (TexNameStr.Contains(MatNameStr[i] + "_M"))
{
Materials[MatNameStr[i]].SetTexture(TEXTURE_TYPE[0], Textures[MatNameStr[i] + "_M"]);
}
else
{
Debug.Log("不包含在其中");
}
}
if (NormalMap)
{
if (TexNameStr.Contains(MatNameStr[i] + "_N"))
{
Materials[MatNameStr[i]].SetTexture(TEXTURE_TYPE[1], Textures[MatNameStr[i] + "_N"]);
}
else
{
Debug.Log("不包含在其中");
}
}
if (HeightMap)
{
if (TexNameStr.Contains(MatNameStr[i] + "_H"))
{
Materials[MatNameStr[i]].SetTexture(TEXTURE_TYPE[2], Textures[MatNameStr[i] + "_H"]);
}
else
{
Debug.Log("不包含在其中");
}
}
if (Occlusion)
{
if (TexNameStr.Contains(MatNameStr[i] + "_AO"))
{
Materials[MatNameStr[i]].SetTexture(TEXTURE_TYPE[3], Textures[MatNameStr[i] + "_AO"]);
}
else
{
Debug.Log("不包含在其中");
}
}
if (DetailMask)
{
if (TexNameStr.Contains(MatNameStr[i] + "_De"))
{
Materials[MatNameStr[i]].SetTexture(TEXTURE_TYPE[4], Textures[MatNameStr[i] + "_De"]);
}
else
{
Debug.Log("不包含在其中");
}
}
}
Debug.Log("执行完成");
}
/// <summary>
/// 清除所有的贴图
/// </summary>
public void ClearAllTexture()
{
for (int i = 0; i < MatNameStr.Count; i++)
{
Materials[MatNameStr[i]].mainTexture = NewTexture;
for (int j = 0; j < TEXTURE_TYPE.Length; j++)
{
Materials[MatNameStr[i]].SetTexture(TEXTURE_TYPE[j], NewTexture);
}
}
}
}