Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- public class SmartInput : MonoBehaviour
- {
- // Fixed size array for hit testing to avoid repeated memory allocation
- // If you might have more than 10 overlapping colliders, you may need to increase array size
- Collider2D[] Hits = new Collider2D[10];
- void Update()
- {
- // Get all 2D colliders under the mouse/finger
- Vector3 v3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
- int hitCount = Physics2D.OverlapPointNonAlloc(new Vector2(v3.x, v3.y), Hits);
- // If none, do nothing.
- if (hitCount == 0)
- return;
- // This will be set to front-most collider
- Collider2D frontMostHit = null;
- // Keep track of highest sort order found so far
- uint highestSortOrder = 0;
- // For each collider hit...
- for(int x=0;x<hitCount;x++)
- {
- Collider2D hit = Hits[x];
- // Get sprite render
- SpriteRenderer sr = hit.GetComponent<SpriteRenderer>();
- // Convert sorting order to unsigned value by adding the minimum (negative) value it can possibly have
- uint uSortingOrder = (uint)(sr.sortingOrder + short.MinValue);
- // In my game I use 3 sorting layers, you will need to adjust this. SortingLayerOrder needs to have
- // a value of 0 for your first layer, 1 for second, 2 for third, and so on. This code
- uint sortingLayerOrder;
- if (sr.sortingLayerID == 0) // Default
- sortingLayerOrder = 1;
- else if (sr.sortingLayerID == 1) // Background
- sortingLayerOrder = 0;
- else
- {
- if (sr.sortingLayerID != 2) // Foreground
- throw new Exception("You updated sorting layers but forgot to update this code!");
- sortingLayerOrder = 2;
- }
- // Assign base value for layer based on layer order
- // 1st layer = 0 to 65535
- // 2nd layer = 65536 to 131,070
- // 3rd layer = 131,070 to 196,605
- // Each value increases by ushort.MaxValue to ensure that the highest value in a prior layer can never be
- // greater than the lowest value in the next layer.
- uint sortingLayerBase = sortingLayerOrder * ushort.MaxValue;
- // Actual sort order is layer base value + object order
- uint sortOrder = sortingLayerBase + uSortingOrder;
- // If sort order is same (mostly to ensure we always have something) or higher than our highest so far...
- if (sortOrder >= highestSortOrder)
- {
- // Save new high value
- highestSortOrder = sortOrder;
- // This hit is our current front-most collider
- frontMostHit = hit;
- }
- }
- // If left mouse or finger just went down between last frame and this one...
- if (Input.GetMouseButtonDown(0))
- {
- // Fire OnLeftDown event if this object has one
- frontMostHit.SendMessage("OnLeftDown", SendMessageOptions.DontRequireReceiver);
- }
- // If left mouse or finger is down...
- if (Input.GetMouseButton(0))
- {
- // Fire OnLeftHoverDown event if this object has one
- frontMostHit.SendMessage("OnLeftHoverDown", SendMessageOptions.DontRequireReceiver);
- }
- // Always fire OnHover if this object has one
- frontMostHit.SendMessage("OnHover", SendMessageOptions.DontRequireReceiver);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement