Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System.Collections.Generic;
  5.  
  6. public class CustomRaycaster : MonoBehaviour
  7. {
  8.     [HideInInspector]
  9.     public GameObject hovered = null;
  10.     [HideInInspector]
  11.     public GameObject hitObject;
  12.     [HideInInspector]
  13.     bool isHit;
  14.  
  15.     public enum HoverState
  16.     {
  17.         Hover, None
  18.     };
  19.  
  20.     [HideInInspector]
  21.     public HoverState hover_state = HoverState.None;
  22.     [HideInInspector]
  23.     PointerEventData pointerData;
  24.     [HideInInspector]
  25.     RaycastHit hitInfo;
  26.     [HideInInspector]
  27.     Ray ray;
  28.  
  29.     [HideInInspector]
  30.     List<RaycastResult> results = new List<RaycastResult>();
  31.  
  32.     void Update ()
  33.     {
  34.         pointerData = new PointerEventData(EventSystem.current);
  35.         pointerData.position = Input.mousePosition;
  36.        
  37.         results.Clear();
  38.         EventSystem.current.RaycastAll(pointerData, results);
  39.        
  40.         if (results.Count > 0)
  41.         {
  42.             hitObject = results[0].gameObject;
  43. //          Debug.Log(Input.mousePosition.x.ToString()+" "+Input.mousePosition.y.ToString()+" "+hitObject.name);
  44.             isHit = true;
  45.         }
  46.         else
  47.         {
  48.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  49.             isHit = (Physics.Raycast(ray, out hitInfo));
  50.             if (isHit)
  51.             {
  52.                 hitObject = hitInfo.collider.gameObject;
  53.             }
  54.         }
  55.         Hover(isHit);
  56.     }
  57.  
  58.     protected void Hover(bool isHit)
  59.     {
  60.         if (isHit)
  61.         {
  62.             if (hover_state == HoverState.None)
  63.             {
  64.                 IClickable click = WholeObject(hitObject);
  65.                 if (click != null) click.OnMouseEnterCustom();
  66.                 hovered = hitObject;
  67.             }
  68.             else
  69.             {
  70.                 if (hovered != hitObject)
  71.                 {
  72.                     IClickable click = WholeObject(hovered);
  73.                     if (click != null) click.OnMouseExitCustom();
  74.                     click = WholeObject(hitObject);
  75.                     if (click != null) click.OnMouseEnterCustom();
  76.                     hovered = hitObject;
  77.                 }
  78.             }
  79.             hover_state = HoverState.Hover;
  80.         }
  81.         else
  82.         {
  83.             if (hover_state == HoverState.Hover)
  84.             {
  85.                 IClickable click = WholeObject(hovered);
  86.                 if (click != null) click.OnMouseExitCustom();
  87.                 hovered = null;
  88.                 hover_state = HoverState.None;
  89.             }
  90.         }
  91.         if (hover_state == HoverState.Hover)
  92.         {
  93.             IClickable click = WholeObject(hovered);
  94.             if (click != null) click.OnMouseOverCustom();
  95.         }
  96.     }
  97.  
  98.     public IClickable WholeObject(GameObject o) //search object and parent for being IClickables
  99.     {
  100.         IClickable click = o.GetComponent<IClickable>();
  101.         if (click == null)
  102.         {
  103.             click = o.GetComponentInParent<IClickable>();
  104.         }
  105.         return click;
  106.     }
  107. }
  108.  
  109. public class RaycastFilter : MonoBehaviour, ICanvasRaycastFilter //partially invisible (think odd shaped buttons)
  110. {
  111.     Collider2D myCollider;
  112.     RectTransform rectTransform;
  113.    
  114.     public void Initialization()
  115.     {
  116.         myCollider = GetComponent<Collider2D>();
  117.         rectTransform = GetComponent<RectTransform>();
  118.     }
  119.  
  120.     public bool IsRaycastLocationValid (Vector2 screenPos, Camera eventCamera)
  121.     {
  122.         Vector3 worldPoint = Vector3.zero;
  123.         bool isInside = RectTransformUtility.ScreenPointToWorldPointInRectangle(
  124.             rectTransform,
  125.             screenPos,
  126.             eventCamera,
  127.             out worldPoint
  128.             );
  129.         if (isInside)
  130.             isInside = myCollider.OverlapPoint(worldPoint);
  131.        
  132.         return isInside;
  133.     }
  134. }
  135.  
  136. public class RaycastInvisible : MonoBehaviour, ICanvasRaycastFilter //invisible to raycasts (think always invisible panels)
  137. {
  138.     public IRaycastHidable owner;
  139.  
  140.     public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
  141.     {
  142.         return (!owner.raycastHidden);
  143.     }
  144. }
  145.  
  146. public interface IClickable //can interact with raycaster
  147. {
  148.     void OnMouseEnterCustom();
  149.                    
  150.     void OnMouseExitCustom();
  151.                    
  152.     void OnMouseOverCustom();
  153. }
  154.  
  155. public interface IRaycastHidable
  156. {
  157.     bool raycastHidden { get; set; }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement