Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using System.Collections.Generic;
- public class CustomRaycaster : MonoBehaviour
- {
- [HideInInspector]
- public GameObject hovered = null;
- [HideInInspector]
- public GameObject hitObject;
- [HideInInspector]
- bool isHit;
- public enum HoverState
- {
- Hover, None
- };
- [HideInInspector]
- public HoverState hover_state = HoverState.None;
- [HideInInspector]
- PointerEventData pointerData;
- [HideInInspector]
- RaycastHit hitInfo;
- [HideInInspector]
- Ray ray;
- [HideInInspector]
- List<RaycastResult> results = new List<RaycastResult>();
- void Update ()
- {
- pointerData = new PointerEventData(EventSystem.current);
- pointerData.position = Input.mousePosition;
- results.Clear();
- EventSystem.current.RaycastAll(pointerData, results);
- if (results.Count > 0)
- {
- hitObject = results[0].gameObject;
- // Debug.Log(Input.mousePosition.x.ToString()+" "+Input.mousePosition.y.ToString()+" "+hitObject.name);
- isHit = true;
- }
- else
- {
- ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- isHit = (Physics.Raycast(ray, out hitInfo));
- if (isHit)
- {
- hitObject = hitInfo.collider.gameObject;
- }
- }
- Hover(isHit);
- }
- protected void Hover(bool isHit)
- {
- if (isHit)
- {
- if (hover_state == HoverState.None)
- {
- IClickable click = WholeObject(hitObject);
- if (click != null) click.OnMouseEnterCustom();
- hovered = hitObject;
- }
- else
- {
- if (hovered != hitObject)
- {
- IClickable click = WholeObject(hovered);
- if (click != null) click.OnMouseExitCustom();
- click = WholeObject(hitObject);
- if (click != null) click.OnMouseEnterCustom();
- hovered = hitObject;
- }
- }
- hover_state = HoverState.Hover;
- }
- else
- {
- if (hover_state == HoverState.Hover)
- {
- IClickable click = WholeObject(hovered);
- if (click != null) click.OnMouseExitCustom();
- hovered = null;
- hover_state = HoverState.None;
- }
- }
- if (hover_state == HoverState.Hover)
- {
- IClickable click = WholeObject(hovered);
- if (click != null) click.OnMouseOverCustom();
- }
- }
- public IClickable WholeObject(GameObject o) //search object and parent for being IClickables
- {
- IClickable click = o.GetComponent<IClickable>();
- if (click == null)
- {
- click = o.GetComponentInParent<IClickable>();
- }
- return click;
- }
- }
- public class RaycastFilter : MonoBehaviour, ICanvasRaycastFilter //partially invisible (think odd shaped buttons)
- {
- Collider2D myCollider;
- RectTransform rectTransform;
- public void Initialization()
- {
- myCollider = GetComponent<Collider2D>();
- rectTransform = GetComponent<RectTransform>();
- }
- public bool IsRaycastLocationValid (Vector2 screenPos, Camera eventCamera)
- {
- Vector3 worldPoint = Vector3.zero;
- bool isInside = RectTransformUtility.ScreenPointToWorldPointInRectangle(
- rectTransform,
- screenPos,
- eventCamera,
- out worldPoint
- );
- if (isInside)
- isInside = myCollider.OverlapPoint(worldPoint);
- return isInside;
- }
- }
- public class RaycastInvisible : MonoBehaviour, ICanvasRaycastFilter //invisible to raycasts (think always invisible panels)
- {
- public IRaycastHidable owner;
- public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
- {
- return (!owner.raycastHidden);
- }
- }
- public interface IClickable //can interact with raycaster
- {
- void OnMouseEnterCustom();
- void OnMouseExitCustom();
- void OnMouseOverCustom();
- }
- public interface IRaycastHidable
- {
- bool raycastHidden { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement