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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Initialize Sprite renderer from ScriptableObject IConvertGameObjectToEntity

Discussion in 'Entity Component System' started by OMGeeky, Jan 4, 2022.

  1. OMGeeky

    OMGeeky

    Joined:
    Sep 5, 2018
    Posts:
    17
    I have a lot of NPCs that have their data stored in scriptable objects, including the sprite they are using. So I need to add a sprite renderer to the entity (and some other stuff but im mainly struggeling with the sprite).
    In some forum posts and videos I saw people using a RenderMesh and making the sprite onto a material and then adding that, but they were all relatively old and said that there wasnt a better way to make sprites visible. But there is now (kind of?) with the hybrid renderer.

    When I convert a GameObject with a SpriteRenderer attached to an Entity it just works, and in the inspector of the entity there is a SpriteRenderer Component. my question is, how could I add that same functionality if my GameObject/Prefab doesnt have the right sprite attached yet but has it inside a ScriptableObject and needs to set it on initialization.
    Is this even possible with normal ecs do I need to rework all of my ScriptableObjects to be prefabs to make it work?

    For a bit of context, I have a Spawner System that spawns an NPC entity every now and then. This NPC should basically be (for now) an Entity with a sprite that can move around (got the move part).
    The spawner is declared with an authoring component with an amount and Entity field for the NPC inside.
     
  2. OMGeeky

    OMGeeky

    Joined:
    Sep 5, 2018
    Posts:
    17
    Figured it out. on the GameObjectConversionSystem is a method 'AddHybridComponent' I just had to set the sprite before that and put the sprite renderer in that method and it works
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,647
    Just a heads up, Hybrid Components for public use is deprecated in 0.50
    https://forum.unity.com/threads/dot...nes-december-2021.1209727/page-2#post-7727349
     
  4. OMGeeky

    OMGeeky

    Joined:
    Sep 5, 2018
    Posts:
    17