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

How can I create sprite entity?

Discussion in 'Project Tiny' started by techielabmy, Jun 28, 2019.

  1. techielabmy

    techielabmy

    Joined:
    Jun 14, 2019
    Posts:
    22
    Below is my code but it's not working. No sprite been created.

    Code (CSharp):
    1.  
    2. EntityArchetype entityArchetype = EntityManager.CreateArchetype(
    3.     typeof(Translation),
    4.     typeof(Sprite2DRenderer),
    5.     typeof(Sprite2DRendererOptions),
    6.     typeof(LayerSorting)
    7. );
    8.  
    9. Entity entity = PostUpdateCommands.CreateEntity(entityArchetype);
    10.  
    11. EntityManager.SetComponentData(entity, new Translation { Value = new float3() });
    12. EntityManager.SetComponentData(entity, new Sprite2DRenderer { color = new Color(1, 1, 0) });
    13. EntityManager.SetComponentData(entity, new Sprite2DRendererOptions { size = new float2(25, 10) });
    14.  
     
  2. badiba

    badiba

    Joined:
    Apr 9, 2018
    Posts:
    9
    Do you want to change sprite of an entity by creating a new one? If that is the case you should first get the sprite on a component from the editor as an entity.
     
  3. techielabmy

    techielabmy

    Joined:
    Jun 14, 2019
    Posts:
    22
    Nope. I want to create like if you select / clicked the unit / player it will create a highlighted.
     
  4. badiba

    badiba

    Joined:
    Apr 9, 2018
    Posts:
    9
    1 - If you put a child entity to your unit entities and that child entity has a Sprite2DRenderer component with the "highlight" sprite; then you can Enable/Disable that entity by adding or removing Disabled() component to/from it.

    OR

    2 - Get a reference to "highlight" sprite, whenever you need it you can assign it to an entity with a Sprite2DRenderer component.

    In both cases you don't need to create a new sprite in runtime. But after all if that's what you need to do, I haven't figured out how to do it yet.

    Good luck!
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    Are you sure that not created? May be you mean not rendered? Try add LocalToWorld type.
     
  6. techielabmy

    techielabmy

    Joined:
    Jun 14, 2019
    Posts:
    22
    How can I read the child component? do you have any sample code or reference for me sir?
     
  7. techielabmy

    techielabmy

    Joined:
    Jun 14, 2019
    Posts:
    22
    Already did that also. the sprite still not appear. thanks
     
  8. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    34
    Hi techielabmy,

    Have you tried:

    1- adding a LayerSorting order value in case it is under something?

    Code (CSharp):
    1. LayerSorting lay = new LayerSorting { order = 1 };
    2. EntityManager.SetComponentData(entity, lay);
    3.  
    2- And you may need also to add a sprite when you create your Sprite2DRenderer, not only set the color
     
    Last edited: Jul 2, 2019
  9. badiba

    badiba

    Joined:
    Apr 9, 2018
    Posts:
    9
    Just add a component to the child entities. Then use Entities.WithAll<ChildEntityComponent>() to get your child entities.