Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AHAKuo · Apr 21, 2021 at 07:36 AM · renderersorting layerssorting order

Unity 2D Sorting Group breaking after 31-32 sprites. How do I manage this?

I'm facing an issue that is limiting my work on my game.


I have a game object that contains a sorting group component that is set to "Background Layer".

alt text

And it's working fine with my gameobjects. They all are on the sorting group (Default). I even tried to set them to Background but that didn't help.


The moment I pass 31 gameobjects (i.e. renderers), it all starts to break. As in, the very first sprite in the hierarchy will jump to the front, and if I keep adding more sprites, it brings other sprites from the top of the list to the front.


I have no idea why it keeps breaking like this.

Any help would be appreciated!


Ps. I have mesh renderers (spine2d) as well as sprite renderers under that BG gameobject. And I tried to see if the problem was with the mesh renderer, but it wasn't. It still broke even with Sprite renderers alone.


screenshot-2021-04-21-092357.png (85.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AHAKuo · Apr 21, 2021 at 07:40 AM

Okay! So, guys, I tried with the sorting group left and right and it still didn't work with me whatsoever. But I found a workaround :D


I tried even making a new scene, adding a game object with just a sorting group component, and then tried to fill it with more than 31 sprites (give or take), and it still broke (bringing images high in the hierarchy to the front and even bringing random images.


And so I just decided to make my own sorting group script. It works just like it, and even better.

It allows the usual number of renderers which is more than 1000.


So here is how I did it:


1 - First, we make a script that will be placed on the parent game object that would usually have the sorting group component. Delete the sorting group component, and add a new script. Let's call it: RendererSorter. Just as an example.


Fill this into it:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class RendererSorter: MonoBehaviour
 {   
     //This script is an alternative to the shabby unity sorting group system. It automatically orders sprites without any hassle. Allowing more than 1000 renderers.
 
     public RendererMarker[] RendererMarkers; //Used for the sorting of groups.
     public string sortingLayerName; //For the sorting of groups.
 
     private void Update() //This will be called each time we change something in the scene.
     {
         if (Application.isPlaying)
         {
             return;
         }
         RendererMarkers = GetComponentsInChildren<RendererMarker>();
         for (int i = 0; i < RendererMarkers.Length; i++)
         {
             RendererMarkers[i].SetSortingLayer(i, sortingLayerName);
         }
     }
 }



2 - Then, we make another script that will be attached to the game objects that contain a renderer (Mesh or sprite). Let's call this script, RendererMarker (just as it is referenced in the first script. And add this into it:


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RendererMarker : MonoBehaviour
 {
     // Marker used to give this gameobject's renderer the appropriate sorting group layer.
 
     MeshRenderer meshRenderer;  //It could be a mesh renderer.
     SpriteRenderer spriteRenderer; //Or it could be a sprite renderer.
 
     public void SetSortingLayer(int order, string sortingLayerName)
     {
         meshRenderer = GetComponent<MeshRenderer>();
         spriteRenderer = GetComponent<SpriteRenderer>();
 
         //Then we sort based on the type of renderer from the void paramater.
 
         if (meshRenderer != null)
         {
             meshRenderer.sortingLayerName = sortingLayerName;
             meshRenderer.sortingOrder = order;
         }
         else if(spriteRenderer != null)
         {
             spriteRenderer.sortingLayerName = sortingLayerName;
             spriteRenderer.sortingOrder = order;
         }
     }
 }



3 - And viola! Just like that. The moment the scripts are attached to the scene, everything will update automatically according to the hierarchy in your game. You can see how everything is order in the public RendererMarker array from the main script.


I hope this helps anyone!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

158 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

dynamically sorting sprites in a 2d oblique perpective 0 Answers

I want to 2d sprite sort layers but force some of them to render in front of another layer? 0 Answers

I'm a beginner , currently i have a problem with sorting order pragramatically . 0 Answers

Programmatically Ordering Game Objects in 2d / Hierarchy 0 Answers

How Can I Make Sprite Sorting Layers 'Relative'? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges