Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Issue setting Sprite alphas

Discussion in 'Project Tiny' started by furroy, Jun 11, 2019.

  1. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    I'm assuming that I'm not accessing the sprite renderer properly or setting the alpha wrong, but I keep getting weird results with this where sprites become near black. Sometimes it works right, sometimes not. I stripped everything down and simplified this loop so the sprites should either be full alpha or 75% transparent, which most are, but some will just randomly show up super dark..

    Code (CSharp):
    1.  
    2.         protected override void OnUpdate()
    3.         {
    4.             if (RecalcAlpha)
    5.             {
    6.                 RecalcAlpha = false;
    7.                 Entities
    8.                     .WithAll<CellComponent>()
    9.                     .WithAll<Sprite2DRenderer>()
    10.                     .ForEach((ref Sprite2DRenderer sr, ref CellComponent cellData) =>
    11.                     {
    12.                         var alpha = RandomExtra.OneIn(ref rand, 2) ? 0.25f : 1f;
    13.                         sr.color.a = alpha;
    14.                     });
    15.             }
    16.         }
    17.  
    Tons of stuff removed to make sure it's not some interaction with other parts of the map:

     
  2. Lucas-Meijer

    Lucas-Meijer

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    175
    We've had issues before where if you specify a component requirement both as an argument to your lambda, as well as in the WithAll<>, you'd get an exception. Maybe that's happening for you (we're fixing this). try removing those two .WithAll<> calls, as those will already be implicitely detected from the lambda arguments.
     
  3. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    took those out and no effect:

    Code (CSharp):
    1.         protected override void OnUpdate()
    2.         {
    3.             if (RecalcAlpha)
    4.             {
    5.                 RecalcAlpha = false;
    6.                 Entities.ForEach((ref Sprite2DRenderer sr, ref CellComponent cellData) =>
    7.                     {
    8.                         var alpha = RandomExtra.OneIn(ref rand, 2) ? 0.20f : 1f;
    9.                         sr.color.a = alpha;
    10.                     });
    11.             }
    12.         }
    13.  
    weirdly when i move the var alpha outside the loop, the effect never reproduces. which smells of some strange code gen issue?

    Code (CSharp):
    1.        protected override void OnUpdate()
    2.         {
    3.             if (RecalcAlpha)
    4.             {
    5.                 var alpha = RandomExtra.OneIn(ref rand, 2) ? 0.20f : 1f;
    6.                 RecalcAlpha = false;
    7.                 Entities.ForEach((ref Sprite2DRenderer sr, ref CellComponent cellData) =>
    8.                     {
    9.                         sr.color.a = alpha;
    10.                     });
    11.             }
    12.         }
    13.  
     
  4. Dunk86

    Dunk86

    Joined:
    May 10, 2015
    Posts:
    53
    I find that lambda's don't allow me to use reference versions of exterior values. I have to do precalculation to a value type before feeding it into such lambdas. I'm surprised you're not getting a compilation error though...
     
  5. Lucas-Meijer

    Lucas-Meijer

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    175
    This is very puzzling. If you are able to reproduce it in a small environment, we'd love to take a look. The "calculate some thigns in a lambda, even when capturing things from the outside" should totally work, and from a tech point of view, it's very hard to imagine that that could somehow be the actual problem.