Search Unity

Make my sprite renderer tile and change color?

Discussion in '2D' started by alice_balls, May 17, 2022.

  1. alice_balls

    alice_balls

    Joined:
    Apr 12, 2020
    Posts:
    4
    I wanted to optimize my code so I replaced blocks of generated tiles with a stretched tile that just has the texture scaled.
    Issue is, my tiles are monochrome, and I use the color property to change their hue when they are instantiated.
    Issue is, with sprite renderer on, I can't change the color and scale it at the same time.
    It gives me a
    Material texture property _MainTex has offset/scale set. It is incompatible with SpriteRenderer.
    Is there a way to fix this? This really sucks because it makes an otherwise efficient process a pain in the ass to program.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The sprite rendering system relies on producing UV coordinates to render the Sprite you want.

    This is out of your sight because it must also work even when Unity atlases all the sprites onto some other interim sheet that you never see.

    One of the assumptions they have is that the mainTexture isn't scaled / offset.

    Are you sure this is necessary or that it will give you a meaningful improvement??

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
    alice_balls likes this.