Exemplo n.º 1
0
    public static void WhatUsesThisComponent(MenuCommand command)
    {
        Component c            = (Component)command.context;
        var       typeToSearch = c.GetType();
        var       typeString   = c.GetType().Name;

        Debug.Log("Finding all Prefabs and scenes that have the component " + c.GetType() + "...");

        string[] guids = AssetDatabase.FindAssets("t:scene t:prefab");

        int count = 0;

        for (int i = 0; i < guids.Length; i++)
        {
            string guid = guids[i];
            //Debug.Log(AssetDatabase.GUIDToAssetPath(guid));
            string   myObjectPath = AssetDatabase.GUIDToAssetPath(guid);
            var      asset        = AssetDatabase.LoadAssetAtPath(myObjectPath, typeof(Object));
            Object[] myObjs;
            Scene    s = default;
            if (asset is SceneAsset)
            {
                s      = EditorSceneManager.OpenPreviewScene(myObjectPath);
                myObjs = s.GetRootGameObjects().SelectMany(g => g.GetComponentsInChildren(typeToSearch, true)).ToArray();
            }
            else
            {
                myObjs = AssetDatabase.LoadAllAssetsAtPath(myObjectPath);
            }

            if (EditorUtility.DisplayCancelableProgressBar("Searching in scenes/prefabs...", count + " matches. Progress: " + i + " / " + guids.Length + ". Current: " + asset.name, i / (guids.Length - 1f)))
            {
                goto End;
            }

            foreach (Object thisObject in myObjs)
            {
                if (typeToSearch.IsAssignableFrom(thisObject.GetType()))
                {
                    Debug.Log("<color=red>" + typeString + "</color> Found in <color=green>" + thisObject.name + "</color> at " + myObjectPath, asset);
                    count++;
                    break;
                }
            }

            if (asset is SceneAsset)
            {
                EditorSceneManager.ClosePreviewScene(s);
            }
        }

End:
        EditorUtility.ClearProgressBar();
    }
        static Scene LoadOrCreatePreviewScene(string environmentEditingScenePath)
        {
            Scene previewScene;

            if (!string.IsNullOrEmpty(environmentEditingScenePath))
            {
                previewScene = EditorSceneManager.OpenPreviewScene(environmentEditingScenePath);
                var roots   = previewScene.GetRootGameObjects();
                var visitor = new TransformVisitor();
                foreach (var root in roots)
                {
                    visitor.VisitAll(root.transform, AppendEnvironmentName, null);
                }
            }
            else
            {
                previewScene = CreateDefaultPreviewScene();
            }

            return(previewScene);
        }