4.Unity经验:Unity3d中查找一个脚本被挂在哪些预设上面

用一个脚本函数可以获取到选择的脚本文件被哪些预设和场景引用

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

public class GetReference
{
    [MenuItem("Assets/Tools/GetFileReference")]
    static void GetFileReference()
    {
        string target = "";
        if (Selection.activeObject != null)
        {
            target = AssetDatabase.GetAssetPath(Selection.activeObject);
        }
        if (string.IsNullOrEmpty(target))
        {
            return;
        }
        string[] files = Directory.GetFiles(Application.dataPath, "*.prefab", SearchOption.AllDirectories);
        string[] scene = Directory.GetFiles(Application.dataPath, "*unity", SearchOption.AllDirectories);

        List<Object> fileList = new List<Object>();
        for (int i = 0; i < files.Length; i++)
        {
            string[] source = AssetDatabase.GetDependencies(new string[] { files[i].Replace(Application.dataPath, "Assets") });
            for (int j = 0; j < source.Length; j++)
            {
                if (source[j] == target)
                {
                    fileList.Add(AssetDatabase.LoadMainAssetAtPath(files[i].Replace(Application.dataPath, "Assets")));
                }
            }
        }
        for (int i = 0; i < scene.Length; i++)
        {
            string[] source = AssetDatabase.GetDependencies(new string[] { scene[i].Replace(Application.dataPath, "Assets")});
            for (int j = 0; j < source.Length; j++)
            {
                if (source[j] == target)
                {
                    fileList.Add(AssetDatabase.LoadMainAssetAtPath(scene[i].Replace(Application.dataPath, "Assets")));
                }
            }
        }
        Selection.objects = fileList.ToArray();
    }
}

更新的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

public class ScriptReferences : EditorWindow
{
    private string _logMsg;
    private Vector2 _scrollPos3;
    private static Object _target;
    private List<Object> _uiPrefabList = new List<Object>();
    private List<string> _childNameList = new List<string>();
    private static string _uiPrefabPath = @"Assets/Package/UI";

    [MenuItem("Assets/输出脚本挂在了哪些预制上")]
    static void ShowMyEditorWindow()
    {
        _target = Selection.GetFiltered(typeof(Object), SelectionMode.Assets)[0];
        GetWindow<ScriptReferences>("查找脚本引用").Show();
    }

    private void OnGUI()
    {
        _uiPrefabPath = EditorGUILayout.TextField("UI预制路径", _uiPrefabPath);
        _target = EditorGUILayout.ObjectField("脚本", _target, typeof(Object), false);
        if (GUILayout.Button("执行"))
        {
            GetFileReference();
        }

        GUILayout.Label("提示输出", EditorStyles.boldLabel);
        EditorGUILayout.TextArea(_logMsg);

        EditorGUILayout.BeginVertical();
        _scrollPos3 = EditorGUILayout.BeginScrollView(_scrollPos3);
        GUILayout.Label("对应的UI预制", EditorStyles.boldLabel);
        for (int i = 0; i < _uiPrefabList.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.ObjectField("", _uiPrefabList[i], typeof(Object), false, GUILayout.MaxWidth(200));
            EditorGUILayout.TextArea(_childNameList[i]);
            EditorGUILayout.EndHorizontal();
        }

        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndVertical();
    }

    private void GetFileReference()
    {
        _uiPrefabList = new List<Object>();
        _childNameList = new List<string>();
        string target = "";
        if (Selection.activeObject != null)
        {
            target = AssetDatabase.GetAssetPath(Selection.activeObject);
        }

        if (string.IsNullOrEmpty(target))
        {
            return;
        }

        string[] files = Directory.GetFiles(_uiPrefabPath, "*.prefab", SearchOption.AllDirectories);
        var curType = typen(_target.name);
        for (int i = 0; i < files.Length; i++)
        {
            string[] source = AssetDatabase.GetDependencies(new string[]
                {files[i].Replace(Application.dataPath, "Assets")});
            for (int j = 0; j < source.Length; j++)
            {
                if (source[j] == target)
                {
                    var item = AssetDatabase.LoadMainAssetAtPath(files[i].Replace(Application.dataPath, "Assets"));
                    _uiPrefabList.Add(item);
                    GameObject go = AssetDatabase.LoadAssetAtPath(files[i], typeof(System.Object)) as GameObject;
                    if (go != null)
                    {
                        string childName = "";
                        if (go.GetComponent(curType) != null)
                            childName = childName + go.name + " |";
                        GetChildName(go.transform, curType, ref childName);
                        _childNameList.Add(childName);
                    }
                }
            }
        }

        _logMsg = "脚本被引用次数:" + _uiPrefabList.Count.ToString();
    }

    private void GetChildName(Transform go, Type type, ref string childName)
    {
        for (int i = 0; i < go.transform.childCount; i++)
        {
            var _child = go.transform.GetChild(i);
            if (_child.GetComponent(type) != null)
                childName = childName + _child.name + " |";

            if (_child.childCount != 0)
                GetChildName(_child, type, ref childName);
        }
    }

    private Type typen(string typeName)
    {
        Type type = null;
        Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
        int assemblyArrayLength = assemblyArray.Length;
        for (int i = 0; i < assemblyArrayLength; ++i)
        {
            type = assemblyArray[i].GetType(typeName);
            if (type != null)
            {
                return type;
            }
        }

        for (int i = 0; (i < assemblyArrayLength); ++i)
        {
            Type[] typeArray = assemblyArray[i].GetTypes();
            int typeArrayLength = typeArray.Length;
            for (int j = 0; j < typeArrayLength; ++j)
            {
                if (typeArray[j].Name.Equals(typeName))
                {
                    return typeArray[j];
                }
            }
        }

        return type;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kerven_HKW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值