Advertisement
Guest User

Unity 4.6 UI Collider Mask

a guest
Feb 1st, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. [ExecuteInEditMode]
  6. public class UIColliderMask : MonoBehaviour, ICanvasRaycastFilter
  7. {
  8.     private RectTransform _rectTransform = null;
  9.    
  10.     public RectTransform rectTransform
  11.     {
  12.         get
  13.         {
  14. #if UNITY_EDITOR
  15.             if(!UnityEditor.EditorApplication.isPlaying)
  16.                 return GetComponent<RectTransform>();
  17. #endif
  18.             return _rectTransform ?? (_rectTransform = GetComponent<RectTransform>());
  19.         }
  20.     }
  21.  
  22.     void Update()
  23.     {
  24.         float s = rectTransform.rect.width * colliderAspect;
  25.        
  26.         colliderGameObject.transform.localScale = new Vector3 (s, s, s);
  27.     }
  28.    
  29.     public GameObject colliderGameObject = null;
  30.  
  31.     public float colliderAspect = 0.0f;
  32.    
  33.     public bool IsRaycastLocationValid(Vector2 screenPosition, Camera raycastEventCamera)
  34.     {
  35.         return colliderGameObject.collider2D.OverlapPoint (screenPosition);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement