0
点赞
收藏
分享

微信扫一扫

Unity导入模型和材质丢失问题处理

何以至千里 2022-02-15 阅读 88

在unity资源商店买的模型包,导入unity后,全部材质丢失,稀里哗啦乱成一片
在网上搜了下材质丢失的办法,结果都是一个一个手动操作
窝草,资源包里接近两万个资产,手动不是要撸的灰飞烟灭
遂自制自动解决材质丢失办法
如下,直接上代码


# if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Custom_Tools
{
    public class UnifyMaterial
    {
        private static string shaderMode = "Standard";

        [MenuItem("Assets/自定义工具/改变选中文件夹下所有材质为Standard")]
        /// <summary>
        /// 改变选中文件夹下所有材质为标准的
        /// </summary>
        public static void ChangeMaterialsMode()
        {
            string rootpath =Tools_File_Debug_Name.GetSelectFilePath();
            foreach (var path in Tools_File_Debug_Name.GetAssetsPathWithFormat(rootpath, "mat"))
            {
                modificationMaterialMode(path);
            }
        }

        [MenuItem("Assets/自定义工具/统一模型材质名称")]
        /// <summary>
        /// 改变选中文件夹下所有材质为标准的
        /// </summary>
        public static void click1()
        {
            string rootpath = Tools_File_Debug_Name.GetSelectFilePath();
            foreach (var path in Tools_File_Debug_Name.GetAssetsPathWithFormat(rootpath, "fbx"))
            {
                modificationFBX_Name(path);
            }
              foreach (var path in Tools_File_Debug_Name.GetAssetsPathWithFormat(rootpath, "FBX"))
            {
                modificationFBX_Name(path);
            }
            AssetDatabase.Refresh();
        }

        public static void modificationFBX_Name(string path)
        {
            if (string.IsNullOrEmpty(path))
                return;
            ModelImporter mode = (ModelImporter)ModelImporter.GetAtPath(path);

            if (mode !=null )
            {
                mode.materialName = ModelImporterMaterialName.BasedOnModelNameAndMaterialName;
                AssetDatabase.ImportAsset(path);
            }
            else
            {
                Debug.LogError("设置模型材质名称失败 : "+path );
            }
        }

        /// <summary>
        /// 根据路径修改材质为 Standard
        /// </summary>
        /// <param name="path">路径为Assets开头</param>
        public static void modificationMaterialMode(string path)
        {
            if (string.IsNullOrEmpty(path))
                return;
            Material material = AssetDatabase.LoadAssetAtPath<Material>(path);
            if (material == null)
            {
                Debug.Log(path);
            }
            else
            {
                material.shader = Shader.Find(shaderMode);                
            }
        }
    }
}

/********************************************************
     文件: Tools.cs
     作者: 阿飞
     日期: CreateTime
     寄语: 虎年 虎虎生威  大吉大利
     功能: 在控制台输出文件名称
*********************************************************/

using System.Collections.Generic;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace Custom_Tools
{
    public class Tools_File_Debug_Name
    {
#if UNITY_EDITOR
        [MenuItem("Assets/自定义工具/当前选择文件/选中文件/显示路径")]
        public static void click1()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            Debug.Log(GetSelectFilePath());
        }

        [MenuItem("Assets/自定义工具/当前选择文件/子文件/子文件名称/名称/所有")]
        public static void click2()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            ShowFileName(GetSelectFilePath());
        }

        [MenuItem("Assets/自定义工具/当前选择文件/子文件/子文件名称/名称/mat")]
        public static void click3()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            ShowFileName(GetSelectFilePath(), "mat");
        }

        [MenuItem("Assets/自定义工具/当前选择文件/子文件/子文件名称/名称/fbx")]
        public static void click4()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            ShowFileName(GetSelectFilePath(), "fbx");
        }

        [MenuItem("Assets/自定义工具/当前选择文件/子文件/子文件名称/路径/所有")]
        public static void click5()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            ShowFilePathName(GetSelectFilePath());
        }

        [MenuItem("Assets/自定义工具/当前选择文件/子文件/子文件名称/路径/fbx")]
        public static void click6()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            ShowFilePathName(GetSelectFilePath(), "fbx");
        }

        [MenuItem("Assets/自定义工具/当前选择文件/子文件/子文件名称/路径/mat")]
        public static void click7()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            ShowFilePathName(GetSelectFilePath(), "mat");
        }

        [MenuItem("Assets/自定义工具/当前选择文件/子文件/子文件名称/目录路径/mat/Assets路径")]
        public static void click8()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            ShowFileDirectoryName(GetSelectFilePath(), "mat");
        }

        [MenuItem("Assets/自定义工具/获取所有格式为mat文件相对Assets的路径")]
        public static void click9()
        {
            Debug.Log(string.Format("<color=#FFC0CB>   ==>>   </color>跳转到代码逻辑区域"));
            foreach (var item in GetAssetsPathWithFormat(GetSelectFilePath(), "mat"))
            {
                Debug.Log(item);
            }
        }

        /// <summary>
        /// 显示当前选择asset窗口中选择的文件的路径
        /// </summary>
        /// <returns></returns>
        public static string GetSelectFilePath()
        {
            UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.TopLevel);
            return (Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[0]));
        }
#endif



        /// <summary>
        /// 输出指定文件夹下所有文件的名称,后缀.beta除外
        /// </summary>
        /// <param name="path"></param>
        public static void ShowFileName(string path)
        {
            //获取指定路径下面的所有资源文件

            if (Directory.Exists(path))

            {
                DirectoryInfo direction = new DirectoryInfo(path);

                FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

                //Debug.Log(string.Format("<color=green>文件数量 : {0}</color>", files.Length));

                for (int i = 0; i < files.Length; i++)

                {
                    if (files[i].Name.EndsWith(".meta"))

                    {
                        continue;
                    }

                    //输出文件名称
                    //Debug.Log("Name:" + files[i].Name);
                    Debug.Log(string.Format("<color=yellow>{0}</color>", files[i].Name));

                    //Debug.Log("FullName:" + files[i].FullName);

                    //Debug.Log("DirectoryName:" + files[i].DirectoryName);

                }
            }
        }

        /// <summary>
        /// 输出指定文件夹下所有文件的名称,后缀.beta除外
        /// </summary>
        /// <param name="path"></param>
        public static void ShowFileDirectoryName(string path, string end_format)
        {
            //获取指定路径下面的所有资源文件

            if (Directory.Exists(path))

            {
                DirectoryInfo direction = new DirectoryInfo(path);

                FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

               // Debug.Log(string.Format("<color=green>文件数量 : {0}</color>", files.Length));

                for (int i = 0; i < files.Length; i++)

                {
                    if (files[i].Name.EndsWith("." + end_format))

                    {
                        Debug.Log(string.Format("<color=yellow>{0}</color>", files[i].DirectoryName));
                    }
                }
            }
        }

        /// <summary>
        /// 输出指定文件夹下所有文件完整的路径名称,后缀.beta除外
        /// </summary>
        /// <param name="path"></param>
        public static void ShowFilePathName(string path)
        {
            //获取指定路径下面的所有资源文件

            if (Directory.Exists(path))

            {
                DirectoryInfo direction = new DirectoryInfo(path);

                FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

                //Debug.Log(string.Format("<color=green>文件数量 : {0}</color>", files.Length));

                for (int i = 0; i < files.Length; i++)

                {
                    if (files[i].Name.EndsWith(".meta"))

                    {
                        continue;
                    }

                    //输出文件名称
                    //Debug.Log("Name:" + files[i].Name);
                    Debug.Log(string.Format("<color=yellow>{0}</color>", files[i].FullName));

                    //Debug.Log("DirectoryName:" + files[i].DirectoryName);

                }
            }
        }


        /// <summary>
        /// 输出指定文件夹下指定后缀格式的文件名称
        /// </summary>
        /// <param name="path"></param>
        public static void ShowFileName(string path, string end_format)
        {
            //获取指定路径下面的所有资源文件

            if (Directory.Exists(path))
            {
                DirectoryInfo direction = new DirectoryInfo(path);

                FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

               // Debug.Log(string.Format("<color=green>文件数量 : {0}</color>", files.Length));

                for (int i = 0; i < files.Length; i++)

                {
                    if (files[i].Name.EndsWith("." + end_format))

                    {
                        Debug.Log(string.Format("<color=yellow>{0}</color>", files[i].Name));
                    }
                }
            }
        }

        /// <summary>
        /// 输出指定文件夹下指定后缀格式的完整文件名称
        /// </summary>
        /// <param name="path"></param>
        public static void ShowFilePathName(string path, string end_format)
        {
            //获取指定路径下面的所有资源文件

            if (Directory.Exists(path))

            {
                DirectoryInfo direction = new DirectoryInfo(path);

                FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

               // Debug.Log(string.Format("<color=green>文件数量 : {0}</color>", files.Length));

                for (int i = 0; i < files.Length; i++)

                {
                    if (files[i].Name.EndsWith("." + end_format))

                    {
                        Debug.Log(string.Format("<color=yellow>{0}</color>", files[i].FullName));
                    }
                }
            }
        }

        /// <summary>
        /// 按指定格式获得相对某文件夹下所有格式的路径
        /// </summary>
        /// <param name="rootpath"></param>
        /// <param name="endformat"></param>
        /// <returns></returns>
        public static List<string> GetAssetsPathWithFormat(string rootpath, string endformat)
        {
            //获取指定路径下面的所有资源文件

            if (Directory.Exists(rootpath))
            {
                DirectoryInfo direction = new DirectoryInfo(rootpath);

                FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);

                List<string> result = new List<string>();

                for (int i = 0; i < files.Length; i++)
                {                    
                    if (files[i].Name.EndsWith("." + endformat))
                    {
                        result.Add ( ConvertToAssetsPath(files[i].FullName));
                    }
                }
                Debug.Log(string.Format("<color=green>文件数量 : {0}</color>", result.Count ));
                return result;
            }
            return null;
        }

        /// <summary>
        /// 将决对路径转换为相对Assets的相对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ConvertToAssetsPath(string path)
        {
            string newpath = "";
            string[] roots = path.Split('\\');

            bool isadd = false;          

            for (int i = 0; i < roots.Length; i++)
            {

                if (roots[i].Equals("Assets") && isadd == false)
                {
                    isadd = true;
                }
                if (isadd)
                {
                    if (i==roots.Length -1)
                    {
                        newpath += roots[i];
                    }
                    else
                    {
                        newpath += roots[i] + "/";
                    }                    
                }

            }
            return newpath;
        }


    }
}

用法很简单粗暴
右键点击选着要更改的文件夹,

点击自定义工具==>改变选中文件夹下所有材质为Standard

点击自定义工具==>统一模型材质名称

ok
反正我是解决了

举报

相关推荐

0 条评论