Exemple #1
0
        private static void GenerateNewProjectAssets(
            DirectoryInfo projectDirectory,
            string name,
            IWorldManager worldManager,
            IArchetypeManager archetypeManager,
            IPersistenceManager persistenceManager
            )
        {
            // Create config entity
            var configEntity = worldManager.CreateEntity(archetypeManager.Config);

            worldManager.EntityManager.SetComponentData(configEntity, DisplayInfo.Default);

            // Generate main scene
            var cameraEntity = worldManager.CreateEntity("Camera", archetypeManager.Camera);

            worldManager.EntityManager.SetComponentData(cameraEntity, Camera2D.Default);
            worldManager.EntityManager.SetComponentData(cameraEntity, DomainCache.GetDefaultValue <Camera2D>());
            worldManager.EntityManager.SetComponentData(cameraEntity, DomainCache.GetDefaultValue <Rotation>());
            worldManager.EntityManager.SetComponentData(cameraEntity, DomainCache.GetDefaultValue <NonUniformScale>());

            using (var entities = new NativeArray <Entity>(cameraEntity.AsArray(), Allocator.Temp))
            {
                var scene     = SceneManager.Create(worldManager.EntityManager, entities, Guid.NewGuid());
                var sceneFile = projectDirectory.Combine("Scenes").GetFile("MainScene.scene");
                sceneFile.Directory.EnsureExists();
                persistenceManager.SaveScene(worldManager.EntityManager, scene, sceneFile.FullName);

                var sceneReference = new SceneReference {
                    SceneGuid = scene.SceneGuid.Guid
                };

                AddScene(worldManager.EntityManager, worldManager.GetConfigEntity(), sceneReference);
                AddStartupScene(worldManager.EntityManager, worldManager.GetConfigEntity(), sceneReference);
            }

            // Generate configuration scene
            using (var entities = new NativeArray <Entity>(configEntity.AsArray(), Allocator.Temp))
            {
                var configurationScene = SceneManager.Create(worldManager.EntityManager, entities, ConfigurationScene.Guid);
                var configurationFile  = projectDirectory.GetFile(name).ChangeExtension("configuration");
                configurationFile.Directory.EnsureExists();
                persistenceManager.SaveScene(worldManager.EntityManager, configurationScene, configurationFile.FullName);

                // Hack: remove scene guid/instance id and persistence id
                worldManager.EntityManager.RemoveComponent <SceneGuid>(configEntity);
                worldManager.EntityManager.RemoveComponent <SceneInstanceId>(configEntity);
            }
        }
        private bool AddType(SearcherItem item)
        {
            if (!(item is TypeSearcherItem typedItem))
            {
                return(false);
            }

            item.Database.ItemList.Remove(item);

            foreach (var entity in Targets)
            {
                var type  = typedItem.Type;
                var index = TypeManager.GetTypeIndex(type);
                if (!EntityManager.HasComponent(entity, type))
                {
                    EntityManager.AddComponent(entity, type);
                    if (TypeManager.IsZeroSized(index))
                    {
                        continue;
                    }

                    if (typeof(IComponentData).IsAssignableFrom(type))
                    {
                        unsafe
                        {
                            var data         = EntityManager.GetComponentDataRawRW(entity, index);
                            var defaultValue = DomainCache.GetDefaultValue(type);
                            // TODO: Find a better way
                            typeof(UIElementsBackend)
                            .GetMethod(nameof(Copy), BindingFlags.NonPublic | BindingFlags.Instance)?
                            .MakeGenericMethod(type)
                            .Invoke(this, new [] { entity, index, defaultValue });
                        }
                    }
                }
            }
            EntityInspector.ForceRebuildAll();
            return(true);
        }
        static BuiltinHierarchyDragAndDrop()
        {
            HierarchyDragAndDrop <UnityEngine.Sprite, EntityHierarchyTree.Key> .SingleObjectDrop = (session, sprite, graph, parent, index) =>
            {
                var worldManager     = session.GetManager <IWorldManager>();
                var entityManager    = worldManager.EntityManager;
                var archetypeManager = session.GetManager <IArchetypeManager>();
                var assetManager     = session.GetManager <IAssetManager>();

                var entity   = entityManager.CreateEntity(archetypeManager.Sprite);
                var renderer = DomainCache.GetDefaultValue <Sprite2DRenderer>();
                renderer.sprite = assetManager.GetEntity(sprite);
                worldManager.SetEntityName(entity, sprite.name);
                entityManager.SetComponentData(entity, renderer);
                entityManager.SetComponentData(entity, DomainCache.GetDefaultValue <Rotation>());
                entityManager.SetComponentData(entity, DomainCache.GetDefaultValue <NonUniformScale>());
                entityManager.SetComponentData(entity, Guid.NewGuid().ToEntityGuid());

                var node = new EntityNode(graph, session, entity);
                if (null != parent)
                {
                    parent.Insert(index, node);
                }
                else
                {
                    graph.Insert(index, node);
                }

                return(node);
            };

            HierarchyDragAndDrop <UnityEngine.Texture2D, EntityHierarchyTree.Key> .SingleObjectDrop = (session, texture, graph, parent, index) =>
            {
                if (null == texture)
                {
                    return(null);
                }

                var path    = AssetDatabase.GetAssetPath(texture);
                var sprites = AssetDatabase.LoadAllAssetsAtPath(path).OfType <UnityEngine.Sprite>().ToArray();
                if (sprites.Length == 0)
                {
                    return(null);
                }

                // Create normal Sprite2DRenderer
                if (sprites.Length == 1)
                {
                    return(HierarchyDragAndDrop <UnityEngine.Sprite, EntityHierarchyTree.Key> .SingleObjectDrop(session, sprites[0], graph,
                                                                                                                parent, index));
                }

                // Create Sprite2DRendererSequence
                var worldManager     = session.GetManager <IWorldManager>();
                var entityManager    = worldManager.EntityManager;
                var archetypeManager = session.GetManager <IArchetypeManager>();
                var assetManager     = session.GetManager <IAssetManager>();

                var entity   = entityManager.CreateEntity(archetypeManager.SpriteSequence);
                var renderer = DomainCache.GetDefaultValue <Sprite2DRenderer>();
                renderer.sprite = assetManager.GetEntity(sprites[0]);
                worldManager.SetEntityName(entity, texture.name);
                entityManager.SetComponentData(entity, renderer);
                entityManager.SetComponentData(entity, DomainCache.GetDefaultValue <Rotation>());
                entityManager.SetComponentData(entity, DomainCache.GetDefaultValue <NonUniformScale>());
                entityManager.SetComponentData(entity, Guid.NewGuid().ToEntityGuid());
                entityManager.SetComponentData(entity, new Sprite2DSequencePlayer {
                    loop = LoopMode.Loop, sequence = entity, paused = false, speed = 1, time = 0
                });
                entityManager.SetComponentData(entity, new Sprite2DSequenceOptions {
                    frameRate = 20
                });

                // [HACK] We need to prime the asset entities, because if they don't already exist, generating them will
                // cause the buffer to be invalid.
                foreach (var sprite in sprites)
                {
                    assetManager.GetEntity(sprite);
                }

                var sequence = entityManager.GetBuffer <Sprite2DSequence>(entity);
                foreach (var sprite in sprites)
                {
                    sequence.Add(new Sprite2DSequence {
                        e = assetManager.GetEntity(sprite)
                    });
                }

                var node = new EntityNode(graph, session, entity);
                if (null != parent)
                {
                    parent.Insert(index, node);
                }
                else
                {
                    graph.Insert(index, node);
                }

                return(node);
            };

            //Create audio source
            HierarchyDragAndDrop <UnityEngine.AudioClip, EntityHierarchyTree.Key> .SingleObjectDrop = (session, audioClip, graph, parent, index) =>
            {
                var worldManager     = session.GetManager <IWorldManager>();
                var entityManager    = worldManager.EntityManager;
                var archetypeManager = session.GetManager <IArchetypeManager>();
                var assetManager     = session.GetManager <IAssetManager>();

                var entitySource = entityManager.CreateEntity(archetypeManager.AudioSource);
                worldManager.SetEntityName(entitySource, audioClip.name);
                entityManager.SetComponentData(entitySource, new AudioSource()
                {
                    clip   = assetManager.GetEntity(audioClip),
                    volume = 1.0f
                });
                entityManager.SetComponentData(entitySource, Guid.NewGuid().ToEntityGuid());

                var node = new EntityNode(graph, session, entitySource);
                if (null != parent)
                {
                    parent.Insert(index, node);
                }
                else
                {
                    graph.Insert(index, node);
                }

                return(node);
            };

            HierarchyDragAndDrop <UnityEngine.Video.VideoClip, EntityHierarchyTree.Key> .SingleObjectDrop = (session, videoClip, graph, parent, index) =>
            {
                var worldManager     = session.GetManager <IWorldManager>();
                var entityManager    = worldManager.EntityManager;
                var archetypeManager = session.GetManager <IArchetypeManager>();
                var assetManager     = session.GetManager <IAssetManager>();

                var entitySource = entityManager.CreateEntity(archetypeManager.VideoSource);
                worldManager.SetEntityName(entitySource, videoClip.name);
                entityManager.SetComponentData(entitySource, new VideoPlayer()
                {
                    clip     = assetManager.GetEntity(videoClip),
                    controls = true
                });
                entityManager.SetComponentData(entitySource, Guid.NewGuid().ToEntityGuid());

                var node = new EntityNode(graph, session, entitySource);
                if (null != parent)
                {
                    parent.Insert(index, node);
                }
                else
                {
                    graph.Insert(index, node);
                }

                return(node);
            };
        }