Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Remove converted entitites that I don't need

Discussion in 'Entity Component System' started by sebas77, Feb 26, 2020.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    I have noticed that I have some prefabs that I need to feed to the conversion system that can generate entities that eventually I don't use at all, so I would like to delete them or avoid to be created. I can't modify the prefabs (so I cannot use Convert To Entity Stop). However I could add Convert To Entity Stop at run time if it's the best thing to do. Suggestions? I was thinking about the GameObjectConversion groups, but not sure if this can be done.
     
  2. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    StopConvertToEntity cannot be used for my case
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    As long as you have a way of knowing that an entity is one you don't need, then a GameObjectConversionSystem can definitely do this.
     
  4. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    how though? Still figuring this out
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    How to know if an entity is one you don't need or how to write a GameObjectConversionSystem? I can't help you with the former, and the latter likely depends on how you do the former.
     
  6. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    At this point in time I am trying to solving it querying all the entities that have ONLY these components:

    upload_2020-2-27_17-29-19.png

    and delete them. But I am not sure if it's possible to write a query that returns entities with only those components
     
  7. ecurtz

    ecurtz

    Joined:
    May 13, 2009
    Posts:
    640
    You can mark all the entities for GameObjects you don't want in the GameObjectConversionSystem and then destroy them in a system later, but there's no way to keep them from being created at this point, as far as I can tell. As you've found, StopConvertToEntity only works for stuff in the scene.
     
    sebas77 likes this.
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    You could try to write a conversion system in AfterConversionGroup (I forget names because I'm having tech issues on my workstation atm) which gets an EntityQuery from DstEntityManager querying for those components you specified, then doing chunk iteration checking the chunk's archetype. If the archetype is an exact match, you add it to a list to be destroyed. After iterating through the chunks, tell DstEntityManager to destroy your list of chunks.

    I don't know if deleting entities like that breaks the mapping system. If it does, you're best bet is to remove all the components and add a DeleteMe tag so that the Entity technically still exists for the mapping system but the memory usage is drastically reduced.
     
    sebas77 likes this.
  9. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    Eventually the simplest solution was actually to remove the unused gameobjects from the prefabs before to convert and reassemble them back right after. I thought it was dangerous, but it seems to work fine.
     
  10. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    I lately realised that prefab hierarchies cannot be changed, so the solution wasn't viable either. I had to use the ConvertScene to be able to use gameobjects as instance of prefab, so that I can change the hierarchy (purge invalid GOs)

    Code (CSharp):
    1.  var gos = new FasterList<GameObject>();
    2.                 var temporaryScene = SceneManager.CreateScene("temporary");
    3.                 SceneManager.SetActiveScene(temporaryScene);
    4.  
    5.                 for (var j = 0; j < prefabData.Count; ++j)
    6.                 {
    7.                     var transformParent = Object.Instantiate(loadTask.Result[j]);
    8.  
    9.                     gos.Add(transformParent);
    10.  
    11.                     Sanitize(transformParent);
    12.                 }
    13.  
    14.                 var gameObjectConversionSettings =
    15.                     GameObjectConversionSettings.FromWorld(physicsWorld, ECSPhysicResourceManager.Instance.blobStore);
    16.  
    17.                 gameObjectConversionSettings.ConversionWorldPreDispose += world =>
    18.                     RelinkEntities(physicsWorld, world.GetExistingSystem<GameObjectConversionSystem>(), gos, prefabData);
    19.  
    20.                 GameObjectConversionUtility.ConvertScene(temporaryScene, gameObjectConversionSettings);
    21.  
    22.                 SceneManager.UnloadScene(temporaryScene);
    With more insights I can say that there is no reason to not support the conversion of a provided array of GameObjects.

    public void CreateEntitiesForGameObjects(Scene scene)

    must have a version

    public void CreateEntitiesForGameObjects(List<GameObjects> scene)

    No reason to not have it really.