Version: Unity 6.6 Alpha (6000.6)
Language : English
Enable 2D lighting with the Tilemap Renderer 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 meshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info
    See in Glossary
    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 InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    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 gizmosA graphic overlay associated with a GameObject in a Scene, and displayed in the Scene View. Built-in scene tools such as the move tool are Gizmos, and you can create custom Gizmos using textures or scripting. Some Gizmos are only drawn when the GameObject is selected, while other Gizmos are drawn by the Editor regardless of which GameObjects are selected. More info
See in Glossary
for the light type in the SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
view.

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

Additional resources

Enable 2D lighting with the Tilemap Renderer in URP
Light 2D component reference for URP