注釋以后有空再寫(xiě)吧,加班到現(xiàn)在還沒(méi)吃飯呢
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
/// <summary>
/// 根據(jù)材質(zhì)球上的主貼圖名字進(jìn)行改為同名的其他后綴的貼圖专缠,并且加上法線貼圖
/// </summary>
public class Replace
{
[MenuItem("Tools/replace")]
public static void Rename()
{
Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);//選擇的所以對(duì)象
foreach (Object item in m_objects)
{
string path = AssetDatabase.GetAssetPath(item);
Debug.Log("材質(zhì)球的位置:");
Debug.Log(path);
if (Path.GetExtension(path) != "")//判斷路徑是否為空
{
if (item.GetType() == typeof(Material))
{
Material m = (Material)item;
path = AssetDatabase.GetAssetPath(m.mainTexture);
Debug.Log("材質(zhì)球上主貼圖的位置:");
Debug.Log(path);
string[] strs = path.Split('.');
string afterpath = strs[0] + ".jpg";
Debug.Log("材質(zhì)球上主貼圖相應(yīng)后綴的位置:");
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!");
}
}