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

How do I create the Sprite Renderer?

Discussion in 'Graphics for ECS' started by nixarn_, Oct 15, 2020.

  1. nixarn_

    nixarn_

    Joined:
    Jul 25, 2015
    Posts:
    42
    Hey,

    I want to create entities with Sprite Renderers but I just can't seem to figure out how. Can someone help me out? If I convert a GameObject with a sprite renderer it works fine, but I want to create these entities on the fly.

    Thanks!
     
    Last edited: Oct 15, 2020
  2. nixarn_

    nixarn_

    Joined:
    Jul 25, 2015
    Posts:
    42
    Bump! (Hope this is allowed?)

    Would be super helpful to know how to create a SpriteRenderer in code (As it's not an IComponentData). For now my process is such that I create a MonoBehaviour, then add the ConvertToEntity component, and manually run that system and fetch the newly created entity. But feels very cumbersome.
     
  3. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    see
     IConvertGameObjectToEntity, IDeclareReferencedPrefabs 

    1. make your Sprite an Entity prefab via IDeclareReferencedPrefabs
    2. in
    Convert
    function use
    conversionSystem.GetPrimaryEntity(your_prefab_gameobject)
    to get entity of the prefab
    3. use
    Instantiate
    function of EntityManager/EntityCommandBuffer to create sprite instance.

    You dont want to create sprite by adding components. SpriteRenderer is added to Entity as ComponentObject. It's just a GameObject with SpriteRender component, the GO is hidden in the hierarchy view. And it's position is synced with the Entity by CompanionLink component which is private in Unity.Entity. So you can not do it without a long walkaround.
     
    nixarn_ likes this.
  4. nixarn_

    nixarn_

    Joined:
    Jul 25, 2015
    Posts:
    42
    Oh right, that's how it works. Big thanks for the clarification & an answer!