Automatic generator for ShadowCaster2D components based on sprite alpha channels. Suitable for creating complex 2D shadows in the Universal Render Pipeline.
- Generates
ShadowCaster2D
components based on sprite outlines. - Determines shadow shape considering transparency (alpha channel).
- Saves created objects as prefabs to a specified folder.
- Cleans up temporary objects from the scene.
- Visualizes contours in the editor via Gizmos.
- Enhanced Efficiency & Advanced Automation:
- Dramatically boosts productivity by transforming 1-5 hours of manual effort into a swift 30-second to 2-minute automated process.
- Utilizes sophisticated algorithms for alpha channel-based pixel traversal and contour mask creation, followed by precise contour tracing and coordinate conversion. This solution was developed with AI guidance, demonstrating an innovative approach to complex automation challenges.
- Add the
ShadowCaster2DMaker
component to aGameObject
in your scene. - Populate the sprites list, select the folder, and specify the
targetFolderName
. - Call
GenerateShadows()
from the Inspector or via an editor script. - If needed, call
ClearMadeGameObjects()
to remove temporary objects.
To generate shadows without saving them to a folder, activate the DoNotSaveToFolder flag in the Advanced menu.
⚠️ Notes:
- If you enable DoNotSaveToFolder during generation and then disable it, the generated objects won't be saved to the folder, and
ClearMadeGameObjects
will no longer have a reference to them.ClearMadeGameObjects
will remove all objects created in the current session from the scene, provided it still holds references to them (see above).
A component for generating shadows from sprites in the editor.
Sprite[] sprites
— An array of sprites for generation.DefaultAsset folder
— The root folder for saving the results.string targetFolderName
— The name of the subfolder for the output.- Advanced:
bool doNotSaveToFolder
— A flag to prevent saving as prefabs.
GenerateShadows()
— The main method for generating shadows.ClearMadeGameObjects()
— Cleans up created objects from the scene (if they were saved as prefabs or cleared manually).
A helper static class for processing sprites.
- GetPixels — Retrieves pixels from the sprite's texture.
- GetAlphaMask — Creates a 2D mask of opaque pixels.
- ExtractContourMask — Extracts the contour based on the mask.
- ExtractContourPoints — Forms a sequence of points along the contour.
- SimplifyPoints — Simplifies the geometry (removes redundant points).
- ConvertToLocalPoints — Converts points to local coordinates.
A Vector3[]
array of points for the m_ShapePath
field of the ShadowCaster2D
component.
A Gizmos component for displaying the shadow contour in the editor.
ShadowCaster2D shadowCaster
— A reference to the caster.
- Draws Gizmos lines between the points of the
m_ShapePath
private field of theShadowCaster2D
component.
A helper static class for saving shadows as prefabs.
const string PREFAB_EXTENSION
- The string".prefab"
used to construct the path before saving.
- Determines the save path.
- Saves a
GameObject
as a.prefab
file.
- The scripts use reflection to access private fields and methods of
ShadowCaster2D
andShadowUtility
. - Tested with
Unity 2022.3.35f1
andURP 14.0.11
, may not work or might cause errors with other Unity versions. - Works only in the Unity Editor, as it utilizes
UnityEditor
,Undo
,EditorUtility
, etc. - It is assumed that Universal Render Pipeline (URP) is already integrated into the project.
SerhiiStudio
Unity Tools & Games
GitHub: SerhiiStudio