123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
-
- namespace NRKernal
- {
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
-
- [DisallowMultipleComponent]
- public class NRMultScrPointerRaycaster : NRPointerRaycaster
- {
-
- public GameObject Mouse;
-
- private Camera m_UICamera;
-
- private float m_ScreenWidth;
-
- private float m_ScreenHeight;
-
- private Vector3 m_LastTouch = m_FarAwayPos;
-
- private static Vector3 m_FarAwayPos = Vector3.one * 10000f;
-
- protected override void Awake()
- {
- base.Awake();
- m_UICamera = gameObject.GetComponent<Camera>();
-
- var resolution = NRPhoneScreen.Resolution;
- m_ScreenWidth = resolution.x;
- m_ScreenHeight = resolution.y;
- }
-
-
- public void UpdateScreenSize(Vector2 size)
- {
- m_ScreenWidth = size.x;
- m_ScreenHeight = size.y;
- }
-
- public override void Raycast()
- {
- sortedRaycastResults.Clear();
- breakPoints.Clear();
- var zScale = transform.lossyScale.z;
- var amountDistance = (FarDistance - NearDistance) * zScale;
- float distance;
- Ray ray;
- RaycastResult firstHit = default(RaycastResult);
- distance = amountDistance;
- var touch_x = NRVirtualDisplayer.SystemButtonState.originTouch.x;
- var touch_y = NRVirtualDisplayer.SystemButtonState.originTouch.y;
- var realTouchPos = new Vector3((touch_x + 1) * m_ScreenWidth * 0.5f, (touch_y + 1) * m_ScreenHeight * 0.5f, 0f);
- Vector3 touchpos = NRVirtualDisplayer.SystemButtonState.pressing ? realTouchPos : m_LastTouch;
- m_LastTouch = NRVirtualDisplayer.SystemButtonState.pressing ? touchpos : m_FarAwayPos;
- touchpos = m_UICamera.ScreenToWorldPoint(touchpos);
-
-
- if (Mouse)
- Mouse.transform.position = touchpos + m_UICamera.transform.forward * 0.3f;
- ray = new Ray(touchpos, m_UICamera.transform.forward);
- breakPoints.Add(touchpos);
- eventCamera.farClipPlane = eventCamera.nearClipPlane + distance;
- eventCamera.orthographicSize = m_UICamera.orthographicSize;
- eventCamera.aspect = m_UICamera.aspect;
- eventCamera.transform.position = ray.origin - (ray.direction * eventCamera.nearClipPlane);
- eventCamera.transform.rotation = Quaternion.LookRotation(ray.direction, transform.up);
- Raycast(ray, distance, sortedRaycastResults);
- firstHit = FirstRaycastResult();
- if (firstHit.isValid && firstHit.gameObject)
- {
- var button = firstHit.gameObject.GetComponent<NRButton>();
- if (button != null)
- {
- button.OnHover(firstHit);
- }
- }
- breakPoints.Add(firstHit.isValid ? firstHit.worldPosition : ray.GetPoint(distance));
- #if UNITY_EDITOR
- if (showDebugRay)
- Debug.DrawLine(breakPoints[0], breakPoints[1], firstHit.isValid ? Color.green : Color.red);
- #endif
- }
-
-
-
-
-
-
-
- public override void GraphicRaycast(ICanvasRaycastTarget raycastTarget, bool ignoreReversedGraphics, Ray ray, float distance, NRPointerRaycaster raycaster, List<RaycastResult> raycastResults)
- {
- if (raycastTarget.canvas == null) { return; }
- var eventCamera = raycaster.eventCamera;
- var screenCenterPoint = eventCamera.WorldToScreenPoint(eventCamera.transform.position);
- raycastTarget.GraphicRaycast(ignoreReversedGraphics, ray, distance, screenCenterPoint, raycaster, raycastResults);
- }
- }
-
- }
|