Advertisement
Guest User

Untitled

a guest
May 13th, 2014
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System;
  3.  
  4. public class SmartInput : MonoBehaviour
  5. {
  6.     // Fixed size array for hit testing to avoid repeated memory allocation
  7.     //  If you might have more than 10 overlapping colliders, you may need to increase array size
  8.     Collider2D[] Hits = new Collider2D[10];
  9.  
  10.     void Update()
  11.     {
  12.         // Get all 2D colliders under the mouse/finger
  13.         Vector3 v3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  14.         int hitCount = Physics2D.OverlapPointNonAlloc(new Vector2(v3.x, v3.y), Hits);
  15.  
  16.         // If none, do nothing.
  17.         if (hitCount == 0)
  18.             return;
  19.  
  20.         // This will be set to front-most collider
  21.         Collider2D frontMostHit = null;
  22.  
  23.         // Keep track of highest sort order found so far
  24.         uint highestSortOrder = 0;
  25.  
  26.         // For each collider hit...
  27.         for(int x=0;x<hitCount;x++)
  28.         {
  29.             Collider2D hit = Hits[x];
  30.  
  31.             // Get sprite render
  32.             SpriteRenderer sr = hit.GetComponent<SpriteRenderer>();
  33.  
  34.             // Convert sorting order to unsigned value by adding the minimum (negative) value it can possibly have
  35.             uint uSortingOrder = (uint)(sr.sortingOrder + short.MinValue);
  36.  
  37.             // In my game I use 3 sorting layers, you will need to adjust this.  SortingLayerOrder needs to have
  38.             //  a value of 0 for your first layer, 1 for second, 2 for third, and so on.  This code
  39.             uint sortingLayerOrder;
  40.             if (sr.sortingLayerID == 0) // Default
  41.                 sortingLayerOrder = 1;
  42.             else if (sr.sortingLayerID == 1) // Background
  43.                 sortingLayerOrder = 0;
  44.             else
  45.             {
  46.                 if (sr.sortingLayerID != 2) // Foreground
  47.                     throw new Exception("You updated sorting layers but forgot to update this code!");
  48.                 sortingLayerOrder = 2;
  49.             }
  50.  
  51.             // Assign base value for layer based on layer order
  52.             //  1st layer = 0 to 65535
  53.             //  2nd layer = 65536 to 131,070
  54.             //  3rd layer = 131,070 to 196,605
  55.             //  Each value increases by ushort.MaxValue to ensure that the highest value in a prior layer can never be
  56.             //   greater than the lowest value in the next layer.
  57.             uint sortingLayerBase = sortingLayerOrder * ushort.MaxValue;
  58.  
  59.             // Actual sort order is layer base value + object order
  60.             uint sortOrder = sortingLayerBase + uSortingOrder;
  61.  
  62.             // If sort order is same (mostly to ensure we always have something) or higher than our highest so far...
  63.             if (sortOrder >= highestSortOrder)
  64.             {
  65.                 // Save new high value
  66.                 highestSortOrder = sortOrder;
  67.  
  68.                 // This hit is our current front-most collider
  69.                 frontMostHit = hit;
  70.             }
  71.         }
  72.  
  73.         // If left mouse or finger just went down between last frame and this one...
  74.         if (Input.GetMouseButtonDown(0))
  75.         {
  76.             // Fire OnLeftDown event if this object has one
  77.             frontMostHit.SendMessage("OnLeftDown", SendMessageOptions.DontRequireReceiver);
  78.         }
  79.        
  80.         // If left mouse or finger is down...
  81.         if (Input.GetMouseButton(0))
  82.         {
  83.             // Fire OnLeftHoverDown event if this object has one
  84.             frontMostHit.SendMessage("OnLeftHoverDown", SendMessageOptions.DontRequireReceiver);
  85.         }
  86.  
  87.         // Always fire OnHover if this object has one
  88.         frontMostHit.SendMessage("OnHover", SendMessageOptions.DontRequireReceiver);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement