diff options
Diffstat (limited to 'Pathfinding Visualizer/Assets/CodeMonkey/MonoBehaviours/PositionRendererSorter.cs')
| -rw-r--r-- | Pathfinding Visualizer/Assets/CodeMonkey/MonoBehaviours/PositionRendererSorter.cs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Pathfinding Visualizer/Assets/CodeMonkey/MonoBehaviours/PositionRendererSorter.cs b/Pathfinding Visualizer/Assets/CodeMonkey/MonoBehaviours/PositionRendererSorter.cs new file mode 100644 index 0000000..46f2058 --- /dev/null +++ b/Pathfinding Visualizer/Assets/CodeMonkey/MonoBehaviours/PositionRendererSorter.cs @@ -0,0 +1,51 @@ +/* + ------------------- Code Monkey ------------------- + + Thank you for downloading the Code Monkey Utilities + I hope you find them useful in your projects + If you have any questions use the contact form + Cheers! + + unitycodemonkey.com + -------------------------------------------------- + */ + +using UnityEngine; + +namespace CodeMonkey.MonoBehaviours { + + /* + * Automatically sort a Renderer (SpriteRenderer, MeshRenderer) based on his Y position + * */ + public class PositionRendererSorter : MonoBehaviour { + + [SerializeField] private int sortingOrderBase = 5000; // This number should be higher than what any of your sprites will be on the position.y + [SerializeField] private int offset = 0; + [SerializeField] private bool runOnlyOnce = false; + + private float timer; + private float timerMax = .1f; + private Renderer myRenderer; + + private void Awake() { + myRenderer = gameObject.GetComponent<Renderer>(); + } + + private void LateUpdate() { + timer -= Time.deltaTime; + if (timer <= 0f) { + timer = timerMax; + myRenderer.sortingOrder = (int)(sortingOrderBase - transform.position.y - offset); + if (runOnlyOnce) { + Destroy(this); + } + } + } + + public void SetOffset(int offset) { + this.offset = offset; + } + + } + +}
\ No newline at end of file |
