Skip to content

Instantly share code, notes, and snippets.

@CraftedPvP
Last active September 6, 2024 08:58
Show Gist options
  • Save CraftedPvP/44c24388ac9c3aff26361fdbc4beabb6 to your computer and use it in GitHub Desktop.
Save CraftedPvP/44c24388ac9c3aff26361fdbc4beabb6 to your computer and use it in GitHub Desktop.
// in Start() of "(Unity Project path)\Library\PackageCache\[email protected]\Runtime\2D\ShadowCaster2D.cs"
// have replace it with this:
private void Awake() {
if(m_ApplyToSortingLayers == null)
m_ApplyToSortingLayers = SetDefaultSortingLayers();
Bounds bounds = new Bounds(transform.position, Vector3.one);
Renderer renderer = GetComponent<Renderer>();
if (renderer != null)
{
bounds = renderer.bounds;
}
else
{
Collider2D collider = GetComponent<Collider2D>();
if (collider != null)
if (collider.GetType() == typeof(PolygonCollider2D)) {
m_ShapePath = Array.ConvertAll<Vector2, Vector3>(((PolygonCollider2D)collider).GetPath(0), vec2To3);
m_UseRendererSilhouette = false;
} else {
bounds = collider.bounds;
}
}
Vector3 relOffset = bounds.center - transform.position;
if (m_ShapePath == null || m_ShapePath.Length == 0)
{
m_ShapePath = new Vector3[]
{
relOffset + new Vector3(-bounds.extents.x, -bounds.extents.y),
relOffset + new Vector3(bounds.extents.x, -bounds.extents.y),
relOffset + new Vector3(bounds.extents.x, bounds.extents.y),
relOffset + new Vector3(-bounds.extents.x, bounds.extents.y)
};
}
}
Vector3 vec2To3(Vector2 inputVector) {
return new Vector3(inputVector.x, inputVector.y, 0);
}
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
using System.Collections.Generic;
/// <summary>
/// Creates shadow casters for 2D tilemaps
/// </summary>
[ExecuteInEditMode]
[DisallowMultipleComponent]
[RequireComponent(typeof(CompositeCollider2D))]
[AddComponentMenu("Rendering/2D/Shadow Caster 2D Tilemap")]
public class ShadowCaster2DTilemap : MonoBehaviour
{
public CompositeCollider2D tilemapCollider;
private GameObject shadowCasterContainer;
public string ShadowCasterContainerName = "shadow_casters";
public List<GameObject> shadowCasters = new List<GameObject>();
private int previousPointCount;
public void Start()
{
tilemapCollider = GetComponent<CompositeCollider2D>();
shadowCasterContainer = GameObject.Find(ShadowCasterContainerName);
if (!GameObject.Find(ShadowCasterContainerName))
{
shadowCasterContainer = new GameObject(ShadowCasterContainerName);
}
else
{
// Clear existing shadow casters
if (Application.isPlaying)
{
foreach (Transform child in shadowCasterContainer.transform)
{
Destroy(child.gameObject);
}
}
else
{
while (shadowCasterContainer.transform.childCount != 0)
{
DestroyImmediate(shadowCasterContainer.transform.GetChild(0).gameObject);
}
}
}
GenerateShadowCasters(false);
}
public void Update()
{
if (previousPointCount != tilemapCollider.pointCount)
{
GenerateShadowCasters(true);
}
}
public void GenerateShadowCasters(bool clearExisting)
{
if (clearExisting)
{
// Clear existing shadow casters
if (Application.isPlaying)
{
foreach (Transform child in shadowCasterContainer.transform)
{
Destroy(child.gameObject);
}
}
else
{
while (shadowCasterContainer.transform.childCount != 0)
{
DestroyImmediate(shadowCasterContainer.transform.GetChild(0).gameObject);
}
}
}
previousPointCount = tilemapCollider.pointCount;
for (int i = 0; i < tilemapCollider.pathCount; i++)
{
Vector2[] pathVertices = new Vector2[tilemapCollider.GetPathPointCount(i)];
tilemapCollider.GetPath(i, pathVertices);
GameObject shadowCaster = new GameObject(ShadowCasterContainerName + "_" + i);
PolygonCollider2D shadowPolygon = (PolygonCollider2D)shadowCaster.AddComponent(typeof(PolygonCollider2D));
shadowCaster.transform.parent = shadowCasterContainer.transform;
shadowPolygon.points = pathVertices;
shadowPolygon.enabled = false;
ShadowCaster2D shadowCasterComponent = shadowCaster.AddComponent<ShadowCaster2D>();
shadowCasterComponent.selfShadows = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment