栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

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

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

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




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


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

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

    [MenuItem("Assets/阿飞的EasyWork/统一模型材质名称")]
    /// 
    /// 改变选中文件夹下所有材质为标准的
    /// 
    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);
        }
    }

    /// 
    /// 根据路径修改材质为 Standard
    /// 
    /// 路径为Assets开头
    public static void modificationMaterialMode(string path)
    {
        if (string.IsNullOrEmpty(path))
            return;
        Material material = AssetDatabase.LoadAssetAtPath(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 GetAssetsPathWithFormat(string rootpath, string endformat)
    {
        //获取指定路径下面的所有资源文件

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

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

            List result = new List();

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith("." + endformat))
                {
                    result.Add(ConvertToAssetsPath(files[i].FullName));
                }
            }
            Debug.Log(string.Format("文件数量 : {0}", 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
反正我是解决了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/897790.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号