Skip to content

Using Z dept in Unity 2D is a bad practice #19

Description

@Jiraiyah

You may not know it, but when you have two sprites in two different depth, in 2d mode, it would rise the draw call because when the game is being built, the built in atlas generator won't be able to figure it out and put those textures into one atlas.
these days, unity is giving an option to sort sprites inside a single layer by the sort order so.....

in your prefabBuilder.cs, line 140 :

child.localPosition = new Vector3 (spriteInfo.x, spriteInfo.y, oref.z_index); //Z-index helps determine draw order

change it to ==>

child.localPosition = new Vector3 (spriteInfo.x, spriteInfo.y, 0);

and right bellow it, add this line :

renderer.sortingOrder = Mathf.Abs ((int) (oref.z_index * 1000)); //Z-index helps determine draw order

this way, your z_index will be converted to integers and set into the sorting order on sprite renderer and every thing would show properly although the whole sprites are on z = 0 !!!!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions