public void TestRemoveRedundantObjects()
        {
            var root   = CreateGameObject("root");
            var child1 = CreateGameObject("child1", root.transform);
            var child2 = CreateGameObject("child2", root.transform);
            var root2  = CreateGameObject("root2");

            // test set: root
            // expected result: root
            var result = ModelExporter.RemoveRedundantObjects(new Object[] { root });

            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.Contains(root));

            // test set: root, child1
            // expected result: root
            result = ModelExporter.RemoveRedundantObjects(new Object[] { root, child1 });
            Assert.AreEqual(1, result.Count);
            Assert.IsTrue(result.Contains(root));

            // test set: root, child1, child2, root2
            // expected result: root, root2
            result = ModelExporter.RemoveRedundantObjects(new Object[] { root, root2, child2, child1 });
            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result.Contains(root));
            Assert.IsTrue(result.Contains(root2));

            // test set: child1, child2
            // expected result: child1, child2
            result = ModelExporter.RemoveRedundantObjects(new Object[] { child2, child1 });
            Assert.AreEqual(2, result.Count);
            Assert.IsTrue(result.Contains(child1));
            Assert.IsTrue(result.Contains(child2));
        }
        public static GameObject[] CreateInstantiatedModelPrefab(
            GameObject [] unityGameObjectsToConvert)
        {
            var toExport = ModelExporter.RemoveRedundantObjects(unityGameObjectsToConvert);

            if (ExportSettings.instance.ShowConvertToPrefabDialog)
            {
                ConvertToPrefabEditorWindow.Init(toExport);
                return(toExport.ToArray());
            }

            var converted     = new List <GameObject>();
            var exportOptions = ExportSettings.instance.ConvertToPrefabSettings.info;

            foreach (var go in toExport)
            {
                var convertedGO = Convert(go, exportOptions: exportOptions);
                if (convertedGO != null)
                {
                    converted.Add(convertedGO);
                }
            }
            return(converted.ToArray());
        }
        public static GameObject[] CreateInstantiatedModelPrefab(
            GameObject[] unityGameObjectsToConvert)
        {
            var toExport = ModelExporter.RemoveRedundantObjects(unityGameObjectsToConvert);

            if (ExportSettings.instance.DisplayOptionsWindow)
            {
                if (toExport.Count == 1)
                {
                    var go = toExport.First();
                    if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go) && !PrefabUtility.IsOutermostPrefabInstanceRoot(go))
                    {
                        DisplayInvalidSelectionDialog(go,
                                                      "Children of a Prefab instance cannot be converted.\nYou can open the Prefab in Prefab Mode or unpack the Prefab instance to convert it's children");
                        return(null);
                    }

                    if (PrefabUtility.IsPartOfPrefabAsset(go) && go.transform.parent != null)
                    {
                        DisplayInvalidSelectionDialog(go,
                                                      "Children of a Prefab Asset cannot be converted.\nYou can open the Prefab in Prefab Mode or unpack the Prefab instance to convert it's children");
                        return(null);
                    }

                    // can't currently handle converting root of prefab in prefab preview scene
                    if (SceneManagement.EditorSceneManager.IsPreviewSceneObject(go) && go.transform.parent == null)
                    {
                        DisplayInvalidSelectionDialog(go,
                                                      "Cannot convert Prefab root in the Prefab Preview Scene.\nYou can convert a Prefab Instance or convert the Prefab Asset directly in the Project view");
                        return(null);
                    }
                }
                ConvertToPrefabEditorWindow.Init(toExport);
                return(toExport.ToArray());
            }

            bool onlyPrefabAssets = ConvertToNestedPrefab.SetContainsOnlyPrefabAssets(unityGameObjectsToConvert);
            int  groupIndex       = -1;

            // If only Prefab Assets on disk are selected (nothing in the scene), then do not
            // try to undo as modifications on disk cannot be undone.
            if (!onlyPrefabAssets)
            {
                Undo.IncrementCurrentGroup();
                groupIndex = Undo.GetCurrentGroup();
                Undo.SetCurrentGroupName(UndoConversionCreateObject);
            }
            var converted     = new List <GameObject>();
            var exportOptions = ExportSettings.instance.ConvertToPrefabSettings.info;

            foreach (var go in toExport)
            {
                var convertedGO = Convert(go, exportOptions: exportOptions);
                if (convertedGO != null)
                {
                    converted.Add(convertedGO);
                }
            }
            if (!onlyPrefabAssets && groupIndex >= 0)
            {
                Undo.CollapseUndoOperations(groupIndex);
                Undo.IncrementCurrentGroup();
            }
            return(converted.ToArray());
        }