Exemplo n.º 1
0
        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);
        }
        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 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);
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
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;
            }
        }
Exemplo n.º 6
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());
                }
            }
            );
        }