Can Target Sorting Layer be set in code for Light2D?

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!

1 Like

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?

Bumping this for visibility, having the same issue here.

Same issue here

나를 too.

Same issue here.

Temporary workaround:
Find Light2D script in packages and make “m_ApplyToSortingLayers” public:

[SerializeField] public int[] m_ApplyToSortingLayers = new int[1];

Then set it from your code:

light.m_ApplyToSortingLayers = new int[] {
  SortingLayer.NameToID("someLayer1"),
  SortingLayer.NameToID("someLayer2"),
  SortingLayer.NameToID("Default"),
};

Not sure if there are any side effects.

Will there be a proper API for this sometime soon?

2 Likes

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.

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.

I think the best way is to just use reflection.

I’ve managed to change it using this:

FieldInfo fieldInfo = light2D.GetType().GetField("m_ApplyToSortingLayers", BindingFlags.NonPublic | BindingFlags.Instance);

fieldInfo.SetValue(light2D, layers);
3 Likes

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.

bumping for visibility

1 Like

bumping for visibility

1 Like

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.

1 Like

Bumping this for visibility. Still a problem
Unity 2020.1.4f1
URP 8.2.0

Still is a problem… 2020.3…

bumping for visibility

Make a prefab of the light you need with the target sorting layer already set

can confirm that this works, by the way. But it’s weird we’re 2 years on and no fix.

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.

I can’t seem to make this field public :frowning: