public static Transform GroupLocally(Transform[] targets, string name = "Group")
        {
            var transformType = InferTransformTypeOrFail(targets);

            var group = CreateGroup(name, transformType);

            Undo.RegisterCreatedObjectUndo(group, "Group");

            var shallowestTarget = TransformOperations.FindShallowest(targets);

            Undo.SetTransformParent(group.transform, shallowestTarget.transform.parent, "Group");

            foreach (var target in targets)
            {
                Undo.SetTransformParent(target.transform, group.transform, "Group");
            }

            if (transformType == typeof(RectTransform))
            {
                TransformOperations.CenterOnPivots(group.transform);
            }
            else
            {
                TransformOperations.CenterOnBounds(group.transform);
            }

            return(group.transform);
        }
        public static void CreateParent(GameObject[] targets, Rect activatorPosition)
        {
            Validate(targets);

            var transformTargets = targets.Select(t => t.transform).ToArray();

            if (!TransformOperations.WarnRestructurable(transformTargets))
            {
                return;
            }

            var shallowestTarget = TransformOperations.FindShallowest(transformTargets);

            CreateMenu
            (
                activatorPosition, created =>
            {
                Undo.SetTransformParent(created.transform, shallowestTarget.transform.parent, "Create Parent");

                foreach (var target in targets)
                {
                    Undo.SetTransformParent(target.transform, created.transform, "Create Parent");
                }

                TransformOperations.CenterOnBounds(created.transform);

                Selection.activeGameObject = created;
                Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
            }
            );
        }
        public static Transform[] Ungroup(Transform target)
        {
            if (!IsGroup(target))
            {
                return(new[] { target });
            }

            for (var i = 0; i < target.childCount; i++)
            {
                if (!TransformOperations.WarnRestructurable(target.GetChild(i)))
                {
                    return(new[] { target });
                }
            }

            var ungrouped = new Transform[target.childCount];

            var siblingIndex = target.GetSiblingIndex();

            var j = 0;

            while (target.childCount > 0)
            {
                var child = target.GetChild(0);
                Undo.SetTransformParent(child, target.parent, "Ungroup");
                Undo.RecordObject(child, "Ungroup");
                child.SetSiblingIndex(siblingIndex);
                ungrouped[j] = child;
                j++;
            }

            Undo.DestroyObjectImmediate(target.gameObject);

            return(ungrouped);
        }
Exemple #4
0
        public static Transform GroupLocally(Transform[] targets, string name = "Group")
        {
            var transformType = InferTransformTypeOrFail(targets);

            var firstSiblingIndex = targets.Select(t => t.GetSiblingIndex()).Min();

            var group = CreateGroup(name, transformType);

            Undo.RegisterCreatedObjectUndo(group, "Group");

            var shallowestTarget = TransformOperations.FindShallowest(targets);

            Undo.SetTransformParent(group.transform, shallowestTarget.transform.parent, "Group");

            foreach (var target in targets.OrderBy(t => t.GetSiblingIndex()))
            {
                Undo.SetTransformParent(target.transform, group.transform, "Group");
            }

            Undo.RecordObject(group.transform, "Group");
            group.transform.SetSiblingIndex(firstSiblingIndex);

            if (transformType == typeof(RectTransform))
            {
                TransformOperations.CenterOnPivots(group.transform);
            }
            else
            {
                TransformOperations.CenterOnBounds(group.transform);
            }

            return(group.transform);
        }
        public static FuzzyWindow Open(GameObject[] targets, Rect activatorPosition)
        {
            if (!TransformOperations.WarnRestructurable(targets.Select(go => go.transform).ToArray()))
            {
                return(null);
            }

            // GameObject menu creators change the selection, so we need to cache it
            var selectionSnapshot = Selection.objects;

            LudiqGUI.FuzzyDropdown
            (
                activatorPosition,
                new CreateGameObjectOptionTree("Replace with..."),
                null,
                (_instance) =>
            {
                var template = (GameObject)_instance;

                var allSelected = new HashSet <GameObject>();

                foreach (var target in targets)
                {
                    var selected     = selectionSnapshot.Contains(target);
                    var position     = target.transform.position;
                    var rotation     = target.transform.rotation;
                    var scale        = target.transform.localScale;
                    var parent       = target.transform.parent;
                    var siblingIndex = target.transform.GetSiblingIndex();
                    var scene        = target.scene;

                    Undo.DestroyObjectImmediate(target);
                    var replacement = DuplicateGameObject(template);
                    Undo.MoveGameObjectToScene(replacement, scene, "Move Replacement To Scene");

                    replacement.transform.position = position;
                    replacement.transform.rotation = rotation;

                    if (PeekPlugin.Configuration.preserveScaleOnReplace)
                    {
                        replacement.transform.localScale = scale;
                    }

                    replacement.transform.SetParent(parent, true);
                    replacement.transform.SetSiblingIndex(siblingIndex);

                    if (selected)
                    {
                        allSelected.Add(replacement);
                    }
                }

                Selection.objects = allSelected.ToArray();

                UnityObject.DestroyImmediate(template);
            }
            );

            return(FuzzyWindow.instance);
        }
Exemple #6
0
        public static void Ungroup(Transform target)
        {
            if (!TransformOperations.WarnRestructurable(target))
            {
                return;
            }

            var ungrouped = GroupUtility.Ungroup(target);

            Selection.objects = ungrouped.Select(t => t.gameObject).ToArray();
        }
        public static Transform GroupLocally(Transform[] targets, string name = "Group")
        {
            var transformType   = InferTransformTypeOrFail(targets);
            var scene           = TransformOperations.FindCommonScene(targets);
            var haveCommonScene = scene != null;

            scene = scene ?? EditorSceneManager.GetActiveScene();

            var firstSiblingIndex = targets.Select(t => t.GetSiblingIndex()).Min();

            var group = CreateGroup(name, transformType);

            Undo.RegisterCreatedObjectUndo(group, "Group");
            Undo.MoveGameObjectToScene(group.gameObject, scene.Value, "Group");

            if (haveCommonScene)
            {
                var shallowestTarget = TransformOperations.FindShallowest(targets);
                Undo.SetTransformParent(group.transform, shallowestTarget.transform.parent, "Group");
            }

            foreach (var target in targets.OrderBy(t => t.GetSiblingIndex()))
            {
                Undo.SetTransformParent(target.transform, null, "Group");
                Undo.MoveGameObjectToScene(target.gameObject, scene.Value, "Group");
                Undo.SetTransformParent(target.transform, group.transform, "Group");
            }

            Undo.RecordObject(group.transform, "Group");

            if (haveCommonScene)
            {
                group.transform.SetSiblingIndex(firstSiblingIndex);
            }

            if (transformType == typeof(RectTransform))
            {
                TransformOperations.CenterOnPivots(group.transform);
            }
            else
            {
                TransformOperations.CenterOnBounds(group.transform);
            }

            return(group.transform);
        }
        public static Transform GroupGlobally(Transform[] targets, string name = "Group")
        {
            var transformType = InferTransformTypeOrFail(targets);
            var scene         = TransformOperations.FindCommonScene(targets) ?? EditorSceneManager.GetActiveScene();

            var group = CreateGroup(name, transformType);

            Undo.RegisterCreatedObjectUndo(group, "Group");
            Undo.MoveGameObjectToScene(group.gameObject, scene, "Group");

            foreach (var target in targets.OrderBy(t => t.GetSiblingIndex()))
            {
                Undo.MoveGameObjectToScene(target.gameObject, scene, "Group");
                Undo.SetTransformParent(target.transform, group.transform, "Group");
            }

            return(group.transform);
        }
Exemple #9
0
        public static void GroupGlobally(Transform[] targets, string defaultName = "Group")
        {
            if (!TransformOperations.WarnRestructurable(targets))
            {
                return;
            }

            if (!WarnTransformType(targets))
            {
                return;
            }

            if (GroupNamePrompt.Prompt(out var name, defaultName))
            {
                var group = GroupUtility.GroupGlobally(targets, name);
                Selection.activeTransform = group;
            }
        }
Exemple #10
0
        public static void StartReparenting(Transform[] targets)
        {
            Ensure.That(nameof(targets)).IsNotNull(targets);
            Ensure.That(nameof(targets)).HasItems(targets);

            if (!TransformOperations.WarnRestructurable(targets))
            {
                return;
            }

            ReparentingPrompt.Prompt
            (
                TransformOperations.FindCommonScene(targets),
                (parentScene, parentTransform) =>
            {
                var reparented = false;

                if (parentTransform != null)
                {
                    ReparentingUtility.Reparent(targets, parentTransform);
                    reparented = true;
                }
                else if (parentScene != null)
                {
                    ReparentingUtility.Reparent(targets, parentScene.Value);
                    reparented = true;
                }

                if (reparented)
                {
                    Selection.objects = targets.Select(t => t.gameObject).ToArray();

                    EditorGUIUtility.PingObject(targets.OrderBy(t => t.GetSiblingIndex()).First());
                }
            }
            );
        }
        private static void CenterOnBounds(MenuCommand menuCommand)
        {
            var target = GetTarget(menuCommand);

            GuiCallback.Enqueue(() => { TransformOperations.CenterOnBounds(target); });
        }
        private static void Bake(MenuCommand menuCommand)
        {
            var target = GetTarget(menuCommand);

            GuiCallback.Enqueue(() => { TransformOperations.Bake(target); });
        }