编辑器扩展 检查Resources目录下所有的预设Text组件

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

class CheckPrefabText : Editor
{
    [MenuItem("Tools/CheckPrefabText")]
    private static void CheckPrefabTexts()
    {
        List mPrefabs = new List();
        string fullPath = "Assets/Resources/Prefab" + "/";  //路径
        if (Directory.Exists(fullPath))
        {
            DirectoryInfo direction = new DirectoryInfo(fullPath);
            FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith(".prefab"))
                {
                    string path = files[i].Directory + "\\" + files[i].Name;
                    
                    string[] str = path.Split('\\');
                    string realPath = "";
                    for (int j = 4;  j < str.Length; j++)
                    {
                        if (j == str.Length - 1)
                        {
                            realPath += files[i].Name.Split('.')[0];
                        }
                        else
                        {
                            realPath += str[j] + "\\";
                        }
                    }
                    realPath = realPath.Replace("\\","/");
                    mPrefabs.Add((Resources.Load(realPath) as GameObject).transform);
                }
            }
        }
        for (int i = 0; i < mPrefabs.Count; i++)
        {
            TraversalChildText(mPrefabs[i], mPrefabs[i].name);
            //Debug.LogError(mPrefabs[i].name);
        }
    }

    //遍历所有预设子物体text
    private static void TraversalChildText(Transform trans,string parentName)
    {

        string txt = "";
        if (trans == null)
        {
            return;
        }
        if (trans.childCount == 0)
        {
            if (trans.GetComponent() != null)
            {
                txt += "\n" + "【" + parentName + "】---" + trans.name + "---" + trans.GetComponent().text;
            }
            return;
        }
        if (trans.childCount > 0)
        {
            if (trans.GetComponent() != null)
            {
                txt += "\n" + "【" + parentName + "】---" + trans.name + "---" + trans.GetComponent().text;
            }
            for (int i = 0; i < trans.childCount; i++)
            {
                if (trans.GetChild(i).GetComponentInChildren() != null)
                {
                    txt += "\n" + "【" + parentName + "】---" + trans.name + "---" + trans.GetChild(i).name + "---" + trans.GetChild(i).GetComponentInChildren().text;
                    TraversalChildText(trans.GetChild(i), parentName);
                }
            }
        }
        WriteIntoTxt(txt);
    }

    static StreamWriter writer;

    //text导出txt文件
    public static void WriteIntoTxt(string message)
    {
        FileInfo file = new FileInfo(Application.dataPath + "/allPrefsb_txt.txt");
        if (!file.Exists)
        {
            writer = file.CreateText();
        }
        else
        {
            writer = file.AppendText();
        }
        writer.WriteLine(message);
        writer.Flush();
        writer.Dispose();
        writer.Close();
    }
}

 

你可能感兴趣的:(u3d)