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

Question Examples of using DOTS ECS 1.0 with UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLD

Discussion in 'Entity Component System' started by sebas77, Jan 1, 2023.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    Hi,

    I find the new DOTS 1.0 ECS documentation somehow cleaner and simpler, but it lacks any indication about how to use it with UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLD (or even UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP if this doesn't cause troubles with the new DOTS)

    now that GameObjectConversionUtility.ConvertGameObjectHierarchy is gone I am not really sure about how to create entities from unity prefabs without needing a subscene.

    My only guess at the moment is that I have to add the baking systems in my custom world and use a subscene anyway? Examples of how to replace ConvertGameObjectHierarchy with UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP and custom worlds are appreciated.

    For my case, it's important that I can access the Entity value from outside a system, specifically in the composition root. I understand that it's quite a specific case, so I will not be surprised if there isn't a way to do this anymore.

    Edit: I think I would be able to solve my problem if I understand how to use the new Baking system with a custom world newed via code at runtime.
     
    Last edited: Jan 1, 2023
  2. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    791
    For baking it makes sense to always have a default world. Even if all it does is store prefabs. That is what I do. I still have custom worlds.
     
  3. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    106
    You can still disable the default system generation. That is what you are referring to if I'm not mistaken. Here are some links to those pages in the documents:

    World concepts - https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/concepts-worlds.html
    System concepts - https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/concepts-systems.html

    It sounds like the baking system is what you are going to have to use. They replaced the other conversion system (which was much more straight-forward) with the new "Baker" approach... Not my favorite change in ECS 1.0

    You cannot just slap the conversion component on a prefab anymore, and let it do the conversion. You will need to set up the "Baker" converter, and it will convert the prefab into an entity. Here is some more information on how Baking works - https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/baking.html

    Hope some of this helps... You may be on the right track with needing to use a special sub-scene, but you should be able to use a special world instead if necessary.
     
  4. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    Hey thanks! If I use the default system to bake, I think the entity will be found in the default world and won't be processed by the custom world. I may be able to copy entities from the default to the custom world perhaps. I would need more details @TheOtherMonarch .
     
  5. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    791
    Use some kind of query get a NativeArray<Entity> of your prefabs and use CopyEntitiesFrom.
     
    Last edited: Jan 2, 2023
  6. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    Thanks, I will defo try this, but I suspect there must be a way to load subscenes in custom worlds.
     
  7. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,594
    OK something like this without using a default world (thus with UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP ) is working somehow

    Code (CSharp):
    1. var sceneEntity = SceneSystem.LoadSceneAsync(
    2.                 customWorld.Unmanaged,
    3.                 new EntitySceneReference(
    4. SceneSystem.GetSceneGUID(ref customWorld.Unmanaged.GetExistingSystemState<SceneSystem>(), "Assets/Scene/CombiningBehaviours/Prefabs.unity"),
    5. 0));
    6.  
    7.             while (SceneSystem.IsSceneLoaded(customWorld.Unmanaged, sceneEntity)
    8.                 == false)
    9.             {
    10.                 customWorld.Update();
    11.                 await Task.Yield();
    12.             }
    13.  
    14. var worldEntityManager = customWorld.EntityManager
    15.  
    16. var query = worldEntityManager.CreateEntityQuery(typeof(PrefabsHolders.MyComponent));
    17.  
    18. var prefabHolder = query.ToComponentDataArray<PrefabsHolders.MyComponent>(Allocator.Temp);
    19.  
    20. var component = prefabHolder[0];
    21.  
    22. var redFoodPrefab = component.RedFood;
    23. var blueFoodPrefab = component.BlueFood;
    24. var redDoofusPrefab = component.RedDoofus;
    25. var blueDoofusPrefab = component.BlueDoofus;
    26. var specialBlueDoofusPrefab = component.SpecialDoofus;
    27.  
    with

    Code (csharp):
    1.  
    2. public class PrefabsHolder: MonoBehaviour
    3. {
    4.     public GameObject BlueDoofus;
    5.     public GameObject RedDoofus;
    6.     public GameObject SpecialDoofus;
    7.     public GameObject RedFood;
    8.     public GameObject BlueFood;
    9.  
    10.     public class MyBaker: Baker<PrefabsHolder>
    11.     {
    12.         public override void Bake(PrefabsHolder authoring)
    13.         {
    14.             AddComponent(new MyComponent
    15.             {
    16.                     BlueDoofus = GetEntity(authoring.BlueDoofus),
    17.                     RedDoofus = GetEntity(authoring.RedDoofus),
    18.                     SpecialDoofus = GetEntity(authoring.SpecialDoofus),
    19.                     RedFood = GetEntity(authoring.RedFood),
    20.                     BlueFood = GetEntity(authoring.BlueFood),
    21.             } );
    22.         }
    23.     }
    24.  
    25.     public struct MyComponent : IComponentData
    26.     {
    27.         public Entity BlueDoofus;
    28.         public Entity RedDoofus;
    29.         public Entity SpecialDoofus;
    30.         public Entity RedFood;
    31.         public Entity BlueFood;
    32.     }
    33. }
    34.  

    let me know if this can be improved or if there could be problems with it, please. The only problem I seem to have at the moment is exceptions during the dispose of the custom world.

    edit: it seems to not work in a built client

    edit2: it did work if I use the hash value directly, but the subscene must be in the main scene, otherwise I guess the scene won't be bundled with the client.

    edit3: it works only in a mono client, it fails in a IL2CPP client
     
    Last edited: Jan 3, 2023
  8. fas3r_

    fas3r_

    Joined:
    May 6, 2021
    Posts:
    11
    Hello @sebas77 , did you look at https://docs.unity3d.com/Packages/c...nt-management-get-a-weak-reference-scene.html ?

    It could be easier, but i'm not sure if it was already available in january. I'm thinking of using with Baker and Blob Asset https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/blob-assets-create.html ( bottom of the page )

    I'm trying to use `UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP ` but I did not got far yet, I'm not sure to understand if I basically have to redo everything that is done in the default BootstrapWorld script