Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting Renderer.sortingLayerID serialized property

Discussion in 'Scripting' started by guavaman, Nov 27, 2013.

  1. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    EDIT: Solved. See this post.

    I'm trying to get Sorting Layers working with a MeshRenderer. I see from the docs all renderers have support for sorting layers now. I'm using a SeralizedObject to set the property in the Renderer. By checking the properties in the Renderer object, I see it has 3 fields:

    m_SortingLayer (unsigned int)
    m_SortingLayerID (sint16)
    m_SortingOrder (sint16)

    I can easily change m_SortingOrder.intValue to another value and it works. Objects are sorted within that layer properly. I can also change m_SortingLayerID.intValue, and the value change sticks, but it doesn't affect the rendering order. m_SortingLayer seems to be the key player here for setting the actual sorting layer of the renderer. The problem is, when I set m_SortingLayer.intValue to another value, it sticks around only until serializedObject.ApplyModifiedProperties(); is called, at which point it always reverts back to 0.

    I've confirmed by looking at SpriteRenderer that m_SortingLayer.intValue does indeed change to the new layer ID when you change the layer in the inspector, so I think this is the key. However, when I change this value on a Renderer with my own inspector, the value just will not stick. Like I said, the other two values stick just fine.

    I've tried setting m_SortingLayerID first, then m_SortingLayer, and the other way around. Neither works. I'm guessing there's something else involved in actually changing the sorting order that I'm not able to see from the serialized properties. Possibly another call to some other editor helper class? I don't know. Any help would be greatly appreciated.
     
    Last edited: Nov 28, 2013
  2. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Hmm... I just found out m_SortingLayerID is not the ID of the layer, at least not the simple id. I also found out that setting the sorting layer via Renderer.sortingLayerID works, but not with a SerializedProperty. So that must be a property that when you set it is setting both m_SortingLayer and getting some internal layer ID and setting that in m_SortingLayerID.

    When Renderer.sortingLayerID is set to 3:

    m_SortingLayer = 3
    m_SortingLayerID = 1973583891

    So m_SortingLayer is the key, but it just doesn't work to set it via SerializedProperty.
     
  3. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Ok I was wrong. m_SortingLayerID is the key. When you set that to a proper layer ID like 1973583891 everything works. But how do I determine these internal layer IDs...
     
  4. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Okay, here's the answer:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditorInternal;
    4. using System.Reflection;
    5.  
    6. public class SomeClass {
    7.  
    8.     // Get the sorting layer names
    9.     public string[] GetSortingLayerNames() {
    10.         Type internalEditorUtilityType = typeof(InternalEditorUtility);
    11.         PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
    12.         return (string[])sortingLayersProperty.GetValue(null, new object[0]);
    13.     }
    14.  
    15.     // Get the unique sorting layer IDs -- tossed this in for good measure
    16.     public int[] GetSortingLayerUniqueIDs() {
    17.         Type internalEditorUtilityType = typeof(InternalEditorUtility);
    18.         PropertyInfo sortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
    19.         return (int[])sortingLayerUniqueIDsProperty.GetValue(null, new object[0]);
    20.     }
    21. }
    22.  
    This will get you a list of the sorting layers IDs. You set m_SortingLayerID to the proper unique ID and everything else is taken care of.
     
  5. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Hey guavaman,

    We seem to be having the same issue as you were. My question is how do we know what SortingLayerUniqueIDs match up with SortingLayerNames? Are the property arrays both sorted, so they both match up? Thanks.
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    Yes, they do as far as I know. The order of the unique ids and names match, and when you shift the layer order around in layer editor, the order of the names and IDs change in these two arrays.