コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
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());
                }
            }
            );
        }