Example #1
0
        public static void CreateCollection()
        {
            if (isExecuted)
            {
                return;
            }

            EditorApplication.delayCall += () => isExecuted = false;

            var selections = Selection.gameObjects;

            if (selections.Length == 1)
            {
                var components    = selections[0].GetComponents <Component>();
                var mainComponent = Components.ChooseMainComponent(components);

                if (!mainComponent || mainComponent is Transform)
                {
                    Undo.AddComponent <Collection>(selections[0]);
                    isExecuted = true;
                    return;
                }
            }

            var collection = new GameObject("New Collection", typeof(Collection));

            Undo.RegisterCreatedObjectUndo(collection, "Create Collection");

            if (selections.Length > 0)
            {
                selections = selections
                             .OrderBy(x => x.transform.GetSiblingIndex()).Reverse().ToArray();

                var firstSelection = selections[selections.Length - 1];

                selections = selections
                             .Where(x => x.transform.parent == firstSelection.transform.parent).ToArray();

                var siblings      = new int[selections.Length];
                var folderSibling = firstSelection.transform.GetSiblingIndex();

                Undo.SetTransformParent(collection.transform, firstSelection.transform.parent, "Create Collection");

                for (int i = 0; i < selections.Length; i++)
                {
                    Undo.SetTransformParent(selections[i].transform, collection.transform, "Create Collection");
                    selections[i].transform.SetSiblingIndex(siblings[i]);
                }

                collection.name = CollectionNaming.ChooseCollectionName(firstSelection);
                collection.transform.SetSiblingIndex(folderSibling);

                SmartHierarchy.active.window.FrameObject(firstSelection.GetInstanceID());
            }

            Selection.activeGameObject = collection;

            isExecuted = true;
        }
        public static void CreateCollection()
        {
            if (isExecuted)
            {
                return;
            }

            EditorApplication.delayCall += () => isExecuted = false;

            var folder = new GameObject("New Collection", typeof(Collection));

            Undo.RegisterCreatedObjectUndo(folder, "Create Collection");

            var selections = Selection.gameObjects;

            if (selections.Length > 0)
            {
                selections = selections
                             .OrderBy(x => x.transform.GetSiblingIndex()).Reverse().ToArray();

                var firstSelection = selections[selections.Length - 1];

                selections = selections
                             .Where(x => x.transform.parent == firstSelection.transform.parent).ToArray();

                var siblings      = new int[selections.Length];
                var folderSibling = firstSelection.transform.GetSiblingIndex();

                Undo.SetTransformParent(folder.transform, firstSelection.transform.parent, "Create Collection");

                for (int i = 0; i < selections.Length; i++)
                {
                    Undo.SetTransformParent(selections[i].transform, folder.transform, "Create Collection");
                    selections[i].transform.SetSiblingIndex(siblings[i]);
                }

                folder.name = CollectionNaming.ChooseCollectionName(firstSelection);
                folder.transform.SetSiblingIndex(folderSibling);

                SmartHierarchy.lastHierarchy.window.FrameObject(firstSelection.GetInstanceID());
                Selection.activeGameObject = folder;
            }
            isExecuted = true;
        }