Search Unity

Can Target Sorting Layer be set in code for Light2D?

Discussion in 'Scripting' started by RedVonix, Oct 17, 2019.

  1. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    I'm trying to find a way to set the Target Sorting Layer for the Light2D object as supplied with LWRP. I've read through documentation, looked at the source, and scoured Google - but the Target Sorting Layer doesn't even seem to exist in the code, so I'm mighty confused. Could anyone point me in the right direction to something I might have missed?

    Thank you!
     
    DoubleDgames likes this.
  2. DoubleDgames

    DoubleDgames

    Joined:
    Oct 11, 2018
    Posts:
    1
    I'm having the same problem. I'm making a 2d platformer and I want to create a global light for every layer in a parallax scroll. The method does not exist on the Light2D definition. Have you found any solution?
     
  3. soundscouts

    soundscouts

    Joined:
    Apr 6, 2016
    Posts:
    7
    Bumping this for visibility, having the same issue here.
     
  4. MaxLorenzi

    MaxLorenzi

    Joined:
    Oct 14, 2019
    Posts:
    1
    Same issue here
     
  5. UDN_546bd3d0-6ea3-497a-b19f-dff09508f82f

    UDN_546bd3d0-6ea3-497a-b19f-dff09508f82f

    Joined:
    Apr 12, 2017
    Posts:
    1
  6. organick

    organick

    Joined:
    Jul 17, 2012
    Posts:
    17
    Same issue here.

    Temporary workaround:
    Find Light2D script in packages and make "m_ApplyToSortingLayers" public:
    Code (CSharp):
    1. [SerializeField] public int[] m_ApplyToSortingLayers = new int[1];
    Then set it from your code:
    Code (CSharp):
    1. light.m_ApplyToSortingLayers = new int[] {
    2.   SortingLayer.NameToID("someLayer1"),
    3.   SortingLayer.NameToID("someLayer2"),
    4.   SortingLayer.NameToID("Default"),
    5. };
    Not sure if there are any side effects.

    Will there be a proper API for this sometime soon?
     
    Last edited: Apr 24, 2020
    nevilleKitala and Dev_Krugz like this.
  7. Dev_Krugz

    Dev_Krugz

    Joined:
    Jan 22, 2020
    Posts:
    3
    Thank you @organick. I borrowed your idea and instead of making the m_ApplyToSortingLayers variable as public, I made a new public function to handle assigning new values. When they don't give you option, just make it yourself!

    Edit: Apparently, the package resets itself once you close Unity making it impossible to add, remove or modify anything from Light2D. I've tried editing it with Unity closed but to no avail. Inheriting from the said class seems to be impossible as well.

    I will try to look for more possible ways to achieve flexibility.

    Edit (1): I basically did what CustomPhase answered in this question:
    https://www.reddit.com/r/Unity3D/comments/dq82jf/editing_urp_shaders_question/
    I moved both com.unity.render-pipelines.core@7.1.9 and com.unity.render-pipelines.universal@7.1.9 from the PackageCache folder to ProjectFolder/Packages. Then I renamed them without the @7.1.9 at the end.

    I've tried editing it once again and it seems like it doesn't reset anymore.

    Please note that you have to re-download the package again if you want to update it.
     
    Last edited: May 2, 2020
  8. CakeMuffinCo

    CakeMuffinCo

    Joined:
    Nov 29, 2019
    Posts:
    2
    I can't even find m_ApplyToSortingLayers any more. So there is no other way to change Light2D Layer? For now I have 10 prefabs with lights that's need to be on 5 different layers. It's 50 prefabs instead of 10, with only 1 parameter difference.
     
  9. jeraldtapz_unity

    jeraldtapz_unity

    Joined:
    Feb 3, 2020
    Posts:
    3
    I think the best way is to just use reflection.

    I've managed to change it using this:

    Code (CSharp):
    1. FieldInfo fieldInfo = light2D.GetType().GetField("m_ApplyToSortingLayers", BindingFlags.NonPublic | BindingFlags.Instance);
    2.  
    3. fieldInfo.SetValue(light2D, layers);
     
    qtran122, nevilleKitala and SimDevs like this.
  10. Zander3535

    Zander3535

    Joined:
    Apr 7, 2020
    Posts:
    1
    This really does need to be fixed. I ended up making a duplicate of my 2d light, and changing the layer to what I needed on the duplicate. Then just disabled/enabled the game objects as needed in the code.

    Not the greatest, but it does work.
     
  11. cgreczkowski

    cgreczkowski

    Joined:
    Sep 17, 2020
    Posts:
    1
    bumping for visibility
     
    organick likes this.
  12. mimuraunity

    mimuraunity

    Joined:
    Oct 10, 2019
    Posts:
    4
    bumping for visibility
     
    organick likes this.
  13. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    422
    For those looking for answers, I swapped this out a while back with Smart Lighting 2D from the asset store as it has far better performance, supports shadows, code variations, and numerous layered and blended effects.
     
    SimDevs likes this.
  14. Lepisto3D

    Lepisto3D

    Joined:
    Oct 6, 2019
    Posts:
    27
    Bumping this for visibility. Still a problem
    Unity 2020.1.4f1
    URP 8.2.0
     
  15. Xiddo

    Xiddo

    Joined:
    Jan 2, 2021
    Posts:
    4
    Still is a problem... 2020.3...
     
  16. nevilleKitala

    nevilleKitala

    Joined:
    Jun 17, 2017
    Posts:
    2
    bumping for visibility
     
  17. juzimmer

    juzimmer

    Joined:
    Feb 14, 2018
    Posts:
    7
    Make a prefab of the light you need with the target sorting layer already set
     
  18. Valynna

    Valynna

    Joined:
    Apr 1, 2021
    Posts:
    39
    can confirm that this works, by the way. But it's weird we're 2 years on and no fix.
     
  19. bdeniz1997

    bdeniz1997

    Joined:
    Jan 26, 2021
    Posts:
    17
    back then it was experimental and you could change the source code, but now you can't. also back in the days i feel like there was a way to stop unity to replace the package over when you re-start it. but it was a long time ago. whatever.
    since source is hidden now you can only see some public variables in metadata file.

    + i tried System.Reflection thing above and it works.
     
  20. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    98
    I can't seem to make this field public :(
     
  21. bdeniz1997

    bdeniz1997

    Joined:
    Jan 26, 2021
    Posts:
    17
    jeraldtapz_unity this guy's answer works.
     
  22. --julian95--

    --julian95--

    Joined:
    Nov 2, 2015
    Posts:
    6
    Still not fixed in 2022.2.19f1. Is it fixed in the 2023 Versions?