1
\$\begingroup\$

I'm trying to get into Unity's Hybrid ECS feature. Creating and Manipulating Entities has worked fine so far for me, but somehow I can't find a viable option to delete Entities which works 100% of the time without errors.

I know about object pooling, however in this particular case there is no way around deleting Entities.

Thank you in advance!

\$\endgroup\$
1
\$\begingroup\$

Coming from an Entitas background here, i usually:

  1. Create a component called Destroy.
  2. at the end of the execution cycle i have a Cleanup System that reacts for entities that have the Destroy component and Destroy them, i also check if they have a View component so i can destroy the attached GameObject. usually Entitas does the magic with pooling.

    public class EntityDestroySystem: ICleanupSystem
    {
        private Contexts _contexts;
    
        private IGroup<RenderEntity> _renderGroup;
        private IGroup<GameEntity> _gameGroup;
    
        public EntityDisposeSystem(Contexts contexts)
        {
            _contexts = contexts;
    
            _renderGroup = _contexts.render.GetGroup(RenderMatcher.AllOf(RenderMatcher.Destroy));
            _gameGroup= _contexts.game.GetGroup(GameMatcher.AllOf(GameMatcher.Destroy));
        }
    
        public void Cleanup()
        {
            foreach (var e in _renderGroup.GetEntities())
            {
    
                e.Destroy();
            }
    
            foreach (var e in _gameGroup.GetEntities())
            {
                e.Destroy();
            }
        }
    }
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.