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 [Solved] Can't change Sprite Color

Discussion in 'Entity Component System' started by Ismaeel370, Apr 16, 2021.

  1. Ismaeel370

    Ismaeel370

    Joined:
    Mar 16, 2015
    Posts:
    11
    Hi

    I am trying to change the color of SpriteRender(Unity.U2D.Entities.SpriteRenderer) as follow.

    Code (CSharp):
    1. Entity spawnedEntity = EntityManager.Instantiate(BrickEntityPrefabHolder.brickPrefabEntity);
    2. float4 brickColor = new float4(1.0f,0.0f,0.0f,1.0f);
    3. EntityManager.SetComponentData(spawnedEntity, new SpriteRenderer
    4.                 {
    5.                     Color = brickColor
    6.                 });

    brickPrefabEntity is public static field in the BrickEntityPrefabHolder class. Which is following Unity's ECS conversion style by implementing IConvertGameObjectToEntity, IDeclareReferencedPrefabs

    Can someone help. Attaching the inspector view of the project for the brick entity with SpriteRenderer component and it's value is update in the inspector but the view in game window is not.

    Let me know what am I missing here.
     

    Attached Files:

  2. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    349
    I think this code just creates new SpriteRenderer instance which is not on any gameobject, so nothing happens. Instead try
    EntityManager.GetComponentObject<SpriteRenderer>(spawnedEntity).color = brickColor;
     
  3. Ismaeel370

    Ismaeel370

    Joined:
    Mar 16, 2015
    Posts:
    11
    Thank you for the response this generates the following error.

    Code (CSharp):
    1. ArgumentException: GetComponentObject must be called with a managed component type.
    If we can change/update Translation of same entity like the following. It should work for SpriteRenderer in same way.

    Code (CSharp):
    1.                 EntityManager.SetComponentData(brickEntity, new Translation
    2.                 {
    3.                     Value = new float3(nextSpwanPosition.x, nextSpwanPosition.y, 0)
    4.                 });
    5.  
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I think OP is referring to the new entities based 2d sprite renderer, not gameobject based one.

    Unfortunately, I haven't used that package yet, so can't really help.
     
  5. Ismaeel370

    Ismaeel370

    Joined:
    Mar 16, 2015
    Posts:
    11
    Yes, I am using SpriteRenderer from U2D package. I guessed from the code shared by @Tony_Max was monobehaviour based but tested anyway and got the error what I was kind of expecting.

    Interestingly I am able to change the color of SpriteRenderer in another project. The template project from following thread.

    https://forum.unity.com/threads/2d-entities-0-29-is-now-available.957126/
     
  6. Ismaeel370

    Ismaeel370

    Joined:
    Mar 16, 2015
    Posts:
    11
    So, finally I was able to get it working. There is something really weird in the solution. In word: I had to set color in the Convert function of IConvertGameObjectToEntity interface to some default color

    Code (CSharp):
    1.  
    2. public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    3. {
    4.         float4 defaultColor = new float4(0.5f, 0.5f, 0.5f, 1);
    5.  
    6.         SpriteRenderer spriteRenderer = dstManager.GetComponentData<SpriteRenderer>(entity);
    7.         spriteRenderer.Color = defaultColor;
    8.         dstManager.SetComponentData(entity, spriteRenderer);
    9. }
    10.  
    After doing this I was able to set the color of Unity.U2D.Entities.SpriteRenderer Color field in my system as follow.

    Code (CSharp):
    1.                
    2. SpriteRenderer brickSpriteRenderer = EntityManager.GetComponentData<SpriteRenderer>(brickEntity);
    3.  
    4. brickSpriteRenderer.Color = brickColor;
    5.  
    6. EntityManager.SetComponentData(brickEntity, brickSpriteRenderer);
    7.  
    What's weird, is that if I don't set/change the Color field of the entity SpriteRenderer component in Convert function of IConvertGameObjectToEntity interface on my gameobject. I can't change the color field of same entity SpriteRenderer component in my system. It took me three days to get it working. May be it has something to do with Unity version(2020.3.0f1) and U2D package. As, same package works fine without this trick in slightly older Unity version(2020.1.17f1).