To create a custom 2D shadow, use the ShadowShape2DProvider API. For example to create a custom shadow shape.
Follow these steps:
In a C# script, create a class that inherits from ShadowShape2DProvider.
Implement the ProviderName method and return a GUIContent object 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:
using UnityEngine;
using UnityEngine.Rendering.Universal;
[System.Serializable]
public class CustomShadow : ShadowShape2DProvider
{
public override GUIContent ProviderName(string componentName)
{
return new GUIContent("My Custom Shadow");
}
}
Implement the IsRequiredComponentData method, which Unity calls for each component on the GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary. Return true for the component that has the data the shadow needs, for example the Transform componentA Transform component determines the Position, Rotation, and Scale of each object in the scene. Every GameObject has a Transform. More info
See in Glossary.
public override bool IsRequiredComponentData(Component currentComponent){
if (currentComponent is Transform){
return true;
} else {
return false;
}
}
Implement the OnBeforeRender method to set the shape of the shadow. Unity passes in a ShadowShape2D instance, which you can use to call SetShape with a shadow shape. Use native arrays for the shadow shape data. For more information about native arrays, refer to Introduction to NativeContainer.
Note: If the spriteA 2D graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. More info
See in Glossary is static, set the shadow shape once using the OnInitialized method instead. For more information, refer to the ShadowShape2DProvider API.
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 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.
For more information, refer to the ShadowShape2DProvider API.
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();
}
}