Unity导入模型和材质丢失问题处理(大批量操作)

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

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

点击 easywork==>改变选中文件夹下所有材质为Standard

点击 easywork==>统一模型材质名称
如下,直接上代码

/********************************************************
     文件: 找回丢失材质.cs
     作者: 阿飞
     日期: CreateTime
     寄语: 虎年 虎虎生威  大吉大利
     功能: Nothing
*********************************************************/



using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;


public class 找回丢失材质
{
    private static string shaderMode = "Standard";

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

    [MenuItem("Assets/阿飞的EasyWork/统一模型材质名称")]
    /// <summary>
    /// 改变选中文件夹下所有材质为标准的
    /// </summary>
    public static void click1()
    {
        string rootpath = GetSelectFilePath();
        foreach (var path in GetAssetsPathWithFormat(rootpath, "fbx"))
        {
            modificationFBX_Name(path);
        }
        foreach (var path in 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);
        }
    }
    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]));
    }
    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;
    }

    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;
    }
}

ok
反正我是解决了

对于UE4静态模型材质重载后打包后丢失问题,您可以尝试以下解决方法: 1. 检查材质引用:首先,请确保您的静态模型在编辑器中正确地引用了材质。在UE4中,材质是通过引用关联到模型的,如果引用的材质发生了变化或丢失,那么在打包后模型可能无法正确地显示材质。请确保材质引用是正确的,并且没有被意外更改。 2. 检查材质实例化:在UE4中,材质可以通过实例化来应用到模型上。请确保您的静态模型上的材质实例化步骤是正确的,并且没有被意外更改。如果您使用了蓝图或者代码来处理材质实例化,也请检查相关逻辑是否正确。 3. 检查打包设置:在进行打包操作时,请确保您已经正确地设置了打包选项。有时候,错误的打包设置可能导致某些材质无法正确地被打包进最终的可执行文件中。请确保您已经选择了正确的打包配置,并且没有禁用或者排除了相关的材质文件。 4. 清理和重新构建:有时候,编译缓存或者旧的数据文件可能会导致材质丢失问题。您可以尝试清理项目的缓存并重新构建项目,以确保最新的材质数据被正确地包含在打包后的文件中。 如果以上方法都没有解决您的问题,我建议您检查UE4的日志文件,以获取更详细的错误信息。您也可以尝试在UE4的开发模式下进行调试,以查找可能的问题所在。 希望以上解决方案能够帮助您解决问题!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值