我的unity(18)

继承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 Fbx_dic;
    private string selectPath;
    private string targetPath;

    [MenuItem ("Tools/CreatPrefabBySearchDir")]
    public static void CreatPrefabBySearch ()
    {
        GetWindow ().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 ();
                    //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);
            }
        }
    }
    /// 
    /// 创建预制体
    /// 
    private void CreatPrefabBySearchDir ( Dictionary 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 ();
    }
   
}

你可能感兴趣的:(边学边记,Unity,编辑器工具)