欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity 批量导入模型

程序员文章站 2022-06-11 10:50:20
...

 

1.获取模型的路径和DirectoryInfo信息

   /// <summary>
        /// 获取所有模型文件
        /// </summary>
        public void GetAllModelFile()
        {

            DirectoryInfo dir = new DirectoryInfo(@"F:\C#工具\模型导入\ModelConsole\ModelConsole\Model\");
            DirectoryInfo[] dirs = dir.GetDirectories();
            foreach (DirectoryInfo d in dirs)
            {
                string name = d.Name.Split('_')[1].Split('(')[0].Split('(')[0];//删除多余字符
                dirInfoData.Add(name, d);
            }
        }

2.获取目录中FBX文件

 /// <summary>
        /// 获取模型文件夹FBX
        /// </summary>
        public void GetAllModelFBX(Dictionary<string, DirectoryInfo> dirs)
        {
            foreach (var item in dirs)
            {
                FileInfo[] fileInfos = item.Value.GetFiles();//获取文件
                foreach (var file in fileInfos)
                {
                    string format = file.Name.Split(".")[1];
                    if (format == "FBX" || format == "fbx")
                    {
                        fileInfoData.Add(item.Key, file);
                    }
                }
            }
        }

3.将模型导入指定Unity项目文件夹下

        /// <summary>
        /// 模型导入文件
        /// </summary>
        public void ModelIntoFile(string path)
        {
            if (Directory.Exists(path))
            {
                Console.WriteLine("开始导入FBX文件");
                foreach (var item in fileInfoData)
                {
                    string intoPath = path + "/" + item.Key;
                    if (!Directory.Exists(intoPath))//判断文件夹是否存在
                    {
                        Directory.CreateDirectory(intoPath);

                        item.Value.CopyTo(intoPath + "/" + item.Key + ".FBX", true);

                        Console.WriteLine(intoPath + "导入成功");
                    }
                    else
                    {
                        Console.WriteLine(intoPath + "文件夹已存在");
                    }
                }
            }
            else
            {
                Console.WriteLine(path + "目录不存在");
                ModelIntoFile(path);//如果输入错误一直递归好了
            }
        }

4.导入模型整体代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ModelConsole.Controller
{
    /// <summary>
    /// 导入模型
    /// </summary>
    class IntoModel
    {

        public Dictionary<string, DirectoryInfo> dirInfoData = new Dictionary<string, DirectoryInfo>();
        private Dictionary<string, FileInfo> fileInfoData = new Dictionary<string, FileInfo>();

        /// <summary>
        /// 获取所有模型文件
        /// </summary>
        public void GetAllModelFile()
        {

            DirectoryInfo dir = new DirectoryInfo(@"F:\C#工具\模型导入\ModelConsole\ModelConsole\Model\");
            DirectoryInfo[] dirs = dir.GetDirectories();
            foreach (DirectoryInfo d in dirs)
            {
                string name = d.Name.Split('_')[1].Split('(')[0].Split('(')[0];//删除多余字符
                dirInfoData.Add(name, d);
            }
        }

        /// <summary>
        /// 获取模型文件夹FBX
        /// </summary>
        public void GetAllModelFBX(Dictionary<string, DirectoryInfo> dirs)
        {
            foreach (var item in dirs)
            {
                FileInfo[] fileInfos = item.Value.GetFiles();//获取文件
                foreach (var file in fileInfos)
                {
                    string format = file.Name.Split(".")[1];
                    if (format == "FBX" || format == "fbx")
                    {
                        fileInfoData.Add(item.Key, file);
                    }
                }
            }
        }

        /// <summary>
        /// 模型导入文件
        /// </summary>
        public void ModelIntoFile(string path)
        {
            if (Directory.Exists(path))
            {
                Console.WriteLine("开始导入FBX文件");
                foreach (var item in fileInfoData)
                {
                    string intoPath = path + "/" + item.Key;
                    if (!Directory.Exists(intoPath))//判断文件夹是否存在
                    {
                        Directory.CreateDirectory(intoPath);

                        item.Value.CopyTo(intoPath + "/" + item.Key + ".FBX", true);

                        Console.WriteLine(intoPath + "导入成功");
                    }
                    else
                    {
                        Console.WriteLine(intoPath + "文件夹已存在");
                    }
                }
            }
            else
            {
                Console.WriteLine(path + "目录不存在");
                ModelIntoFile(path);//如果输入错误一直递归好了
            }
        }
    }
}

5.Unity设置导入FBX格式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class ModifyMoidel : Editor
{
    [MenuItem("BenBen/修改模型")]
    public static void ModifyMoidelScale()
    {

        DirectoryInfo directoryInfo = new DirectoryInfo(System.Environment.CurrentDirectory + @"/Assets/Models");
        DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
        foreach (DirectoryInfo directory in directoryInfos)
        {
            FileInfo[] fileInfos = directory.GetFiles();
            //Debug.LogError("1");
            foreach (FileInfo file in fileInfos)
            {
                //Assets/Models/淡竹叶/淡竹叶.FBX   
                string extension = Path.GetExtension(file.FullName);
                //Debug.LogError(extension);
                if (extension == ".FBX")
                {
                    string fileName = Path.GetFileNameWithoutExtension(file.FullName);
                    string path = "Assets/Models/" + fileName + "/" + file.Name;

                    ModelImporter modelimporter = ModelImporter.GetAtPath(path) as ModelImporter;
                    modelimporter.materialLocation = ModelImporterMaterialLocation.External;
                    AssetDatabase.ImportAsset(path);
                }
            }
        }
        AssetDatabase.Refresh();
    }
}

 

整体代码运行后得出

Unity 批量导入模型

相关标签: 开发工具 unity