Esempio n. 1
0
        private static bool RevealAndSelectGameObjectInPrefab(string enclosingAssetPath, string transformPath, long objectId, long componentId)
        {
            var targetAsset = AssetDatabase.LoadMainAssetAtPath(enclosingAssetPath) as GameObject;

            if (targetAsset == null)
            {
                return(false);
            }

            Object target = CSObjectTools.FindChildGameObjectRecursive(targetAsset.transform, objectId, targetAsset.transform.name, transformPath);

            // in some cases, prefabs can have nested non-GameObject items
            if (target == null)
            {
                var allObjectsInPrefab = AssetDatabase.LoadAllAssetsAtPath(enclosingAssetPath);

                foreach (var objectOnPrefab in allObjectsInPrefab)
                {
                    if (objectOnPrefab is BillboardAsset || objectOnPrefab is TreeData)
                    {
                        var objectOnPrefabId = CSObjectTools.GetUniqueObjectId(objectOnPrefab);
                        if (objectOnPrefabId == objectId)
                        {
                            target = objectOnPrefab;
                        }
                    }
                }
            }

            if (target == null)
            {
                Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + enclosingAssetPath + " with ObjectID " + objectId + "!"));
                return(false);
            }

            if (target is GameObject)
            {
                CSObjectTools.SelectGameObject((GameObject)target, false);
            }
            else
            {
                Selection.activeObject = target;
            }

            if (transformPath.Split('/').Length > 2)
            {
                EditorApplication.delayCall += () =>
                {
                    EditorGUIUtility.PingObject(targetAsset);
                };
            }

            if (componentId != -1)
            {
                return(TryFoldAllComponentsExceptId(componentId));
            }

            return(true);
        }
Esempio n. 2
0
        private static bool RevealAndSelectGameObjectInPrefab(string enclosingAssetPath, string transformPath, long objectId, long componentId)
        {
            /*Debug.Log("LOOKING FOR objectId " + objectId);
             * Debug.Log("enclosingAssetPath " + enclosingAssetPath);*/

            var targetAsset = AssetDatabase.LoadMainAssetAtPath(enclosingAssetPath) as GameObject;
            var prefabType  = PrefabUtility.GetPrefabAssetType(targetAsset);

            GameObject target;

            if (prefabType == PrefabAssetType.Model)
            {
                target = targetAsset;
            }
            else
            {
                if (!AssetDatabase.OpenAsset(targetAsset))
                {
                    Debug.LogError(Maintainer.ConstructError("Couldn't open prefab at " + enclosingAssetPath + "!"));
                    return(false);
                }

                var stage = PrefabStageUtility.GetCurrentPrefabStage();
                if (stage == null)
                {
                    Debug.LogError(Maintainer.ConstructError("Couldn't get prefab stage for prefab at " + enclosingAssetPath + "!"));
                    return(false);
                }

                target = stage.prefabContentsRoot;
            }

            if (target == null)
            {
                Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + enclosingAssetPath + " with ObjectID " + objectId + "!"));
                return(false);
            }

            target = CSObjectTools.FindChildGameObjectRecursive(target.transform, objectId, target.transform.name, transformPath);

            EditorApplication.delayCall += () =>
            {
                CSObjectTools.SelectGameObject(target, false);
                EditorGUIUtility.PingObject(targetAsset);

                if (componentId != -1)
                {
                    EditorApplication.delayCall += () =>
                    {
                        TryFoldAllComponentsExceptId(componentId);
                    };
                }
            };

            return(true);
        }
Esempio n. 3
0
        private static bool RevealAndSelectGameObjectInScene(string path, string transformPath, long objectId, long componentId)
        {
            Scene targetScene;

            if (!string.IsNullOrEmpty(path))
            {
                var openResult = OpenSceneForReveal(path);
                if (!openResult.success)
                {
                    return(false);
                }

                targetScene = openResult.scene;
            }
            else
            {
                targetScene = CSSceneTools.GetUntitledScene();
            }

            if (!targetScene.IsValid())
            {
                Debug.LogError(Maintainer.ConstructError("Target scene is not valid or not found! Scene path: " + path + ", looked for ObjectID " + objectId + "!"));
                return(false);
            }

            var target = CSObjectTools.FindGameObjectInScene(targetScene, objectId, transformPath);

            if (target == null)
            {
                Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + path + " with ObjectID " + objectId + "!"));
                return(false);
            }

            // workaround for a bug when Unity doesn't expand hierarchy in scene
            EditorApplication.delayCall += () =>
            {
                EditorGUIUtility.PingObject(target);
            };

            CSObjectTools.SelectGameObject(target, true);

            var enclosingAssetInstanceId = CSAssetTools.GetMainAssetInstanceID(path);

            EditorApplication.delayCall += () =>
            {
                EditorGUIUtility.PingObject(enclosingAssetInstanceId);
            };

            if (componentId != -1)
            {
                return(TryFoldAllComponentsExceptId(componentId));
            }

            return(true);
        }
Esempio n. 4
0
        private static bool RevealAndSelectGameObjectInScene(string enclosingAssetPath, string transformPath, long objectId, long componentId)
        {
            var openResult = OpenSceneForReveal(enclosingAssetPath);

            if (!openResult.success)
            {
                return(false);
            }

            var target = CSObjectTools.FindGameObjectInScene(openResult.scene, objectId, transformPath);

            if (target == null)
            {
                Debug.LogError(Maintainer.ConstructError("Couldn't find target Game Object " + transformPath + " at " + enclosingAssetPath + " with ObjectID " + objectId + "!"));
                return(false);
            }

            // workaround for a bug when Unity doesn't expand hierarchy in scene
            EditorApplication.delayCall += () =>
            {
                EditorGUIUtility.PingObject(target);
            };

            CSObjectTools.SelectGameObject(target, true);

            var enclosingAssetInstanceId = CSAssetTools.GetMainAssetInstanceID(enclosingAssetPath);

            EditorApplication.delayCall += () =>
            {
                EditorGUIUtility.PingObject(enclosingAssetInstanceId);
            };

            if (componentId != -1)
            {
                return(TryFoldAllComponentsExceptId(componentId));
            }

            return(true);
        }
Esempio n. 5
0
        public static bool RevealAndSelectSubAsset(string assetPath, string name, long objectId)
        {
            var targetAssets = AssetDatabase.LoadAllAssetsAtPath(assetPath);

            if (targetAssets == null || targetAssets.Length == 0)
            {
                return(false);
            }

            foreach (var targetAsset in targetAssets)
            {
                if (!AssetDatabase.IsSubAsset(targetAsset))
                {
                    continue;
                }
                if (targetAsset is GameObject || targetAsset is Component)
                {
                    continue;
                }
                if (!string.Equals(targetAsset.name, name, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                var assetId = CSObjectTools.GetUniqueObjectId(targetAsset);
                if (assetId != objectId)
                {
                    continue;
                }

                Selection.activeInstanceID = targetAsset.GetInstanceID();
                return(true);
            }

            return(false);
        }
 public static bool IsComponentVisibleInInspector(Component component)
 {
     return(component == null || !CSObjectTools.IsHiddenInInspector(component));
 }
Esempio n. 7
0
        public static bool TryFoldAllComponentsExceptId(long componentId)
        {
            var tracker = CSEditorTools.GetActiveEditorTrackerForSelectedObject();

            if (tracker == null)
            {
                Debug.LogError(Maintainer.ConstructError("Can't get active tracker."));
                return(false);
            }

            tracker.RebuildIfNecessary();

            var editors = tracker.activeEditors;

            if (editors.Length > 1)
            {
                var targetFound = false;
                var skipCount   = 0;

                for (var i = 0; i < editors.Length; i++)
                {
                    var editor           = editors[i];
                    var editorTargetType = editor.target.GetType();
                    if (editorTargetType == CSReflectionTools.assetImporterType ||
                        editorTargetType == CSReflectionTools.gameObjectType)
                    {
                        skipCount++;
                        continue;
                    }

                    if (i - skipCount == componentId)
                    {
                        targetFound = true;

                        /* known corner cases when editor can't be set to visible via tracker */

                        if (editor.serializedObject.targetObject is ParticleSystemRenderer)
                        {
                            var renderer = (ParticleSystemRenderer)editor.serializedObject.targetObject;
                            var ps       = renderer.GetComponent <ParticleSystem>();
                            componentId = CSObjectTools.GetComponentIndex(ps);
                        }

                        break;
                    }
                }

                if (!targetFound)
                {
                    return(false);
                }

#if UNITY_2018_3_OR_NEWER
                for (var i = 1; i < editors.Length; i++)
#else
                for (var i = 0; i < editors.Length; i++)
#endif
                {
                    tracker.SetVisible(i, i - skipCount != componentId ? 0 : 1);
                }

                var inspectorWindow2 = CSEditorTools.GetInspectorWindow();
                if (inspectorWindow2 != null)
                {
                    inspectorWindow2.Repaint();
                }

                // workaround for bug when tracker selection gets reset after scene open
                // (e.g. revealing TMP component in new scene)
                EditorApplication.delayCall += () =>
                {
                    EditorApplication.delayCall += () =>
                    {
                        try
                        {
                            for (var i = 0; i < editors.Length; i++)
                            {
                                tracker.SetVisible(i, i - skipCount != componentId ? 0 : 1);
                            }

                            var inspectorWindow = CSEditorTools.GetInspectorWindow();
                            if (inspectorWindow != null)
                            {
                                inspectorWindow.Repaint();
                            }
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    };
                };
            }

            return(true);
        }