Create a custom 2D shadow
Create a custom 2D shadow using the ShadowShape2DProvider API.
Read time 2 minutesLast updated 4 days ago
To create a custom 2D shadow, use the API. For example to create a custom shadow shape.
ShadowShape2DProviderFollow these steps:
-
In a C# script, create a class that inherits from.
ShadowShape2DProvider -
Implement themethod and return a
ProviderNameobject with your name for the new shadow type. The name appears in the Casting Source property of the Shadow Caster 2D component reference. For example:GUIContentusing UnityEngine;using UnityEngine.Rendering.Universal;[System.Serializable]public class CustomShadow : ShadowShape2DProvider{ public override GUIContent ProviderName(string componentName) { return new GUIContent("My Custom Shadow"); }} -
Implement themethod, which Unity calls for each component on the GameObject. Return
IsRequiredComponentDatafor the component that has the data the shadow needs, for example the Transform component.truepublic override bool IsRequiredComponentData(Component currentComponent){ if (currentComponent is Transform){ return true; } else { return false; }} -
Implement themethod to set the shape of the shadow. Unity passes in a
OnBeforeRenderinstance, which you can use to callShadowShape2Dwith a shadow shape. Use native arrays for the shadow shape data. For more information about native arrays, refer to Introduction to NativeContainer.SetShape -
To create custom parameters, add a public field to the class. For example:public float size = 1f;
-
Add a Shadow Caster 2D component to a GameObject, and set its Casting Source to your new shadow shape. The public field appears in a new Provider section in thewindow.Inspector
?
For more information, refer to the API.
ShadowShape2DProviderExample
The following example creates a custom shadow that has an outline triangle shape and a size parameter.
using Unity.Collections;using UnityEngine;using UnityEngine.Rendering.Universal;[System.Serializable]public class TriangleShadow : ShadowShape2DProvider{ public override GUIContent ProviderName(string componentName) => new GUIContent("Triangle shadow"); public override bool IsRequiredComponentData(Component currentComponent){ if (currentComponent is Transform){ return true; } else { return false; } } [SerializeField] float size = 1f; public override void OnBeforeRender(Component sourceComponent, Bounds cullingBounds, ShadowShape2D shadowShape) { Transform triangle = sourceComponent.transform; NativeArray<Vector3> vertices = new NativeArray<Vector3>(3, Allocator.Temp); vertices[0] = triangle.TransformPoint(new Vector3(-size, -size, 0f)); vertices[1] = triangle.TransformPoint(new Vector3(size, -size, 0f)); vertices[2] = triangle.TransformPoint(new Vector3(0f, size, 0f)); NativeArray<int> indices = new NativeArray<int>(6, Allocator.Temp); indices[0] = 0; indices[1] = 1; indices[2] = 1; indices[3] = 2; indices[4] = 2; indices[5] = 0; shadowShape.SetShape(vertices, indices, ShadowShape2D.OutlineTopology.Lines); vertices.Dispose(); indices.Dispose(); }}