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

Feedback Light 2D set sorting layers from script

Discussion in '2D' started by Baste, Mar 4, 2021.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    It seems like I can't set the "target sorting layers" on a Light2D from script?

    We use a multi-scene setup, so we need to have some editor and runtime scripts that ensures that there's only a single global 2D light. The solution we've got is to spawn/destroy those lights from scripts when the active scene changes.

    The problem is, if we just AddComponent<Light2D>, the light only targets the default sorting layer, since the code does this:

    Code (csharp):
    1.  
    2. [SerializeField] int[] m_ApplyToSortingLayers = new int[1];     // These are sorting layer IDs. If we need to update this at runtime make sure we add code to update global lights
    3.  
    As the comment there states, we can't update it at runtime. But we really need to!

    Right now our current solution is updating m_ApplyToSortingLayers through reflection, but would we please expose a method to add/remove sorting layers? It seems pretty essential.

    I'm a bit worried about the comment there, since these are global lights, but it seems like they affect all the sprite renderers in the world even if it's set after startup, so not sure what that's about.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    We also can't set the falloff intensity from script!

    That's crazy, it's just used through a getter, so I have to idea why you don't have a setter there, like you have for the intensity and the color and whatnot. Please?
     
    RemDust likes this.
  3. Klijarosto

    Klijarosto

    Joined:
    Jul 24, 2020
    Posts:
    1
    For those who are still looking for a solution. Here is a small hack as a temporary solution to the problem. I have not tested it on different platforms, but in the editor and Unity Remote on android, it works. For further exploration of the topic, see this thread: https://forum.unity.com/threads/target-sorting-layers-as-assets-or-editor-configurations.1095247/

    Code (CSharp):
    1. using System.Reflection;
    2.  
    3. namespace UnityEngine.Experimental.Rendering.Universal
    4. {
    5.     //Be careful! If during development the Unity team changes the name of the "m_ApplyToSortingLayers" variable, here it will also need to be changed.
    6.     public static class Light2DLayersMaskAccessExtension
    7.     {
    8.         public static int[] GetLayers(this Light2D light)
    9.         {
    10.             FieldInfo targetSortingLayersField = typeof(Light2D).GetField("m_ApplyToSortingLayers",
    11.                                                                        BindingFlags.NonPublic |
    12.                                                                        BindingFlags.Instance);
    13.             int[] mask = targetSortingLayersField.GetValue(light) as int[];
    14.             return mask;
    15.         }
    16.         public static void SetLayers(this Light2D light, int[] mask)
    17.         {
    18.             FieldInfo targetSortingLayersField = typeof(Light2D).GetField("m_ApplyToSortingLayers",
    19.                                                                        BindingFlags.NonPublic |
    20.                                                                        BindingFlags.Instance);
    21.             targetSortingLayersField.SetValue(light, mask);
    22.         }
    23.     }
    24. }
     
    Ayrnas likes this.
  4. Lepisto3D

    Lepisto3D

    Joined:
    Oct 6, 2019
    Posts:
    27
    This is still a problem guys. I cant find anything in any changelog of any version.

    Unity, please, respond to this problem, it's been adressed over and over.
     
  5. Ayrnas

    Ayrnas

    Joined:
    Jul 6, 2018
    Posts:
    5
    As of version 2021.3.0f1, still have this problem.

    Klijarosto's posted solution works perfectly, though!
     
  6. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Still haven't found a solution for this guys ? :/
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    We just forked URP and started fixing all the little things that are silly like this.

    @rustum, it's really disappointing that so many of the 2D things are just hidden behind private modifiers for no reason. It makes the tools less flexible, and the cost of fixing it is near zero, and nothing happens for years.
     
    RemDust likes this.
  8. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    This is crazy man -_-
     
  9. kennyy_

    kennyy_

    Unity Technologies

    Joined:
    Apr 7, 2021
    Posts:
    96
    Sorting layers in Light2Ds were updated in 2022.1 to affect all sorting layers in the scene by default, no longer targeting only the default layer.

    Please share some use cases of needing to modify the sorting layers during runtime. I'd like to improve the extensibility of Light2Ds for everyone.
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Affecting all sorting layers by default is an improvement, but the obvious example is if we were spawning a global light at runtime had a layer that we didn't want to hit by default.

    As I said, we also need to set the falloff intensity, we use that as part of some effect animations.

    Thing is, we're already setting these, and there doesn't seem to be any problems, so to us it seems like there's no reason to have them be private other than, idk, you not wanting to test it?
     
    Avalin, kayroice and hew_77 like this.