Exemple #1
0
        protected override void processAsset(string assetPath)
        {
            Scene?currentScene = null;

            if (job.scope.projectScope == ProjectScope.CurrentScene)
            {
                currentScene = EditorSceneManager.GetActiveScene();
            }
            else
            {
                currentScene = SceneUtil.LoadScene(assetPath, OpenSceneMode.Single);
            }

            // Debug.Log("[SearchJob] scene:"+currentScene.path+" loaded.");
            GameObject[] rootObjects = currentScene.Value.GetRootGameObjects();

            foreach (GameObject go in rootObjects)
            {
                // Debug.Log("[SearchJob] root:"+go);
                job.searchGameObject(go);
            }
            if (job.assetData.assetIsDirty)
            {
                EditorSceneManager.SaveScene(currentScene.Value);
            }

            job.searchDependencies(rootObjects.ToArray());
        }
Exemple #2
0
        public IEnumerator ExecuteAsync()
        {
            initializeSearch();
            if (searchIncludesScenes)
            {
                bool shouldContinue = SceneUtil.SaveDirtyScenes();
                if (!shouldContinue)
                {
                    userAbortedSearch();
                    yield break;
                }
            }


            foreach (SearchAssetData searchAssetData in searchAssets)
            {
                if (cancelled)
                {
                    break;
                }
                searchAsset(searchAssetData);
                yield return(searchDependencies(searchAssetData.Roots));

                searchAssetData.Unload();

                if (resultSet.resultsCount > maxItemsAllowed && !shownMaxItemsWarning)
                {
                    shownMaxItemsWarning = true;
                    bool userContinues = EditorUtility.DisplayDialog("Too Many Results", "The search and replace plugin has found " + resultSet.resultsCount + " results so far. Do you want to continue searching?", "Continue", "Cancel");
                    if (!userContinues)
                    {
                        cancelled = true;
                    }
                }
                currentItem++;
                yield return(null);
            }

            EditorUtility.ClearProgressBar();
            search.OnSearchEnd(this);

            if (cancelled)
            {
                userAbortedSearch();
            }

            calculateSearchedItems();

            string log = logBuilder.ToString();

            if (log.Length > 0)
            {
                Debug.Log("[Search & Replace] Log:\n" + log);
            }
            SRWindow.Instance.Repaint();
        }
Exemple #3
0
 protected override void filterAssetPaths(string[] allAssets)
 {
     base.filterAssetPaths(allAssets);
     if (job.scope.projectScope == ProjectScope.CurrentScene)
     {
         // Let's add it!
         // Debug.Log("[SceneSubJob] Adding:"+SceneUtil.GuidPathForActiveScene());
         addItems(new string[] { SceneUtil.GuidPathForActiveScene() });
     }
 }
Exemple #4
0
        public static void OpenObjectInScene(PathInfo pathInfo)
        {
            // Debug.Log("[SceneUtil] Opening object:" + pathInfo.FullPath());
            Scene?scene = SceneUtil.LoadScene(pathInfo.assetPath, OpenSceneMode.Single);

            if (scene.HasValue)
            {
                UnityEngine.Object obj = pathInfo.objID.searchForObjectInScene(scene.Value);
                Selection.activeObject = obj;
            }
        }
        void parseIfSceneObject()
        {
            if (obj != null)
            {
                // Debug.Log("[ObjectID] guid: " + guid + " for  " + obj);
                if (guid == "")
                {
                    GameObject go = null;
                    if (obj is Component)
                    {
                        Component c = (Component)obj;
                        go = c.gameObject;
                    }
                    else
                    {
                        go = (GameObject)obj;
                    }
                    isSceneObject = go.scene.path.EndsWith(".unity");
                    if (isSceneObject)
                    {
                        guid = go.scene.path;
                    }
                    else
                    {
                        guid = AssetDatabase.AssetPathToGUID(go.scene.path);
#if UNITY_2018_3_OR_NEWER
                        PrefabStage stage = PrefabStageUtility.GetPrefabStage(go);
                        if (stage != null)
                        {
                            isPrefabStageObject = true;
                        }
#endif
                    }
                    localPath = PathData.ToPathData(obj);
                }
                else
                if (guid == SceneUtil.GuidPathForActiveScene())
                {
                    isSceneObject = true;
                    localPath     = PathData.ToPathData(obj);
                }
                else
                {
                    isSceneObject = false;
                    localPath     = null;
                }
            }
            else
            {
                isSceneObject = false;
                localPath     = null;
            }
        }
Exemple #6
0
        /*
         * Called upon deserialization to initialize the object.
         */
        void GetObject()
        {
            if (obj == null)
            {
                //At this point we don't know if this is a scene object,
                bool guidIsScene = guid == SceneUtil.GuidPathForActiveScene();
                if (guidIsScene)
                {
                    // Debug.Log("[ObjectID] looking in scene:"+guid);
                    obj = searchForObjectInScene();
                }
                else
                {
                    // Debug.Log("[ObjectID] not a scene object");
                }
                UnityEngine.Object o = null;
                // attempt to retrieve the object.
                assetPath = AssetDatabase.GUIDToAssetPath(guid);
                // Debug.Log("[ObjectID] looking at : "+assetPath);
                if (assetPath == "Resources/unity_builtin_extra")
                {
                    // Debug.Log("[ObjectID] internal resource;"+guid+" : " + localID);
                    o = EditorUtility.InstanceIDToObject((int)localID);
                    // Debug.Log("[ObjectID] we found internally:"+o);
                }
                else
                {
                    o = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityEngine.Object));
                }
                //search for local object.
                if (o == null)
                {
                    return; //aka 'null'
                }
                obj = searchForLocalObject(o);
                if (obj == null)
                {
                    // Check our monoscript cache.
                    // Debug.Log("[ObjectID] couldn't find object....checking monoscript cache");
                    obj = MonoScriptCache.getScript(localID);
                }

                // Debug.Log("[ObjectID] obj:"+obj);
                if (obj == null)
                {
                    localID = 0;
                    guid    = "";
                    // wipe it.
                }
                setRequiresImporter();
            }
        }
        public UnityEngine.Object searchForObjectInScene()
        {
            Scene scene;

#if UNITY_2018_3_OR_NEWER
            if (SceneUtil.IsSceneStage())
            {
                scene = PrefabStageUtility.GetCurrentPrefabStage().scene;
                return(searchForObjectInScene(scene));
            }
#endif
            scene = EditorSceneManager.GetActiveScene();
            return(searchForObjectInScene(scene));
        }
Exemple #8
0
        public override bool CanReplaceObject(UnityEngine.Object obj, out string reason)
        {
            reason = "";
            if (!(obj is GameObject))
            {
                reason = "Not a game object.";
                return(false);
            }

            GameObject go = (GameObject)obj;

            if (SceneUtil.IsSceneStage() && roots.Contains(go))
            {
                reason = "Cannot replace root of prefab.";
                return(false);
            }

            return(true);
        }
Exemple #9
0
        public SceneViewSearchAssetData(string path) : base(path)
        {
            containsScripts = true;
#if UNITY_2018_3_OR_NEWER
            if (SceneUtil.IsSceneStage())
            {
                PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                scene     = prefabStage.scene;
                assetPath = prefabStage.prefabAssetPath;
                roots     = new UnityEngine.Object[] { prefabStage.prefabContentsRoot };
            }
            else
            {
                addActiveScene();
            }
#else
            addActiveScene();
#endif
        }
Exemple #10
0
        /**
         * This functions to select active items within the scene and project.
         * It cannot select items when the scene is closed and reopened, since
         * the instanceIDs go away forEVER.
         */
        public void SelectAll()
        {
            HashSet <int> selections        = new HashSet <int>();
            string        openScenePath     = SceneUtil.GuidPathForActiveScene();
            int           unSelectableItems = 0;

            foreach (SearchResultGroup srg in results)
            {
                foreach (SearchResult sr in srg.results)
                {
                    if (sr.pathInfo.objID.isSceneObject)
                    {
                        if (sr.pathInfo.objID.guid == openScenePath)
                        {
                            Debug.Log("[SearchResultSet] searching in scene:" + sr.pathInfo.FullPath());
                            UnityEngine.Object obj = sr.pathInfo.objID.searchForObjectInScene();
                            if (obj != null)
                            {
                                selections.Add(obj.GetInstanceID());
                            }
                        }
                        else
                        {
                            unSelectableItems++;
                        }
                    }
                    else
                    {
                        selections.Add(sr.pathInfo.gameObjectID);
                    }
                }
            }
            if (unSelectableItems > 0)
            {
                Debug.LogWarning("[Search & Replace] " + unSelectableItems + " items were not selected because they are not within the active scene, have been moved, or deleted.");
            }

            int[] selectionsArray = new int[selections.Count];
            selections.CopyTo(selectionsArray);
            Selection.instanceIDs = selectionsArray;
        }
        public override void ProcessAsset(SearchJob job)
        {
            scene = SceneUtil.LoadScene(assetPath, OpenSceneMode.Single).Value;
            if (roots == null)
            {
                roots        = scene.GetRootGameObjects();
                shouldUnload = true;
            }

            job.OnAssetSearchBegin();
            foreach (GameObject root in roots)
            {
                job.searchGameObject(root);
            }
            job.OnAssetSearchEnd();
            //  yield return job.searchDependencies(roots.ToArray());

            if (assetIsDirty)
            {
                EditorSceneManager.SaveScene(scene);
            }
        }
Exemple #12
0
 void parseIfSceneObject()
 {
     if (obj != null)
     {
         if (guid == "" || guid == SceneUtil.GuidPathForActiveScene())
         {
             isSceneObject = true;
             guid          = SceneUtil.GuidPathForActiveScene();
             // Debug.Log("[ObjectID] Setting to scene object:"+guid);
             localPath = PathData.ToPathData(obj);
         }
         else
         {
             isSceneObject = false;
             localPath     = null;
         }
     }
     else
     {
         isSceneObject = false;
         localPath     = null;
     }
 }
Exemple #13
0
        public Object GetSelection(bool openInProject)
        {
            string extension = Path.GetExtension(pathInfo.assetPath);

            switch (extension)
            {
            case ".unity":
                if (openInProject)
                {
                    // it is not possible to open a scene object in the project.
                    return(null);
                }
                Scene currentScene = EditorSceneManager.GetActiveScene();
                if (currentScene.path != pathInfo.assetPath)
                {
                    bool shouldContinue = SceneUtil.SaveDirtyScenes();
                    if (!shouldContinue)
                    {
                        return(null);
                    }
                    currentScene = EditorSceneManager.OpenScene(pathInfo.assetPath, OpenSceneMode.Single);
                }
#if UNITY_2018_3_OR_NEWER
                PrefabStage currentPrefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                if (currentPrefabStage != null)
                {
                    StageUtility.GoToMainStage();
                }
#endif
                return(pathInfo.objID.searchForObjectInScene(currentScene));

            case ".prefab":
#if UNITY_2018_3_OR_NEWER
                if (openInProject)
                {
                    GameObject root = AssetDatabase.LoadAssetAtPath <GameObject>(pathInfo.assetPath);
                    return(pathInfo.objID.localPath.GetObject(root));
                }
                else
                {
                    // is the prefab open in the stage?
                    PrefabStage stage = PrefabStageUtility.GetCurrentPrefabStage();
                    GameObject  root  = null;
                    if (stage == null || stage.assetPath != pathInfo.assetPath)
                    {
                        // this is not the currently open path. open the latest.
                        root = AssetDatabase.LoadAssetAtPath <GameObject>(pathInfo.assetPath);
                        AssetDatabase.OpenAsset(root);
                    }
                    else
                    {
                        root = stage.prefabContentsRoot;
                    }
                    return(pathInfo.objID.searchForObjectInScene());
                }
#else
                // This case is pretty straightforward. Just select the object.
                return(EditorUtility.InstanceIDToObject(pathInfo.gameObjectID));
#endif
            case ".controller":
                EditorApplication.ExecuteMenuItem("Window/Animator");
                // now get the animator to display it.
                string             assetPath = AssetDatabase.GetAssetPath(pathInfo.gameObjectID);
                UnityEngine.Object assetObj  = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityEngine.Object));
                //set it as the selection.
                Selection.activeObject = assetObj;
                //wait until inspectors are updated and now we can set the
                // selection to the internal object.
                EditorApplication.delayCall += () => {
                    Selection.activeInstanceID = pathInfo.gameObjectID;
                };
                return(assetObj);

            default:
                return(EditorUtility.InstanceIDToObject(pathInfo.gameObjectID));
            }
        }
Exemple #14
0
        public virtual void Draw()
        {
            GUIStyle resultStyle = alternate ? SRWindow.resultStyle1 : SRWindow.resultStyle2;

            if (selectedResult == this)
            {
                resultStyle = SRWindow.selectedStyle;
            }
            GUILayout.BeginHorizontal(resultStyle);
            string labelStr = "";
            string template = "";

            switch (actionTaken)
            {
            case SearchAction.Found:
                //search only.
                template = SRWindow.Instance.Compact() ? foundCompact : found;
                break;

            case SearchAction.Replaced:
                template = SRWindow.Instance.Compact() ? replacedCompact : replaced;
                break;

            case SearchAction.InstanceFound:
                template = SRWindow.Instance.Compact() ? instanceFoundCompact : instanceFound;
                break;

            case SearchAction.InstanceReplaced:
                template = SRWindow.Instance.Compact() ? instanceReplacedCompact : instanceReplaced;
                break;

            case SearchAction.Error:
                template = SRWindow.Instance.Compact() ? errorTemplateCompact : errorTemplate;
                break;

            case SearchAction.NotFound:
                template = SRWindow.Instance.Compact() ? notFoundCompact : notFound;
                break;

            default:
                template = unknown;
                break;
            }
            labelStr = format(template);
            float      width   = SRWindow.Instance.position.width - 80;
            GUIContent content = new GUIContent(labelStr);
            float      height  = SRWindow.richTextStyle.CalcHeight(content, width);

            EditorGUILayout.SelectableLabel(labelStr, SRWindow.richTextStyle, GUILayout.Height(height));
            Texture2D icon = SRWindow.prefabIcon;

            if (pathInfo.objID.isSceneObject)
            {
                icon = SRWindow.goIcon;
            }
            if (GUILayout.Button(icon, new GUILayoutOption[] { GUILayout.Width(30), GUILayout.Height(20) }))
            {
                if (pathInfo.objID.isSceneObject)
                {
                    SceneUtil.OpenObjectInScene(pathInfo);
                }
                else
                {
                    Selection.activeInstanceID = pathInfo.gameObjectID;
                    if (AnimationUtil.isInternalAnimationObject(Selection.activeObject))
                    {
                        // first show the animator window.
                        EditorApplication.ExecuteMenuItem("Window/Animator");
                        // now get the animator to display it.
                        string             assetPath = AssetDatabase.GetAssetPath(pathInfo.gameObjectID);
                        UnityEngine.Object assetObj  = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityEngine.Object));
                        //set it as the selection.
                        Selection.activeObject = assetObj;
                        //wait until inspectors are updated and now we can set the
                        // selection to the internal object.
                        EditorApplication.delayCall += () => {
                            Selection.activeInstanceID = pathInfo.gameObjectID;
                        };
                    }
                }
                selectedResult = this;
            }
            GUILayout.EndHorizontal();
        }
Exemple #15
0
        public void Execute()
        {
            addIgnorableResources();
            string[] assetPaths = AssetDatabase.GetAllAssetPaths();
            AnimationMode.StopAnimationMode(); // If recording is on, it will hose the animation.

            subjobs.Add(new SearchSubJob(this, AssetScope.Prefabs, assetPaths, new string[] { ".prefab" }, false));

            //scenes are oh so 'special'.
            sceneSubJob = new SceneSubJob(this, AssetScope.Scenes, assetPaths, new string[] { ".unity" });
            subjobs.Add(sceneSubJob);
            subjobs.Add(new SearchSubJob(this, AssetScope.Materials, assetPaths, new string[] { ".mat" }, false));
            subjobs.Add(new SearchSubJob(this, AssetScope.ScriptableObjects, assetPaths, new string[] { ".asset" }, false));
            subjobs.Add(new SearchSubJob(this, AssetScope.Animations, assetPaths, new string[] { ".anim" }, false));
            subjobs.Add(new SearchSubJob(this, AssetScope.Animators, assetPaths, new string[] { ".controller" }, false));
            subjobs.Add(new SearchSubJob(this, AssetScope.AudioClips, assetPaths, new string[] { ".wav", ".mp3" }, true));
            subjobs.Add(new SearchSubJob(this, AssetScope.Textures, assetPaths, new string[] { ".png", ".psd", ".tiff", ".tif", ".tga", ".gif", ".bmp", ".jpg", ".jpeg", ".iff", ".pict" }, true));
            subjobs.Add(new SceneObjectSubJob(this, AssetScope.None, assetPaths, new string[] { "" }));

            foreach (SearchSubJob subjob in subjobs)
            {
                totalItems += subjob.assetPaths.Count;
            }
            search.OnSearchBegin();
            result        = new SearchResultSet(search);
            result.status = SearchStatus.InProgress;

            currentItem = 1;

            if (sceneSubJob.assetPaths.Count > 0 && scope.projectScope != ProjectScope.CurrentScene)
            {
                bool shouldContinue = SceneUtil.SaveDirtyScenes();
                if (!shouldContinue)
                {
                    userAbortedSearch();
                    return;
                }
            }
            bool cancelled = false;

            try{
                foreach (SearchSubJob subjob in subjobs)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (!cancelled)
                        {
                            cancelled = progress();
                        }
                        while (!cancelled && subjob.SearchNextAsset())
                        {
                            if (result.resultsCount > maxItemsAllowed && !shownMaxItemsWarning)
                            {
                                shownMaxItemsWarning = true;
                                bool userContinues = EditorUtility.DisplayDialog("Too Many Results", "The search and replace plugin has found " + result.resultsCount + " results so far. Do you want to continue searching?", "Continue", "Cancel");
                                if (!userContinues)
                                {
                                    cancelled = true;
                                }
                            }
                            currentItem++;
                        }
                    }
                }
            }catch (System.Exception ex) {
                Debug.LogException(ex);
                result.status    = SearchStatus.InProgress;
                result.statusMsg = "An exception occurred: " + ex.ToString();
                EditorUtility.ClearProgressBar();
            }

            EditorUtility.ClearProgressBar();

            foreach (SearchSubJob subjob in subjobs)
            {
                subjob.JobDone();
            }

            search.OnSearchEnd(this);

            if (cancelled)
            {
                userAbortedSearch();
            }

            //calculate searchedItems
            int searchedItems = 0;

            foreach (var kvp in searchAssetsData)
            {
                SearchAssetData data = kvp.Value;
                if (data.searchExecuted)
                {
                    searchedItems++;
                }
            }
            if (result.status == SearchStatus.InProgress)
            {
                //standard termination.
                result.searchedItems = searchedItems;
                result.status        = SearchStatus.Complete;
            }
            result.OnSearchComplete();

            string log = logBuilder.ToString();

            if (log.Length > 0)
            {
                Debug.Log("[Search & Replace] Log:\n" + log);
            }
        }
        public static List <SearchAssetData> GetAssets(SearchScopeData scope)
        {
            List <SearchAssetData> retVal = new List <SearchAssetData>();

            if (scope.projectScope == ProjectScope.EntireProject)
            {
                //If the asset scope is set and has this value OR the scope is not set at all (everything)
                HashSet <string>     suffixes      = scope.GetSuffixesForScope();
                string[]             allAssets     = AssetDatabase.GetAllAssetPaths();
                IEnumerable <string> filteredPaths = allAssets.Where(asset => asset.StartsWith("Assets/")).Where(asset => suffixes.Contains(Path.GetExtension(asset).ToLowerInvariant()));

                foreach (string asset in filteredPaths)
                {
                    AddAsset(asset, retVal);
                }
            }
            else if (scope.projectScope == ProjectScope.SceneView)
            {
                SceneViewSearchAssetData sceneView = new SceneViewSearchAssetData(SceneUtil.GuidPathForActiveScene());
                retVal.Add(sceneView);
            }
            else if (scope.projectScope == ProjectScope.SpecificLocation)
            {
                string[]         allAssets = null;
                HashSet <string> suffixes  = scope.GetSuffixesForScope();
                addObjectsInLocation(scope.scopeObj, ref allAssets, suffixes, retVal);
            }
            else if (scope.projectScope == ProjectScope.CurrentSelection)
            {
                string[] allAssets = null;
                SceneObjectSearchAssetData sceneObject = null;
                foreach (UnityEngine.Object obj in Selection.objects)
                {
                    HashSet <string> suffixes = scope.GetSuffixesForScope(SearchScope.allAssets);
                    ObjectID         objID    = new ObjectID(obj);
                    objID.Clear();
                    if (objID.isSceneObject)
                    {
                        GameObject go = (GameObject)obj;
                        if (sceneObject == null)
                        {
                            sceneObject = new SceneObjectSearchAssetData(go.scene.path);
                            retVal.Add(sceneObject);
                        }
                        sceneObject.AddObject(go);
                    }
                    else if (objID.isPrefabStageObject)
                    {
                        if (sceneObject == null)
                        {
                            sceneObject = new SceneObjectSearchAssetData(SceneUtil.GuidPathForActiveScene());
                            retVal.Add(sceneObject);
                        }
                        sceneObject.AddObject((GameObject)obj);
                    }
                    else
                    {
                        //Ignore asset scope for current selection.
                        addObjectsInLocation(objID, ref allAssets, suffixes, retVal);
                    }
                }
            }
            return(retVal);
        }