Version: Unity 6.5 (6000.5)
Language : English
Optimize 2D lights with batching in URP
Light 2D component reference for URP

Create a custom 2D light type

To create a custom 2D light type, use the Light2DProvider API. For example to create a new light type with a custom shape.

Follow these steps:

  1. In a C# script, create a class that inherits from Light2DProvider.

  2. Implement the ProviderName method and return a GUIContent object with your name for the new light type. The name appears in the Light Type property of the Light 2D component reference for URP. For example:

    using UnityEngine;
    using UnityEngine.Rendering.Universal;
    
    [System.Serializable]
    public class QuadLight : Light2DProvider
    {
        public override GUIContent ProviderName() {
            return new GUIContent("My Quad Light");
        }
    }
    
  3. Implement the GetMesh() method to return a mesh that defines the shape of the light.

  4. To create custom parameters, add a serialized field to the class. For example:

    [SerializeField] float size = 1f;
    
  5. Select an existing 2D light in the Hierarchy view, then change Light Type to your new type. The serialized field appears in a new Provider section in the Inspector window.

Example

The following example creates a new 2D light type that has a triangle shape and a scale parameter.

using UnityEngine;
using UnityEngine.Rendering.Universal;

[System.Serializable]
public class MyTriangleLight : Light2DProvider
{
    public override GUIContent ProviderName() => new GUIContent("Triangle Light");

    [SerializeField] float scale = 0.5f;

    public override Mesh GetMesh()
    {
        Mesh triangleMesh = new Mesh
        {
            vertices = new[]
            {
                new Vector3(-scale, -scale, 0f),
                new Vector3(scale, -scale, 0f),
                new Vector3(scale, scale, 0f),
            },
            triangles = new[] { 0, 1, 2 }
        };
        triangleMesh.RecalculateBounds();
        triangleMesh.RecalculateNormals();
        return triangleMesh;
    }
}

In the Unity Editor, you can also override the OnDrawGizmos method to draw gizmos for the light type in the Scene view.

For more information, refer to the Light2D.LightType.Provider enumeration value.

Additional resources

Optimize 2D lights with batching in URP
Light 2D component reference for URP