Unity3D Editor 编辑器扩展——脚本查询

前言:

小熊来了!啊呜~

话说前几天心血来潮想把现有的工具整理一下,手欠啊(狠狠打一下,还真疼,55555555)
咱就说接手一个项目,发现四处挂载着脚本,你闹心不?
不不不,你不该闹心,真正让你闹心的是发现有的位置挂载脚本丢失了,再或者你根本不知道他这个脚本都挂在了哪些物体身上,痛苦不?
不行了,我得找个角落难受一会儿去…

当当当当~小熊特推出脚本查询工具!
先上图看看效果
Unity3D Editor 编辑器扩展——脚本查询_第1张图片
Unity3D Editor 编辑器扩展——脚本查询_第2张图片

功能实现

就说这功能香不香?
哎?谁打我,不就话多了点,皮了一下嘛~555555555555555555
好吧,上代码

// ========================================================
// 描 述:脚本查询
// 作 者:SW
// 创建时间:2023/03/07 00:011:07
// 版 本:v 1.0
// ========================================================

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

public class FindMissingScriptsRecursively : EditorWindow
{
    private int _toolbarid;
    private int _tip = 0;

    #region 查找丢失的脚本

    public Vector2 scrollViewPos = new Vector2(0, 0);
    private static int _goCount = 0, _componentsCount = 0, _missingCount = 0;
    private string _resStr = "";
    private Dictionary _missGoDic = new Dictionary();
    private bool _isClear = true;

    #endregion

    #region 脚本位置查询

    private MonoScript _scriptObj = null;
    List _results = new List();

    #endregion

    [MenuItem("SW/FindScripts")]
    public static void ShowWindow()
    {
        // var  window = GetWindow();
        // window.Show();
        GetWindow(typeof(FindMissingScriptsRecursively));
    }

    public void OnGUI()
    {
        _toolbarid = GUILayout.Toolbar(_toolbarid, new[] { "寻找丢失的脚本", "脚本位置查询" });
        if (_toolbarid == 0)
        {
            EditorGUILayout.LabelField("当前选择了 " + _tip + " 个对象");
            if (GUILayout.Button("开始寻找丢失的脚本"))
            {
                FindInSelected();
            }

            if (!_isClear)
            {
                EditorGUILayout.HelpBox(_resStr, _missingCount > 0 ? MessageType.Warning : MessageType.Info);
            }


            scrollViewPos = EditorGUILayout.BeginScrollView(scrollViewPos, false, false, GUILayout.Height(300));
            foreach (var missGo in _missGoDic)
            {
                EditorGUILayout.ObjectField("脚本丢失物体:", missGo.Key, typeof(GameObject), false);
                EditorGUILayout.LabelField("描述:");
                EditorGUILayout.LabelField(missGo.Value, GUILayout.Height(30));
                EditorGUILayout.LabelField("");
            }

            EditorGUILayout.EndScrollView();

            if (GUILayout.Button("清除"))
            {
                _resStr = "";
                _missGoDic.Clear();
                _isClear = true;
            }
        }

        if (_toolbarid == 1)
        {
            EditorGUILayout.LabelField("当前选择了 " + _tip + " 个对象");
            _scriptObj = (MonoScript)EditorGUILayout.ObjectField("脚本类型:", _scriptObj, typeof(MonoScript), true);
            if (GUILayout.Button("Find"))
            {
                _results.Clear();
                FindScript();
            }

            if (_results.Count > 0)
            {
                scrollViewPos = EditorGUILayout.BeginScrollView(scrollViewPos, false, false, GUILayout.Height(200));
                foreach (var trs in _results)
                {
                    EditorGUILayout.ObjectField(trs, typeof(Transform), false);
                }

                EditorGUILayout.EndScrollView();
            }
            else
            {
                GUILayout.Label("无数据");
            }
        }
    }


    #region 查找丢失的脚本

    private void FindInSelected()
    {
        _isClear = false;
        _goCount = 0;
        _componentsCount = 0;
        _missingCount = 0;
        _missGoDic.Clear();
        foreach (var go in Selection.gameObjects)
        {
            FindInGO(go);
        }

        _resStr = string.Format("找到 {0} 个物体 , {1} 个组件 , 找到 {2} 个missing", _goCount, _componentsCount, _missingCount);
    }

    private void FindInGO(GameObject go)
    {
        _goCount++;
        var components = go.GetComponents();

        for (int i = 0; i < components.Length; i++)
        {
            _componentsCount++;
            if (components[i] == null)
            {
                _missingCount++;
                var s = go.name;
                var t = go.transform;
                while (t.parent != null)
                {
                    s = t.parent.name + "/" + s;
                    t = t.parent;
                }

                var res = "    关系位置:" + s + "\n    索引位置:" + i;
                _missGoDic.Add(go, res);
            }
        }

        foreach (Transform childT in go.transform)
        {
            FindInGO(childT.gameObject);
        }
    }

    #endregion


    #region 脚本位置查询

    private void FindScript()
    {
        foreach (var trs in Selection.gameObjects)
        {
            if (trs != null && _scriptObj != null)
            {
                var ssss = trs.GetComponentsInChildren(_scriptObj.GetClass());
                foreach (var component in ssss)
                {
                    _results.Add(component.transform);
                }
            }
        }
    }

    #endregion


    void OnSelectionChange()
    {
        _tip = Selection.gameObjects.Length;
    }
}

你问我这东西咋用?

你别动,让我想想拿个什么趁手的东西打你,毕竟直接上手的话,手疼啊,我又不傻~
在这里插入图片描述
呐~就酱紫的,把脚本找个Editor文件放进去,然后在编译器顶部菜单栏中会新增一个名叫“SW”的菜单,点开下拉就看到啦

如果有哪里不合适,小宝宝们提出来我马上修改,我保证不摔盘子不摔碗,拿着大锤认真聆听你们的建议~哎哎哎,还真有人上前

你们还想要什么编译器工具,描述说清楚写在评论区,我会优先制作哒!

你可能感兴趣的:(Unity3D,Editor,编译器扩展,unity,编辑器)