Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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:
    365
    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:
    6
    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:
    4
    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
    Dev_Krugz likes this.
  7. Dev_Krugz

    Dev_Krugz

    Joined:
    Jan 22, 2020
    Posts:
    1
    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. CackeMuffin

    CackeMuffin

    Joined:
    Nov 29, 2019
    Posts:
    1
    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:
    1
    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);
     
unityunity