143 lines
6.3 KiB
C#
143 lines
6.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
using System.IO;
|
|
|
|
public class Tool : EditorWindow
|
|
{
|
|
[SerializeField]
|
|
private VisualTreeAsset m_VisualTreeAsset = default;
|
|
|
|
[MenuItem("Tool/工具箱")]
|
|
public static void ShowExample()
|
|
{
|
|
Tool wnd = GetWindow<Tool>();
|
|
wnd.titleContent = new GUIContent("工具箱");
|
|
}
|
|
|
|
//private string[] _materialsPath;
|
|
List<string> _materialFolders = new List<string>();
|
|
public void CreateGUI()
|
|
{
|
|
VisualElement root = rootVisualElement;
|
|
VisualElement labelFromUXML = m_VisualTreeAsset.Instantiate();
|
|
labelFromUXML.Q<Button>().clicked += ()=>TextureAutoUse(labelFromUXML.Q<TextField>("Color").value,
|
|
labelFromUXML.Q<TextField>("Normal").value,
|
|
labelFromUXML.Q<TextField>("Meta").value,
|
|
labelFromUXML.Q<TextField>("AO").value,
|
|
labelFromUXML.Q<TextField>("Emi").value,
|
|
labelFromUXML.Q<TextField>("Texture").value);
|
|
root.Add(labelFromUXML);
|
|
}
|
|
//检测路径为材质球自动赋予贴图
|
|
void TextureAutoUse(string colorSuf,string normSuf,string metaSuf,string aoSuf,string emiSuf,string texturePre)
|
|
{
|
|
float assignedCount = 0;
|
|
//获取所有材质球路径
|
|
string projectPath=Application.dataPath;
|
|
var materialDirs= Directory.GetDirectories(projectPath, "Materials", SearchOption.AllDirectories);
|
|
foreach (var dir in materialDirs)
|
|
{
|
|
string relativePath= "Assets"+dir.Substring(projectPath.Length);
|
|
_materialFolders.Add(relativePath);
|
|
}
|
|
|
|
foreach (var materialPath in _materialFolders)
|
|
{
|
|
string texturesPath = materialPath.Replace("Materials", "Textures");
|
|
if (Directory.Exists(texturesPath))
|
|
{
|
|
|
|
//"t:" 按类型搜索 "t:Material"
|
|
//"l:" 按标签搜索 "l:Environment"
|
|
//"n:" 按名称搜索 "n:PlayerModel"
|
|
//"p:" 按路径搜索 "p:Assets/UI"
|
|
string[] texturesGuids=AssetDatabase.FindAssets("t:Texture2D",new string[]{texturesPath});
|
|
string[] materialsGuids=AssetDatabase.FindAssets("t:Material",new string[]{materialPath});
|
|
|
|
List<Texture2D> textures = new List<Texture2D>();
|
|
List<Material> materials = new List<Material>();
|
|
//获取所有贴图
|
|
foreach (var guid in texturesGuids)
|
|
{
|
|
//获取贴图的相对路径
|
|
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
|
Texture2D texture= AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
|
|
textures.Add(texture);
|
|
//Debug.Log(texture.name);
|
|
}
|
|
//获取所有材质
|
|
foreach (var guid in materialsGuids)
|
|
{
|
|
//获取材质的相对路径
|
|
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
|
Material material= AssetDatabase.LoadAssetAtPath<Material>(assetPath);
|
|
materials.Add(material);
|
|
}
|
|
//匹配材质和贴图
|
|
foreach (var material in materials)
|
|
{
|
|
Texture2D colorTex= System.Array.Find(textures.ToArray(), (Texture2D texture) => { return texture.name.StartsWith(texturePre+material.name) && texture.name.EndsWith(colorSuf);});
|
|
Texture2D normTex= System.Array.Find(textures.ToArray(), (Texture2D texture) => { return texture.name.StartsWith(texturePre+material.name) && texture.name.EndsWith(normSuf);});
|
|
Texture2D metaTex= System.Array.Find(textures.ToArray(), (Texture2D texture) => { return texture.name.StartsWith(texturePre+material.name) && texture.name.EndsWith(metaSuf);});
|
|
Texture2D aoTex= System.Array.Find(textures.ToArray(), (Texture2D texture) => { return texture.name.StartsWith(texturePre+material.name) && texture.name.EndsWith(aoSuf);});
|
|
Texture2D emiTex= System.Array.Find(textures.ToArray(), (Texture2D texture) => { return texture.name.StartsWith(texturePre+material.name) && texture.name.EndsWith(emiSuf);});
|
|
|
|
if (colorTex != null)
|
|
{
|
|
material.mainTexture = colorTex;
|
|
textures.Remove(colorTex);
|
|
}
|
|
|
|
if (normTex != null)
|
|
{
|
|
material.SetTexture("_BumpMap",normTex);
|
|
textures.Remove(normTex);
|
|
}
|
|
|
|
if (metaTex != null)
|
|
{
|
|
material.SetTexture("_MetallicGlossMap",metaTex);
|
|
textures.Remove(metaTex);
|
|
}
|
|
|
|
if (aoTex != null)
|
|
{
|
|
material.SetTexture("_OcclusionMap",aoTex);
|
|
textures.Remove(aoTex);
|
|
}
|
|
if (emiTex != null)
|
|
{
|
|
material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;
|
|
material.SetColor("_EmissionColor", Color.white );
|
|
material.EnableKeyword("_EMISSION");
|
|
material.SetTexture("_EmissionMap",emiTex);
|
|
textures.Remove(emiTex);
|
|
}
|
|
|
|
EditorUtility.SetDirty(material);
|
|
|
|
}
|
|
}
|
|
|
|
assignedCount += 1;
|
|
EditorUtility.DisplayProgressBar("处理中", $"正在赋值路径: {materialPath}", (float)assignedCount / _materialFolders.Count);
|
|
}
|
|
AssetDatabase.SaveAssets();
|
|
EditorUtility.ClearProgressBar();
|
|
_materialFolders.Clear();
|
|
|
|
}
|
|
//保存生成的Texture到项目中
|
|
void SaveTextureToProject(Texture2D texture,string path)
|
|
{
|
|
|
|
byte[] bytes = texture.EncodeToPNG();
|
|
File.WriteAllBytes(path, bytes);
|
|
#if UNITY_EDITOR
|
|
AssetDatabase.Refresh();
|
|
#endif
|
|
}
|
|
}
|