注释以后有空再写吧,加班到现在还没吃饭呢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
/// <summary>
/// 根据材质球上的主贴图名字进行改为同名的其他后缀的贴图,并且加上法线贴图
/// </summary>
public class Replace
{
[MenuItem("Tools/replace")]
public static void Rename()
{
Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);//选择的所以对象
foreach (Object item in m_objects)
{
string path = AssetDatabase.GetAssetPath(item);
Debug.Log("材质球的位置:");
Debug.Log(path);
if (Path.GetExtension(path) != "")//判断路径是否为空
{
if (item.GetType() == typeof(Material))
{
Material m = (Material)item;
path = AssetDatabase.GetAssetPath(m.mainTexture);
Debug.Log("材质球上主贴图的位置:");
Debug.Log(path);
string[] strs = path.Split('.');
string afterpath = strs[0] + ".jpg";
Debug.Log("材质球上主贴图相应后缀的位置:");
Debug.Log(afterpath);
if (AssetDatabase.LoadAssetAtPath<Texture>(afterpath))
m.mainTexture = AssetDatabase.LoadAssetAtPath<Texture>(afterpath);
string strNormalMap = strs[0] + "_NRM.jpg";
Texture texturebump = AssetDatabase.LoadAssetAtPath<Texture>(strNormalMap);
if (texturebump != null)
m.SetTexture("_BumpMap", texturebump);
}
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("Complete!");
}
}