【每天一个Unity技巧】批量修改材质球的主贴图和添加法线贴图

注释以后有空再写吧,加班到现在还没吃饭呢

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!");
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容