Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
3
Question by guavaman · Nov 27, 2013 at 08:56 PM · spritessorting layers

How do you get a list of Sorting Layers via scripting?

Unity just added sorting layers in 4.3, however I cannot find where to access these layers via scripting in the docs. I know how to get a list of regular layers:

 string[] options = new string[32];
 for(int i = 0; i < 32; i++) { // get layer names
     options[i] = i + " : " + LayerMask.LayerToName(i);
 }

But I can't find anything else in the docs called layer, sorting layer, or anything related.

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

2 Replies

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

Answer by guavaman · Nov 27, 2013 at 10:45 PM

Thanks to Ivan.Murashko for answering this question yesterday on the forum.

C# version:

 using UnityEngine;
 using UnityEditor;
 using UnityEditorInternal;
 using System.Reflection;

 public class SomeClass {

     // Get the sorting layer names
     public string[] GetSortingLayerNames() {
         Type internalEditorUtilityType = typeof(InternalEditorUtility);
         PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
         return (string[])sortingLayersProperty.GetValue(null, new object[0]);
     }

     // Get the unique sorting layer IDs -- tossed this in for good measure
     public int[] GetSortingLayerUniqueIDs() {
         Type internalEditorUtilityType = typeof(InternalEditorUtility);
         PropertyInfo sortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
         return (int[])sortingLayerUniqueIDsProperty.GetValue(null, new object[0]);
     }
 }

JS version:

 #pragma strict
 import UnityEditor;
 import UnityEditorInternal;
 import System.Reflection;
  
 public class SomeClass {
  
     // Get the sorting layer names
     public function GetSortingLayerNames() : String[] {
         var internalEditorUtilityType : System.Type = InternalEditorUtility;
         var sortingLayersProperty : PropertyInfo = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
         return sortingLayersProperty.GetValue(null, new System.Object[0]);
     }
  
     // Get the unique sorting layer IDs -- tossed this in for good measure
     public function GetSortingLayerUniqueIDs() : int[] {
         var internalEditorUtilityType : System.Type = InternalEditorUtility;
         var sortingLayerUniqueIDsProperty : PropertyInfo = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
         return sortingLayerUniqueIDsProperty.GetValue(null, new System.Object[0]);
     }
 }
 
Comment
Add comment · Show 9 · 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
avatar image Spidyy · Feb 13, 2014 at 06:26 AM 0
Share

I tried to use your script. The names are properly retrived, but the unique ids give me incoherent numbers. What should be, for example, an id of 10 on the layer editor give me back -11475652 or the like in my script with GetSortingLayerUniqueIDs(). Any idea?

avatar image guavaman · Feb 13, 2014 at 08:19 PM 0
Share

@Spidyy Those "incoherent" numbers are correct. These are unique ids internal to unity similar to a GUID so that no matter how you change the ordering of layers, an object that already has a layer assigned always stay assigned to the same layer. To set an object to this layer, assign the unique id to Renderer.m_SortingLayerID, which you will have to do via SerializedProperty.

If all you want to know is the current layer order, the name array is sorted by index as you see them in the inspector.

avatar image imurashka · Mar 18, 2014 at 02:31 PM 2
Share

Oh cool. It's my solution :) I hope this helped you.

avatar image guavaman · Mar 18, 2014 at 07:51 PM 0
Share

@Ivan.$$anonymous$$urashko Indeed it did! Thanks a lot!

avatar image fermmmm · May 24, 2014 at 10:31 PM 0
Share

@Spidyy: To set an object to this layer, assign the unique id to Renderer.m_SortingLayerID, which you will have to do via SerializedProperty.

Can you post the code to do this? I don't know how to do it.

Show more comments
avatar image
7

Answer by Sarkahn · Aug 22, 2016 at 06:37 PM

Not sure if this is new but as of 5.4 there is an easier way using the SortingLayer class which requires no reflection. Put this in an editor script and you can use it to draw a nice popup list similar to the one in SpriteRenderers.

     /// 
     /// Draws a popup of the project's existing sorting layers.
     /// </summary>
     /// <param name="layerID">The internal layer id, can be assigned to renderer.SortingLayerID to change sorting layers.</param>
     public static int DrawSortingLayersPopup( int layerID )
     {
         var layers = SortingLayer.layers;
         var names = layers.Select(l => l.name).ToArray();

         if( !SortingLayer.IsValid(layerID) )
         {
             layerID = layers[0].id;
         }

         var layerValue = SortingLayer.GetLayerValueFromID(layerID);

         var newLayerValue = EditorGUILayout.Popup(layerValue, names);

         return layers[newLayerValue].id;
     }

Just make sure you're aware of the values you're getting from SortingLayer. "ID" means the internal unity value, "Value" means the actual index in the list of layers.

Comment
Add comment · Show 3 · 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
avatar image TreyH · Mar 19, 2017 at 02:34 PM 0
Share

$$anonymous$$uch simpler answer for 5.4+ users.

avatar image Suzuka91 · Jun 02, 2017 at 11:12 AM 0
Share

Works perfect. Thank you :)

avatar image SimonST · Jun 12, 2018 at 10:29 AM 0
Share

This will run into problems if you have a SortingLayer with negative value. I suggest replacing var layerValue = SortingLayer.GetLayerValueFromID(layerID); var newLayerValue = EditorGUILayout.Popup(layerValue, names);

with something like var entryIndex = Array.FindIndex(layers, l => l.id == layerID); var newLayerValue = EditorGUILayout.Popup(entryIndex, names);

to find the correct index in the list of layer names. (assu$$anonymous$$g "using System", a simple for-loop to find the index works just fine, of course)

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

23 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

Related Questions

How to make a 2D sprite always render in front of the 3D gameobjects 1 Answer

is there any way to group sprites? - Unity2D, 1 Answer

Per-Sorting Layer Collision 1 Answer

Sorting Layers in a 3D Environment 1 Answer

Sorting Layers not working? 3 Answers


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