Advertisement
dmitryzenevich

Untitled

Jul 12th, 2020
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. namespace Test
  5. {
  6.     public class ClickOnTroughUiObjects : MonoBehaviour
  7.     {
  8.         [SerializeField] private RectTransform _rectTransform;
  9.  
  10.         public UnityEvent OnClick = null;
  11.  
  12.         private RectTransform RectTransform =>
  13.             _rectTransform != null && ReferenceEquals(_rectTransform.gameObject, gameObject)
  14.                 ? _rectTransform
  15.                 : _rectTransform = transform as RectTransform;
  16.  
  17. #if UNITY_EDITOR
  18.         private void OnValidate()
  19.         {
  20.             var rectTransform = RectTransform;
  21.         }
  22. #endif
  23.  
  24.         private void Update()
  25.         {
  26.             if (Input.GetMouseButtonUp(0))
  27.             {
  28.                 var localMousePosition = RectTransform.InverseTransformPoint(Input.mousePosition);
  29.                 if (RectTransform.rect.Contains(localMousePosition))
  30.                     OnClick?.Invoke();
  31.             }
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement