Search Unity

2D top-down sorting script that doesnt rely on camera?

Discussion in 'Scripting' started by mrCharli3, Nov 20, 2017.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    My current script for creating a 2.5D effect looks like this:

    Code (CSharp):
    1. public class Sorter : MonoBehaviour {
    2.  
    3.     private SpriteRenderer spriteRenderer;
    4.  
    5.     void Start()
    6.     {
    7.         spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         spriteRenderer.sortingOrder = (int)Camera.main.WorldToScreenPoint(spriteRenderer.bounds.min).y * -1;
    13.     }
    14. }
    This script uses the camera to set the sortingOrder. It works perfectly but with this method I need to set it in update, and with 1000-ish object with this script (of which 980 are static) this feels really wasteful (?). But not sure how I can create a Script that only sets the position on Start() if the object is static, something like:

    Code (CSharp):
    1.     private SpriteRenderer spriteRenderer;
    2.     public bool OnlySetOnce;
    3.  
    4.     void Start()
    5.     {
    6.         spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
    7.         spriteRenderer.sortingOrder = (int)Camera.main.WorldToScreenPoint(spriteRenderer.bounds.min).y * -1;
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         if (!OnlySetOnce)
    13.         {
    14.             spriteRenderer.sortingOrder = (int)Camera.main.WorldToScreenPoint(spriteRenderer.bounds.min).y * -1;
    15.         }
    16.     }
    17. }
    However this obviously wont work if I use the camera to control the sortingOrder, any ideas?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You can use the Transparency Sort Mode and Transparency Sort Axis found Edit > Project Settings > Graphics, or Camera.transparencySortMode, to set the automatic sorting method.

    It sounds like you'd probably want something like this:
    Code (CSharp):
    1. Camera cam = Camera.main;
    2. cam.transparencySortMode = TransparencySortMode.CustomAxis;
    3. cam.transparencySortAxis = cam.transform.up;
     
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Im not sure I understand.

    Does this not require sorting layers?
    Where would I add that code?
    Would I still use orthographic camera?
    Should I make any changes in "Edit > Project Settings > Graphics"?
    Should I keep my old Sorting Script or replace it, as mentioned I want the drawing order to only be set once on static objects.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    From the docs:
    This isn't a new addition to your system, it's changing what Unity is already doing. So that makes it clear that SortingOrder and associated properties will take priority over this setting.

    If two objects have the same sorting properties, they will be sorted using this sort mode, which is why they sort by depth by default. Now, instead, they will sort based on the camera's up vector.

    If your camera never changes orientation, you can set that once from any script in Awake. If it does rotate at all, you'll have to update that setting with the new up vector.
     
    Last edited: Nov 20, 2017
  5. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Hi thanks for helping. I tried adding your code to the Awake() on my Camera. That is the only change I made (maybe I'm still misunderstanding you), and I get the same effect as I did before (i.e it sets sorting layer on Start(), but since I move and Im still using:

    Code (CSharp):
    1. spriteRenderer.sortingOrder = (int)Camera.main.WorldToScreenPoint(spriteRenderer.bounds.min).y * -1;
    to do sorting, it does not work. I'm sure I must be missing your meaning somehow, I would think that the above line is the one that needs changing?
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If your camera never rotates, then you don't need that line anymore. Your sprites will automatically sort based on their position on the camera's local Y axis.
     
    mrCharli3 likes this.
  7. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    :O What kind of dark magic is that. Thank you Wizard. Keep using ur powers for good!

    The reason I had trouble understanding was cus I thot it could not be that easy, haha.

    For anyone following, here is a video doing the same thing:

    https://unity3d.com/learn/tutorials...ion/sorting-groups-and-transparency-sort-axis
     
    Last edited: Nov 20, 2017
    LiterallyJeff likes this.
  8. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Do you know why this stops working when I add another sorting layer? :S
    Not even doing anything w it, just added one and poof; stopped sorting. Works again when I remove the Sorting Layer I created