Unity查找脚本被哪些Perfab或场景引用

Unity中查找脚本被哪些Prefab或场景引用

Unity中有个Find References In Scene的功能,可是仅仅能查找在当前场景中的引用。

假设发现某个脚本不知道被挂在哪个Prefab上了,以下这个脚本你可能用得到

实如今查找脚本在哪些Prefab或者场景中被引用。查找脚本引用了哪些对象(脚本,Texture,字体等)

先看截图:

源代码在这里

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

public class FindReference : EditorWindow{

    static public FindReference instance;
    Vector2 mScroll = Vector2.zero;
    public Dictionary<string,BetterList<string>> dict;

    void OnEnable() { instance = this; }
    void OnDisable() { instance = null; }

    void OnGUI()
    {

        if (dict == null)
        {
            return;
        }
        mScroll = GUILayout.BeginScrollView(mScroll);

        BetterList<string> list = dict["prefab"];
        if (list != null && list.size>0)
        {
            if(NGUIEditorTools.DrawHeader("Prefab"))
            {
                foreach (string item in list)
                {
                    GameObject go = AssetDatabase.LoadAssetAtPath(item, typeof(GameObject)) as GameObject;
                    EditorGUILayout.ObjectField("Prefab", go, typeof(GameObject), false);

                }
            }
            list = null;
        }

        list = dict["fbx"];
        if (list != null && list.size > 0)
        {
            if(NGUIEditorTools.DrawHeader("FBX")){
                foreach (string item in list)
                {
                    GameObject go = AssetDatabase.LoadAssetAtPath(item, typeof(GameObject)) as GameObject;
                    EditorGUILayout.ObjectField("FBX", go, typeof(GameObject), false);

                }
            }
            list = null;
        }

        list = dict["cs"];
        if (list != null && list.size > 0)
        {
            if(NGUIEditorTools.DrawHeader("Script")){
                foreach (string item in list)
                {
                    MonoScript go = AssetDatabase.LoadAssetAtPath(item, typeof(MonoScript)) as MonoScript;
                    EditorGUILayout.ObjectField("Script", go, typeof(MonoScript), false);

                }
            }
            list = null;
        }

        list = dict["texture"];
        if (list != null && list.size > 0)
        {
            if (NGUIEditorTools.DrawHeader("Texture"))
            {
                foreach (string item in list)
                {
                    Texture2D go = AssetDatabase.LoadAssetAtPath(item, typeof(Texture2D)) as Texture2D;
                    EditorGUILayout.ObjectField("Texture:" + go.name, go, typeof(Texture2D), false);

                }
            }
            list = null;
        }

        list = dict["mat"];
        if (list != null && list.size > 0)
        {
            if (NGUIEditorTools.DrawHeader("Material"))
            {
                foreach (string item in list)
                {
                    Material go = AssetDatabase.LoadAssetAtPath(item, typeof(Material)) as Material;
                    EditorGUILayout.ObjectField("Material", go, typeof(Material), false);

                }
            }
            list = null;
        }

        list = dict["shader"];
        if (list != null && list.size > 0)
        {
            if (NGUIEditorTools.DrawHeader("Shader"))
            {
                foreach (string item in list)
                {
                    Shader go = AssetDatabase.LoadAssetAtPath(item, typeof(Shader)) as Shader;
                    EditorGUILayout.ObjectField("Shader", go, typeof(Shader), false);
                }
            }
            list = null;
        }

        list = dict["font"];
        if (list != null && list.size > 0)
        {
            if (NGUIEditorTools.DrawHeader("Font"))
            {
                foreach (string item in list)
                {
                    Font go = AssetDatabase.LoadAssetAtPath(item, typeof(Font)) as Font;
                    EditorGUILayout.ObjectField("Font", go, typeof(Font), false);
                }
            }
            list = null;
        }

        list = dict["anim"];
        if (list != null && list.size > 0)
        {
            if (NGUIEditorTools.DrawHeader("Animation"))
            {
                foreach (string item in list)
                {
                    AnimationClip go = AssetDatabase.LoadAssetAtPath(item, typeof(AnimationClip)) as AnimationClip;
                    EditorGUILayout.ObjectField("Animation:", go, typeof(AnimationClip), false);
                }
            }
            list = null;
        }

        list = dict["animTor"];
        if (list != null && list.size > 0)
        {
            if (NGUIEditorTools.DrawHeader("Animator"))
            {
                foreach (string item in list)
                {
                    //Animator go = AssetDatabase.LoadAssetAtPath(item, typeof(Animator)) as Animator;
                    //EditorGUILayout.ObjectField("Animator:", go, typeof(Animator), true);
                    EditorGUILayout.LabelField(item);
                }
            }
            list = null;
        }

        list = dict["level"];
        if (list != null && list.size > 0)
        {
            if (NGUIEditorTools.DrawHeader("Level"))
            {
                foreach (string item in list)
                {
                    //SceneView go = AssetDatabase.LoadAssetAtPath(item, typeof(SceneView)) as SceneView;
                    EditorGUILayout.LabelField(item);
                    //SceneView go = AssetDatabase.LoadAssetAtPath(item, typeof(SceneView)) as SceneView;
                    //EditorGUILayout.ObjectField("Animation:" , go, typeof(SceneView), true);
                }
            }
            list = null;
        }

        GUILayout.EndScrollView();
        //NGUIEditorTools.DrawList("Objects", list.ToArray(), "");
    }

    /// <summary>
    /// 依据脚本查找引用的对象
    /// </summary>
    [MenuItem("Assets/Wiker/Find Script Reference", false, 0)]
    static public void FindScriptReference()
    {
        //EditorWindow.GetWindow<UIAtlasMaker>(false, "Atlas Maker", true).Show();
        //Debug.Log("Selected Transform is on " + Selection.activeObject.name + ".");
        //foreach(string guid in Selection.assetGUIDs){

        //    Debug.Log("GUID " + guid);

        //}
        ShowProgress(0,0,0);
        string curPathName = AssetDatabase.GetAssetPath(Selection.activeObject.GetInstanceID());

        Dictionary<string, BetterList<string>> dic = new Dictionary<string, BetterList<string>>();
        BetterList<string> prefabList = new BetterList<string>();
        BetterList<string> fbxList = new BetterList<string>();
        BetterList<string> scriptList = new BetterList<string>();
        BetterList<string> textureList = new BetterList<string>();
        BetterList<string> matList = new BetterList<string>();
        BetterList<string> shaderList = new BetterList<string>();
        BetterList<string> fontList = new BetterList<string>();
        BetterList<string> levelList = new BetterList<string>();

        string[] allGuids = AssetDatabase.FindAssets("t:Prefab t:Scene", new string[]{"Assets"});
        int i = 0;
        foreach (string guid in allGuids)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            string[] names = AssetDatabase.GetDependencies(new string[]{assetPath});  //依赖的东东
            foreach (string name in names)
            {
                if (name.Equals(curPathName))
                {
                    //Debug.Log("Refer:" + assetPath);
                    if (assetPath.EndsWith(".prefab"))
                    {
                        prefabList.Add(assetPath);
                        break;
                    }
                    else if (assetPath.ToLower().EndsWith(".fbx"))
                    {
                        fbxList.Add(assetPath);
                        break;
                    }
                    else if (assetPath.ToLower().EndsWith(".unity"))
                    {
                        levelList.Add(assetPath);
                        break;
                    }
                    else if (assetPath.EndsWith(".cs"))
                    {
                        scriptList.Add(assetPath);
                        break;
                    }
                    else if (assetPath.EndsWith(".png"))
                    {
                        textureList.Add(assetPath);
                        break;
                    }
                    else if (assetPath.EndsWith(".mat"))
                    {
                        matList.Add(assetPath);
                        break;
                    }
                    else if (assetPath.EndsWith(".shader"))
                    {
                        shaderList.Add(assetPath);
                        break;
                    }
                    else if (assetPath.EndsWith(".ttf"))
                    {
                        fontList.Add(assetPath);
                        break;
                    }
                }
            }
            ShowProgress((float)i / (float)allGuids.Length, allGuids.Length,i);
            i++;
        }

        dic.Add("prefab", prefabList);
        dic.Add("fbx", fbxList);
        dic.Add("cs", scriptList);
        dic.Add("texture", textureList);
        dic.Add("mat", matList);
        dic.Add("shader", shaderList);
        dic.Add("font", fontList);
        dic.Add("level", levelList);
        dic.Add("anim", null);
        dic.Add("animTor", null);
        EditorUtility.ClearProgressBar();
        EditorWindow.GetWindow<FindReference>(false, "Object Reference", true).Show();

        //foreach (KeyValuePair<string, BetterList<string>> d in dic)
        //{
        //    foreach (string s in d.Value)
        //    {
        //        Debug.Log(d.Key + "=" + s);
        //    }
        //}

        if (FindReference.instance.dict != null) FindReference.instance.dict.Clear();
        FindReference.instance.dict = dic;

        //string[] path = new string[1];
        //path[0] = AssetDatabase.GetAssetPath(Selection.activeObject.GetInstanceID());
        //string[] names = AssetDatabase.GetDependencies(path);  //依赖的东东
        //foreach (string name in names)
        //{
        //    Debug.Log("Name:"+name);
        //}

    }

    public static void ShowProgress(float val,int total,int cur)
    {
        EditorUtility.DisplayProgressBar("Searching", string.Format("Finding ({0}/{1}), please wait...",cur,total), val);
    }

    /// <summary>
    /// 查找对象引用的类型
    /// </summary>
    [MenuItem("Assets/Wiker/Find Object Dependencies", false, 0)]
    public static void FindObjectDependencies()
    {

        ShowProgress(0,0,0);
        Dictionary<string, BetterList<string>> dic = new Dictionary<string, BetterList<string>>();
        BetterList<string> prefabList = new BetterList<string>();
        BetterList<string> fbxList = new BetterList<string>();
        BetterList<string> scriptList = new BetterList<string>();
        BetterList<string> textureList = new BetterList<string>();
        BetterList<string> matList = new BetterList<string>();
        BetterList<string> shaderList = new BetterList<string>();
        BetterList<string> fontList = new BetterList<string>();
        BetterList<string> animList = new BetterList<string>();
        BetterList<string> animTorList = new BetterList<string>();
        string curPathName = AssetDatabase.GetAssetPath(Selection.activeObject.GetInstanceID());
        string[] names = AssetDatabase.GetDependencies(new string[] { curPathName });  //依赖的东东
        int i = 0;
        foreach (string name in names)
        {
            if (name.EndsWith(".prefab"))
            {
                prefabList.Add(name);
            }
            else if (name.ToLower().EndsWith(".fbx"))
            {
                fbxList.Add(name);
            }
            else if (name.EndsWith(".cs"))
            {
                scriptList.Add(name);
            }
            else if (name.EndsWith(".png"))
            {
                textureList.Add(name);
            }
            else if (name.EndsWith(".mat"))
            {
                matList.Add(name);
            }
            else if (name.EndsWith(".shader"))
            {
                shaderList.Add(name);
            }
            else if (name.EndsWith(".ttf"))
            {
                fontList.Add(name);
            }
            else if (name.EndsWith(".anim"))
            {
                animList.Add(name);
            }
            else if (name.EndsWith(".controller"))
            {
                animTorList.Add(name);
            }
            Debug.Log("Dependence:"+name);
            ShowProgress((float)i / (float)names.Length,names.Length,i);
            i++;
        }

        dic.Add("prefab", prefabList);
        dic.Add("fbx", fbxList);
        dic.Add("cs", scriptList);
        dic.Add("texture", textureList);
        dic.Add("mat", matList);
        dic.Add("shader", shaderList);
        dic.Add("font", fontList);
        dic.Add("level", null);
        dic.Add("animTor", animTorList);
        dic.Add("anim", animList);
        //deps.Sort(Compare);
        EditorWindow.GetWindow<FindReference>(false, "Object Dependencies", true).Show();
        if (FindReference.instance.dict != null) FindReference.instance.dict.Clear();
        FindReference.instance.dict = dic;
        EditorUtility.ClearProgressBar();
    }

}
时间: 2024-12-29 01:03:35

Unity查找脚本被哪些Perfab或场景引用的相关文章

unity 查找脚本被场景中哪些对象引用

在需要查找的脚本上右键: 在场景中已经显示出所有引用该脚本的对象

自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药

自制Unity小游戏TankHero-2D(5)声音+爆炸+场景切换+武器弹药 我在做这样一个坦克游戏,是仿照(http://game.kid.qq.com/a/20140221/028931.htm)这个游戏制作的.仅为学习Unity之用.图片大部分是自己画的,少数是从网上搜来的.您可以到我的github页面(https://github.com/bitzhuwei/TankHero-2D)上得到工程源码. 本篇主要记录声音.场景切换.武器弹药等. 关于碰撞 先插一句.上一篇记录了Unity3

Unity Mono脚本 加密

加密环境 引擎版本:Unity3D 5.3.4 及更高版本 (使用Mono而并非IL2CPP) 操作系统:CentOS 6.2(Final) 加密环境:Android.IOS(暂定) 加密对象:C#源代码(dll文件) 解密方法:libmono.so (重点:加入解密算法并重编译此文件) 加密的目地 .NET Reflector等反编译工具 无法通过对dll反编译得到源码 注意事项 dll的加密算法和libmono.so解密算法一致 思路分析 重点:我们需要对libmono.so重编译,加入我们

【Unity 3D】学习笔记二十六:unity游戏脚本(六)

在3D游戏世界中,任何一个游戏对象在创建的时候都会附带Transform(变换)组件,并且该组件是无法删除的,也不应该删除.在unity中,Transform面板一共有3个属性: Position  (位置) Rotation(旋转) Scale(缩放) 这三个值都是用来调整游戏对象在游戏界面中的位置,状态等相关参数. Position  (位置) 任何一个游戏对象的三维坐标都保存在Vector3容器中,该容器记录对象在X轴,Y轴,Z轴的坐标.一旦Vector33容器中的坐标发生变化,那么Sce

Unity3D技术之Android 脚本高级 Unity 手机脚本

欢迎来到unity学习.unity培训.unity企业培训教育专区,这里有很多U3D资源.U3D培训视频.U3D教程.U3D常见问题.U3D项目源码,我们致力于打造业内unity3d培训.学习第一品牌. 高级 Unity 手机脚本 设备属性 您可以访问一系列特定设备的属性:     SystemInfo.deviceUniqueIdentifier 唯一的设备标识. SystemInfo.deviceName 用户指定的设备名称. SystemInfo.deviceModel 设备型号. Sys

datastage 作业查找脚本

下面两个shell脚本是shell调用datastage作业时查找缺少作业和错误作业名的脚本 脚本一: [[email protected] findjob]# more errcfgjob.sh  #!/bin/bash ####################################################################### #purpose:find the error configuration job in the file dsjob_list2.

Lr中脚本的迭代次数和场景运行时间的关系

Loadrunner中脚本的迭代次数和场景运行时间的关系 LR 的Vugen和controller中迭代是这样的: 当场景的持续时间为“运行至结束”时,以Vugen中设置的迭代次数为准 当场景的持续时间为“具体的几分钟”时,忽略Vugen中的迭代次数,脚步的action重复迭代,直到时间结束为止,按退出策略,执行退出操作

【Unity 3D】学习笔记二十七:unity游戏脚本(七)

使用C#编写游戏脚本 在前面提到,unity支持三种语言编写脚本:js,C#,boo.入门的时候建议只用js,因为js比较简单易懂,语法也不是很严格.但后来晋级的时候推荐使用C#,因为它比较符合unity的编程思想,执行效率更高.下面总结下怎么使用C#编写脚本. 继承MonoBehaviour类 在unity中,任何一个脚本,包括上述三种语言都需要去继承MonoBehaviour这个类.为什么我们之前写JS代码的时候没有继承咧?因为在创建JS代码的时候,系统会将其类名与继承关系隐藏起来. 在pr

unity查找场景中所有物体

代码很简单就一句话,至于你想怎么用再往里面加东西就好 //查找场景中所有的物体 foreach (GameObject objj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject))) { Debug.Log(objj.transform.name); } 原文地址:https://www.cnblogs.com/qq2351194611/p/11423827.html