Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scripted Light2D Component - Sorting Layers

Discussion in '2D' started by owlgrey, Oct 7, 2020.

  1. owlgrey

    owlgrey

    Joined:
    Jul 30, 2020
    Posts:
    2
    Just got 2D lighting and the URP up and running, but I'm rather new to all this.

    Is there a way to set the sorting layers for a script added Light2D component? It defaults to the Default layer, and there doesn't seem to be a way to change it.
    Am I just not using the Light2D component as expected?

    I'm hoping to control global lighting during the most of the day cycle and then switch it to a directional style light during dawn and dusk, which is why I'd like to be able to play with the sorting layers at runtime.

    Thanks in advance!
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    ss - Copy.png
    Opening the script, I'm not seeing any public field, property, or setter. That means that your best bet might be reflection: int[] m_ApplyToSortingLayers

    Could blend styles work for what you want to accomplish? Without reflection, you can set the blendStyleIndex. You could have dawn and dusk blend styles each claiming a different mask channel.

     
  3. owlgrey

    owlgrey

    Joined:
    Jul 30, 2020
    Posts:
    2
    Thanks for the suggestion, Lo-renzo!
    Masking does look like it helps the lights look like they have a lot more depth, but it won't help my dynamically created lights ability to shine on a background or foreground sorting layer.

    What I did for now, was create a prefab that has the light component with the layers I want to use, and then just instantiate them as required.

    Still seems like I'm missing something though. Are sorting layers not something that are expected to be changed at runtime?
    I will poke around the Light2D script a bit more and look into the possibility of reflection.