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

Feature Request True Entity Prefab needed.

Discussion in 'Entity Component System' started by Dog_Like, Jun 10, 2023.

  1. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    I want a true entity prefab, similar to Unity prefabs, that can be saved in the project, packaged as an asset bundle, and loaded when needed and released when not needed. Why isn't there an editor tool that allows us to easily convert Unity prefabs into entity prefabs and save them in the project?
     
    threeplus likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    SubScenes (kind of an asset bundle) and content management allow you to do pretty much exactly what you ask.

    Look at SceneSystem.LoadPrefabAsync and EntityPrefabReference
     
    Last edited: Jun 10, 2023
  3. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    Thanks, I found PrefabComponent, I will attempt to save data in a ScriptableObject, then dynamically assemble the entity at runtime, and finally add the PrefabComponent to generate an entity prefab.
    Afterward, I will take a look at the EntityPrefabReference.
     
  4. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    941
    I never got SceneSystem.LoadPrefabAsync() to work. Can you show code how it is done?
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    There's samples for streaming now, PrefabAndSceneReferences found here (but there's 4 other samples as well a folder back)

    https://github.com/Unity-Technologi...les/Assets/Streaming/PrefabAndSceneReferences

    Code (CSharp):
    1. // request load weak prefab references
    2. foreach (var (weakPrefabReference, entity) in
    3.          SystemAPI.Query<RefRO<PrefabReference>>()
    4.              .WithNone<RequestEntityPrefabLoaded>()
    5.              .WithEntityAccess())
    6. {
    7.     ecb.AddComponent(entity, new RequestEntityPrefabLoaded
    8.     {
    9.         Prefab = weakPrefabReference.ValueRO.Value
    10.     });
    11. }
    12.  
    13. // instantiated the weak prefab references once they're loaded
    14. foreach (var (prefabLoadResult, entity) in
    15.          SystemAPI.Query<RefRO<PrefabLoadResult>>()
    16.              .WithAll<PrefabReference>()
    17.              .WithEntityAccess())
    18. {
    19.     ecb.Instantiate(prefabLoadResult.ValueRO.PrefabRoot);
    20.     ecb.RemoveComponent<PrefabReference>(entity);
    21. }
    the core sample code for loading

    -edit-
    oh i guess this specific sample didn't answer your question as you asked about LoadPrefabAsync not EntityPrefabReference

    AssetLoadingSystem has an example of this api

    https://github.com/Unity-Technologi...reaming/AssetManagement/AssetLoadingSystem.cs
     
    Last edited: Jun 10, 2023
    davenirline likes this.
  6. koonm

    koonm

    Joined:
    Jan 5, 2016
    Posts:
    12
    it only work in editor!
    when can we get examples of mobile?
     
  7. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    941
    I got it working but only in Editor because the constructor EntityPrefabReference(GameObject) is editor only. Why is this?
     
  8. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    Are you using Baker? The baking process happens during the build process, and it won't be called again after packaging it into an APK (baking has already been completed).
     
  9. threeplus

    threeplus

    Joined:
    May 23, 2013
    Posts:
    2
    Baking is not triggered if you only reference it by EntityPrefabReference in the build process.
    You have to reference it by a Subscene.
    You can make a scene with a Subscene component assoicated with the scene you used with EntityPrefabReference.
    Add that scene with subscene in you build scene list, it will trigger baking when you build.

    A easy way to know if you entity is baked: Add logs in the baker in your authoring class.
     
  10. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    941
    No, not using Baker. I wanted to preload "entity prefabs" at runtime without using a Baker. Is this even possible?
     
  11. Dog_Like

    Dog_Like

    Joined:
    Nov 24, 2017
    Posts:
    25
    It is possible by assemble entities at runtime, add components, and set values. Then, add a prefab (component) to the entity. There may be other methods available in the future.