Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Correct way to convert unknown gameobject at runtime

Discussion in 'Entity Component System' started by HoHStudios, Jul 13, 2021.

  1. HoHStudios

    HoHStudios

    Joined:
    Nov 3, 2016
    Posts:
    20
    Hi guys,

    I apologize if this is a stupid question or has been asked before but I'm trying to create a spawning system that can take in any gameobject/prefab and convert it to an entity and spawn it. I understand how to spawn objects, but what I'm looking to know is the best or most correct way to convert ANY unknown game object to an entity prefab at runtime if possible. I was looking at the documentation found here and I know there is the "Convert to Entity" and "GameObjectConversionUtility" helpers but it says directly in that documentation that those are considered temporary and will be removed in the future. If those won't exist later, what other means of converting an unknown gameobject to an entity at runtime should I be using?

    Thank you in advance
     
    BeerCanAI likes this.
  2. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    988
    Hi, my understanding of Unity's philosophy on the subject is to aim for the conversion pipeline to only be used while making the game.
    There should be no conversion done at runtime.

    For now you can either use the conversion utility you mention or convert you prefab through the conversion system and use only the entity representation at runtime.

    A prefab is by definition known before runtime.
    You can look here to see what I suggested to build a prefab dictionary you can use in a spawner.

    The only situation where I would see un unknown prefab is in a scenario where you would deliver the prefab through the addressable pipeline. In that case you could use the conversion utility.
    In the future I expect Unity will provide some way to deliver entity directly (Live Link is already a good indication in that direction IMO).

    I also think something involving addressable and subscenes could work but I have not add time to work on that idea due to a incompatibility between subscenes and UITK.
     
  3. LethalInjection

    LethalInjection

    Joined:
    Jan 25, 2015
    Posts:
    37
    A lot of coders build a "factory" for each type of "object." This makes it easier to understand and maintain, especial for other developers, rather than trying to do everything in one huge long class. Polymorphism and inheritance can help here too.
     
  4. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    @HoHStudios I am still struggling to understand this too.
    i.e. "How do I instantiate GameObject 'Prefabs' in DOTS"
    What can I do? What can't I do? How do I load a bucketload of prefabs with predefined data e.g. a scene?

    There is lots of stuff about conversion workflows and the like but I've really struggled to find anything that really explains, in simple terms, the correct way to:
    1. Load an asset (e.g. scene, prefab or subscene prefab)
    - Addressables does work for loading but not sure how it works for unloading as it's all GO based
    2. Instantiate via script
    - a subscene
    - a regular GO prefab (must use ConvertToEntity here?)
     
  5. HoHStudios

    HoHStudios

    Joined:
    Nov 3, 2016
    Posts:
    20
    Thanks for the reply. That is what I was afraid of because I'm trying to make an asset that others can use that will allow them to input their own gameobjects into the system and have the spawner convert them to entities before spawning. Thats why I personally won't know what the contents of the GO would be. But I don't want to use a system that is bound to be deprecated. Do you know any way to do this? I dont know about the addressable pipeline im going to look into it now

    This intrigues me do you have any more info on this?

    Right, it seems like every documentation I see is all for the "eventually deprecated" conversion utility classes. I was hoping to find a "universal" way to take any gameobject or prefab that a different user might give it and have it auto convert to entity before spawning.
     
  6. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    969
    We use something like this.
     
  7. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    988
    I still don't get why you need it to be at runtime.
    If you make an asset for the asset store then your user will import it in their project use the monobehaviour you provided for a spawner authoring component and assign their prefab to a list of things the prefab can instantiate. There is 0 runtime implication here. And addressable has nothing to do with anything here either.

    The link @davenirline provided should do what you want using a scriptable object as a database for prefab.
    In addition the link I provided in my previous post also should do what you want (just need to extend it to accept a list of prefab instead of a single one).

    @davenirline , you mention at the end of your blog post that it should probably use blob asset. It's been a year sinc your post so maybe you figured it out. In any case if you want I made a BlobMap in a public package (but it needs some edit to the entities package in version 17., hopefully that will be fixed in 18.)
     
    andreiagmu and davenirline like this.
  8. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    969
    I'll check your BlobMap. We haven't updated it really. Too lazy because it's already working and there are too many new tasks with higher priority.
     
    WAYNGames likes this.
  9. HoHStudios

    HoHStudios

    Joined:
    Nov 3, 2016
    Posts:
    20
    Ahh, I see. Thank you for walking me through this. There seems to have been a disconnect between my thinking and how entities worked. Thank you! I think I have enough of a gameplan to see it through.

    Thank you too! Going to use a mix between both of your posts to come up with a solution. Cheers all!