using System; using System.Collections; using System.Collections.Generic; using Unity.Entities; using Unity.Scenes; using UnityEditor; using UnityEngine; using UnityEngine.PlayerLoop; namespace GarmentButton.EntityEditor { public class EntityEditorWorld { public EntityManager EntityManager; public World World; public EntityEditorWorld() { SetUpWorld(); EditorApplication.update += UpdateSystems; } public void UpdateSystems() => World.Update(); private void SetUpWorld() { ScriptBehaviourUpdateOrder.RemoveWorldFromCurrentPlayerLoop(World.DefaultGameObjectInjectionWorld); World = CreateSimulationWorld("GarmentButton"); EntityManager = World.EntityManager; CopySubSceneEntities(); } private void CopySubSceneEntities() { var loadParams = new SceneSystem.LoadParameters { Flags = SceneLoadFlags.NewInstance | SceneLoadFlags.DisableAutoLoad, }; foreach (var scene in SubScene.AllSubScenes) { SceneSystem.LoadSceneAsync(World.Unmanaged, scene.SceneGUID, loadParams); } } private World CreateSimulationWorld(string name) { IReadOnlyList systems = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default); string[] nameSpacesOfUnity = new string[] { "Unity.Rendering", "Unity.Scenes", "Unity.Physics.GraphicsIntegration", "Unity.Physics.Authoring", "Unity.Physics.Systems", "Unity.Entities", "GarmentButton.EntitiesSpawnTool", "Unity.Scenes.Editor", "Unity.Transforms", }; List simulationSystems = new List(); for (int i = 0; i < systems.Count; i++) { if (systems[i].Namespace == null) continue; foreach (var nameSpace in nameSpacesOfUnity) { if (systems[i].Namespace.Equals(nameSpace)) { simulationSystems.Add(systems[i]); break; } } } var world = new World(name, WorldFlags.Live & WorldFlags.Simulation); DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, simulationSystems); ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(world); return world; } public void CreateSystems(SystemHandle[] systems) { var mainGroup = World.GetOrCreateSystemManaged(); var totalGroup = World.CreateSystemManaged(); foreach (var system in systems) totalGroup.AddSystemToUpdateList(system); mainGroup.AddSystemToUpdateList(totalGroup); mainGroup.SortSystems(); } public void DestroyWorld() { EditorApplication.update -= UpdateSystems; ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(World.DefaultGameObjectInjectionWorld); if (World.IsCreated == false) return; World.Dispose(); } } public partial class BackgroundSystemsGroup : ComponentSystemGroup { } }