Created
March 8, 2015 15:35
-
-
Save frarees/8b3a834eb498274525fe to your computer and use it in GitHub Desktop.
Filter canvas raycasts with Collider2D data (Unity3D)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
[RequireComponent (typeof (Image), typeof (Collider2D))] | |
public class ColliderMask : MonoBehaviour, ICanvasRaycastFilter { | |
Image m_Image; | |
Collider2D m_Collider; | |
void Awake () { | |
m_Image = GetComponent<Image> (); | |
m_Collider = GetComponent<Collider2D> (); | |
} | |
bool ICanvasRaycastFilter.IsRaycastLocationValid (Vector2 sp, Camera eventCamera) { | |
Vector3 point = Vector3.zero; | |
if (RectTransformUtility.ScreenPointToWorldPointInRectangle (m_Image.rectTransform, sp, eventCamera, out point)) { | |
return m_Collider.OverlapPoint (new Vector2 (point.x, point.y)); | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment