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

Resolved How to convert resources.load prefab to entity

Discussion in 'Entity Component System' started by James-YangDan, Jul 10, 2023.

  1. James-YangDan

    James-YangDan

    Joined:
    Feb 19, 2014
    Posts:
    7
    I use Resources to load the resource, and then I want to convert it to an entity. Is there any way?
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    274
    Not really. You seem to be leaning towards runtime conversion. That isn't a pattern that meshes well with the current paradigm, it's all about baking ahead of time in the editor.

    If you just have something premade in Resources (i.e. something that is static for a player build and not anything like Addressables or UGC), you can just bake that asset into an entity in a subscene of your choosing, assuming that it's a GameObject prefab and you just want to produce a prefab instance or an entity Prefab. If you want global access to those entities, you could do something like bake sets of such entities into specific utility subscenes that purely contain those converted objects, and load the subscene(s) whenever appropriate. You can use the asset's GUID (+ maybe the local file ID) in the data model somewhere so you can identify which entity corresponds to the original asset if that's necessary for you to hook things up.
     
  3. James-YangDan

    James-YangDan

    Joined:
    Feb 19, 2014
    Posts:
    7

    Thank you for your reply, I think your idea is great. I'll give it a try. The removal of the GameObjectConversionUtility.ConvertGameObjectHierarchy caused me a lot of trouble
     
  4. zonunity

    zonunity

    Joined:
    Oct 19, 2022
    Posts:
    1
    This really caused me trouble. My current solution is to set a unique ID for the prefab, and then add Entity based on this ID
     
  5. James-YangDan

    James-YangDan

    Joined:
    Feb 19, 2014
    Posts:
    7
    I'm currently using scenarios to manage prefab's bake.Because I don't have a lot of prefab. If there are many, my way may bring some other problems. Such as performance degradation.

    I don't think it's the best way to bake in scene init . Hopefully, there will be corresponding API later
     
  6. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    288
    AFAIK, if they want to support runtime baking, they have to make baking Burstable first.
     
  7. James-YangDan

    James-YangDan

    Joined:
    Feb 19, 2014
    Posts:
    7
    I kind of regret upgrading from the 0.5 to 1.0.
    The old version worked fine, but now the new version crashes and the physical performance is poor.
    I had to wait for the update and check the changelog
     
  8. poon2

    poon2

    Joined:
    Oct 30, 2022
    Posts:
    10
    Put each prefab from the resources folder in a separate subscene and let it bake. Then during runtime where you used to load the prefabs from resources, stream in the subscene for that prefab instead.
    (I think it's possible to automate the subscene creation step with editor scripting).