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.
Read time 2 minutesLast updated 25 days ago
To create a custom 2D light type, use the API. For example to create a new light type with a custom shape.
Light2DProviderFollow these steps:
-
In a C# script, create a class that inherits from.
Light2DProvider -
Implement themethod and return a
ProviderNameobject 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:GUIContentusing UnityEngine;using UnityEngine.Rendering.Universal;[System.Serializable]public class QuadLight : Light2DProvider{ public override GUIContent ProviderName() { return new GUIContent("My Quad Light"); }} -
Implement themethod to return a mesh that defines the shape of the light.
GetMesh() -
To create custom parameters, add a serialized field to the class. For example:[SerializeField] float size = 1f;
-
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 thewindow.Inspector
?
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 method to draw gizmos for the light type in the Scene view.
OnDrawGizmosFor more information, refer to the enumeration value.
Light2D.LightType.Provider