Esempio n. 1
0
        private void ProcessNewGameObjects(IEnumerable <Transform> transforms)
        {
            using (var pooledNewReferences = ListPool <Guid> .GetDisposable())
            {
                var newReferences = pooledNewReferences.List;
                foreach (var t in transforms)
                {
                    var go     = t.gameObject;
                    var status = PrefabUtility.GetPrefabInstanceStatus(go);
                    if (status != PrefabInstanceStatus.NotAPrefab)
                    {
                        // TODO: handle dragging prefabs
                    }
                    else
                    {
                        var reference = go.GetComponent <EntityReference>();
                        if (!reference)
                        {
                            reference = go.AddComponent <EntityReference>();
                        }

                        reference.Guid = Guid.NewGuid();

                        CreateEntityFromGameObject(go, reference);

                        newReferences.Add(reference.Guid);
                    }
                }

                m_WorldManager.RebuildGuidCache();

                var array = new NativeArray <Guid>(newReferences.Count, Allocator.Temp,
                                                   NativeArrayOptions.UninitializedMemory);
                try
                {
                    for (int i = 0; i < newReferences.Count; ++i)
                    {
                        array[i] = newReferences[i];
                    }

                    TransferBack(array);
                }
                finally
                {
                    array.Dispose();
                }

                if (newReferences.Count > 0)
                {
                    EntityHierarchyWindow.SelectOnNextPaint(newReferences);
                }
            }
        }
        private static Entity CreateEntity(EntityArchetype archetype, string name, Guid parentGuid = default)
        {
            var session       = Application.AuthoringProject.Session;
            var worldManager  = session.GetManager <IWorldManager>();
            var entityManager = worldManager.EntityManager;
            var sceneManager  = session.GetManager <IEditorSceneManagerInternal>();

            var parent       = worldManager.GetEntityFromGuid(parentGuid);
            var parentExists = parent != Entity.Null && entityManager.Exists(parent);
            var scene        = !parentExists?sceneManager.GetActiveScene() : sceneManager.GetScene(parent);

            Assert.AreNotEqual(scene, Scene.Null);

            var graph      = sceneManager.GetGraphForScene(scene);
            var uniqueName = EntityNameHelper.GetUniqueEntityName(name, worldManager, parentExists ? graph.FindNode(parent)?.Children : graph.Roots);

            var entity = worldManager.CreateEntity(uniqueName, archetype);

            scene.AddEntityReference(entityManager, entity);

            if (parentExists)
            {
                if (entityManager.HasComponent <Parent>(entity))
                {
                    entityManager.SetComponentData(entity, new Parent {
                        Value = parent
                    });
                }
                else
                {
                    entityManager.AddComponentData(entity, new Parent {
                        Value = parent
                    });
                }
            }

            SetComponentDefaultValue <NonUniformScale>(entity);
            SetComponentDefaultValue <SiblingIndex>(entity);

            EntityHierarchyWindow.SelectOnNextPaint(worldManager.GetEntityGuid(entity).AsEnumerable().ToList());
            return(entity);
        }
        public static void CreateEmptyChild(CommandExecuteContext context = null)
        {
            var session      = Application.AuthoringProject.Session;
            var worldManager = session.GetManager <IWorldManager>();

            using (var pooled = ListPool <Entity> .GetDisposable())
            {
                var list = pooled.List;
                foreach (var guid in SelectionUtility.GetEntityGuidSelection())
                {
                    list.Add(CreateEntity(Application.AuthoringProject.Session.GetManager <IArchetypeManager>().Empty, "Entity", guid));
                }

                if (list.Count == 0)
                {
                    list.Add(CreateEntity(Application.AuthoringProject.Session.GetManager <IArchetypeManager>().Empty, "Entity", default));
                }

                EntityHierarchyWindow.SelectOnNextPaint(list.Select(worldManager.GetEntityGuid).ToList());
            }
        }
Esempio n. 4
0
        public static void DuplicateSelection(CommandExecuteContext context)
        {
            var session         = Application.AuthoringProject?.Session;
            var worldManager    = session.GetManager <IWorldManager>();
            var m_EntityManager = worldManager.EntityManager;
            var m_SceneManager  = session.GetManager <IEditorSceneManagerInternal>();

            using (var pooled = ListPool <ISceneGraphNode> .GetDisposable())
            {
                var toSelect  = pooled.List;
                var selection = SelectionUtility.GetEntityGuidSelection();

                foreach (var group in selection.Select(worldManager.GetEntityFromGuid)
                         .GroupBy(e => m_EntityManager.GetSharedComponentData <SceneGuid>(e)))
                {
                    var graph = m_SceneManager.GetGraphForScene(group.Key);
                    var list  = group.Select(graph.FindNode).Cast <ISceneGraphNode>().ToList();
                    toSelect.AddRange(graph.Duplicate(list));
                }

                EntityHierarchyWindow.SelectOnNextPaint(toSelect.OfType <EntityNode>().Select(e => e.Guid).ToList());
            }
        }
Esempio n. 5
0
        private UndoPropertyModification[] HandleInvertedChanges(UndoPropertyModification[] mods)
        {
            using (var pooledModifications = ListPool <Guid> .GetDisposable())
                using (var pooledNewReferences = ListPool <Guid> .GetDisposable())
                {
                    var modifications = pooledModifications.List;
                    var newReferences = pooledNewReferences.List;

                    modifications.AddRange(mods.Select(m => m.currentValue?.target)
                                           .Select(o =>
                    {
                        if (!o)
                        {
                            return(Guid.Empty);
                        }

                        var go = (o is GameObject gameObj) ? gameObj : ((o is Component comp) ? comp.gameObject : null);
                        if (go)
                        {
                            var reference = go.GetComponent <EntityReference>();
                            //
                            if (!reference || null == reference)
                            {
                                reference      = go.AddComponent <EntityReference>();
                                reference.Guid = Guid.NewGuid();
                                CreateEntityFromGameObject(go, reference);
                                newReferences.Add(reference.Guid);
                                return(reference.Guid);
                            }

                            return(reference.Guid);
                        }

                        return(Guid.Empty);
                    })
                                           .Where(g => g != Guid.Empty)
                                           .Distinct());

                    if (modifications.Count > 0)
                    {
                        m_WorldManager.RebuildGuidCache();

                        var array = new NativeArray <Guid>(modifications.Count, Allocator.Temp,
                                                           NativeArrayOptions.UninitializedMemory);
                        try
                        {
                            for (int i = 0; i < modifications.Count; ++i)
                            {
                                array[i] = modifications[i];
                            }

                            TransferBack(array);
                        }
                        finally
                        {
                            array.Dispose();
                        }

                        if (newReferences.Count > 0)
                        {
                            EntityHierarchyWindow.SelectOnNextPaint(newReferences);
                        }
                    }

                    return(mods);
                }
        }