我的unity(18)

本文介绍了一个使用Unity EditorWindow创建的工具类,该工具能够递归遍历指定目录下的FBX模型文件,并在Unity中批量创建对应的预制体。通过简单的GUI界面,用户可以设置源文件路径和目标保存路径,实现快速、批量的预制体生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继承EditorWindow 写的工具类
递归遍历模型 创建预制体

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

public class ToolDirectory : EditorWindow
{

    [MenuItem ("Tools/CreatStreamAssetsDir")]
    public static void CreatStreamingAssetsDir ()
    {
        if (!Directory.Exists (Application.streamingAssetsPath))
        {
            Directory.CreateDirectory (Application.streamingAssetsPath);
        }
    }
    [MenuItem ("Tools/CreatWkAndUnityPacksDir")]
    public static void CreatWKAndPackageDir ()
    {
        CreatStreamingAssetsDir ();
        if (!Directory.Exists (Application.streamingAssetsPath + "Wk"))
        {
            Directory.CreateDirectory (Application.streamingAssetsPath + "Wk");
        }
        if (!Directory.Exists (Application.streamingAssetsPath + "unityPacks"))
        {
            Directory.CreateDirectory (Application.streamingAssetsPath + "unityPacks");
        }
    }

    Dictionary<string, Object> Fbx_dic;
    private string selectPath;
    private string targetPath;

    [MenuItem ("Tools/CreatPrefabBySearchDir")]
    public static void CreatPrefabBySearch ()
    {
        GetWindow<ToolDirectory> ().Show ();
    }


    private void OnGUI ()
    {
        EditorGUILayout.BeginHorizontal ();
        GUILayout.Label ("文件路径");
        EditorGUILayout.TextField (selectPath);
        if (GUILayout.Button ("选择路径"))
        {
            selectPath = EditorUtility.OpenFolderPanel ("选择文件夹", Application.dataPath, "");
        }
        EditorGUILayout.EndHorizontal ();

        EditorGUILayout.BeginHorizontal ();
        GUILayout.Label ("保存路径");
        EditorGUILayout.TextField (targetPath);
        if (GUILayout.Button ("保存路径"))
        {
            targetPath = EditorUtility.OpenFolderPanel ("选择文件夹", Application.dataPath, "");
        }
        EditorGUILayout.EndHorizontal ();
        EditorGUILayout.BeginHorizontal ();
        GUILayout.FlexibleSpace ();  //弹性空间
        if (GUILayout.Button ("创建预制体"))
        {
            if (!string.IsNullOrEmpty (selectPath))
            {
                SearcheFbx (selectPath);
                CreatPrefabBySearchDir (Fbx_dic);
            }

        }
        GUILayout.FlexibleSpace ();  //弹性空间
        EditorGUILayout.EndHorizontal ();
    }

    private void SearcheFbx ( string dirPath )
    {
        DirectoryInfo info = new DirectoryInfo (dirPath);
        FileInfo[] fileA_rr = info.GetFiles ("*.fbx");
        if (fileA_rr != null)
        {
            foreach (var file in fileA_rr)
            {
                if (file.Name.Contains (".fbx"))
                {
                    if (Fbx_dic == null)
                        Fbx_dic = new Dictionary<string, Object> ();
                    //Debug.Log ("file.Name"+file.Name);
                    Debug.Log ("file.FullName" + file.FullName);
                    if (!Fbx_dic.ContainsKey (file.Name))
                    {
                        int index = file.FullName.IndexOf (@"Assets\");
                        string AssetPath = file.FullName.Substring (index);
                        Object obj = AssetDatabase.LoadAssetAtPath (AssetPath, typeof (Object));
                        Debug.Log (obj.name);
                        Fbx_dic.Add (file.Name, obj);
                    }
                }
            }
        }
        DirectoryInfo[] dir_Arr = info.GetDirectories ();
        if (dir_Arr != null)
        {
            foreach (var dir in dir_Arr)
            {
                SearcheFbx (dir.FullName);
            }
        }
    }
    /// <summary>
    /// 创建预制体
    /// </summary>
    private void CreatPrefabBySearchDir ( Dictionary<string, Object> Fbx_dic)
    {
        if (Fbx_dic != null && Fbx_dic.Count > 0)
        {
            foreach (var item in Fbx_dic)
            {
                GameObject go = GameObject.Instantiate (item.Value) as GameObject;
                go.name = item.Key.Replace (".fbx", "");
                string Existpath = targetPath + "/" + item.Key.Replace (".fbx", "") +".prefab";
                if (File.Exists (Existpath))
                {
                    File.Delete (Existpath);
                    Debug.Log ("已删除"+Existpath);
                }
                string path = Existpath.Remove (0,Existpath.IndexOf("Assets"));
                Debug.Log (path);
                PrefabUtility.CreatePrefab (path, go, ReplacePrefabOptions.ConnectToPrefab);
            }          
        }
        Fbx_dic.Clear ();
    }
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值