美文网首页
批量修改材质球名称及材质球上主贴图的名称

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

作者: 木心Sepith | 来源:发表于2017-03-25 13:55 被阅读128次
    
      //根据材质球重命名并重命名主贴图名字
        [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;
        }
    

    相关文章

      网友评论

          本文标题:批量修改材质球名称及材质球上主贴图的名称

          本文链接:https://www.haomeiwen.com/subject/vhrcottx.html