批量修改材质球名称及材质球上主贴图的名称

2017-03-25  本文已影响128人  木心Sepith

  //根据材质球重命名并重命名主贴图名字
    [MenuItem("Tools/ReNameFromMaterial")]
    public static void ReNameFromMaterial()
    {
        //获取到选中的材质球
        Object[] m_objects = Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets);




        int index = 0;




        foreach (Object item in m_objects)
        {
            if (Path.GetExtension(AssetDatabase.GetAssetPath(item)) != "")
            {
                if (item.GetType() != typeof(Material)) return;




                //获取到材质球
                Material m = (Material)item;




                //根据索引更改材质球的名称
                string itemPath = AssetDatabase.GetAssetPath(item);
                string itemNewName = "Mat_test00" + index;
                string s = AssetDatabase.RenameAsset(itemPath, itemNewName);


                //获取到材质球路径和材质球上所有的依赖关系
                string path = AssetDatabase.GetAssetPath(item);
                string[] strDepends = AssetDatabase.GetDependencies(path);
                int dependsIndex = ToolsReNameFromMaterial(strDepends);
                if (dependsIndex == -1)
                {
                    Debug.Log("未找到贴图");
                    index++;
                    continue;
                }
                
                //得到主贴图名称
                string textureNewName = "Tex_test00" + index;
                AssetDatabase.RenameAsset(strDepends[dependsIndex], textureNewName);


                //索引自增
                index++;
            }




        }




        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();


    }


    static int ToolsReNameFromMaterial(string[] strs)
    {
        for (int i = 0; i < strs.Length; i++)
        {
            Texture2D t = AssetDatabase.LoadAssetAtPath(strs[i], typeof(Texture2D)) as Texture2D;


            if (t != null)
            {
                return i;
            }
        }
        return -1;
    }
上一篇下一篇

猜你喜欢

热点阅读