1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| using System; using UnityEngine; [RequireComponent(typeof(SpriteRenderer))] public class SpriteShadow : MonoBehaviour{ [HideInInspector] public Vector2 offset; public float offsetX; public float offsetY; private SpriteRenderer sprRndCaster; private SpriteRenderer sprRndShadow; private Transform transCaster; private Transform transShadow; [HideInInspector] public Material ShadowMaterial; [Tooltip("Order in Layer")] public int sortingOrderDifference;
public bool updatePositionOverTime = true; private void Start(){ this.offset = new Vector2(this.offsetX, this.offsetY); this.transCaster = this.transform; this.transShadow = new GameObject().transform; this.transShadow.parent = this.transCaster; this.transShadow.gameObject.name = "Shadow"; this.transShadow.localRotation = Quaternion.identity; var vector = Vector3.one; var parent = this.transShadow; while (parent != null){ parent = parent.parent; } this.transShadow.localScale = vector; this.sprRndCaster = this.GetComponent<SpriteRenderer>(); this.sprRndShadow = this.transShadow.gameObject.AddComponent<SpriteRenderer>(); this.sprRndShadow.material = this.sprRndCaster.material; this.sprRndShadow.material.color = new Color(1,1,1,0.35f); this.sprRndShadow.color = Color.black; this.sprRndShadow.sortingLayerName = this.sprRndCaster.sortingLayerName; this.sprRndShadow.sortingOrder = this.sprRndCaster.sortingOrder + this.sortingOrderDifference - 1; this.transShadow.position = new Vector2(this.transCaster.position.x + this.offset.x, this.transCaster.position.y + this.offset.y); this.sprRndShadow.sprite = this.sprRndCaster.sprite; this.sprRndShadow.flipX = this.sprRndCaster.flipX; }
private void LateUpdate() { if (this.updatePositionOverTime) { this.transShadow.position = new Vector2(this.transCaster.position.x + this.offset.x, this.transCaster.position.y + this.offset.y); } } }
|