diff options
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters')
10 files changed, 374 insertions, 0 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/BaseRaycaster.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/BaseRaycaster.cs new file mode 100644 index 0000000..d58c358 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/BaseRaycaster.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; + +namespace UnityEngine.EventSystems +{ + public abstract class BaseRaycaster : UIBehaviour + { + public abstract void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList); + public abstract Camera eventCamera { get; } + + [Obsolete("Please use sortOrderPriority and renderOrderPriority", false)] + public virtual int priority + { + get { return 0; } + } + + public virtual int sortOrderPriority + { + get { return int.MinValue; } + } + + public virtual int renderOrderPriority + { + get { return int.MinValue; } + } + + public override string ToString() + { + return "Name: " + gameObject + "\n" + + "eventCamera: " + eventCamera + "\n" + + "sortOrderPriority: " + sortOrderPriority + "\n" + + "renderOrderPriority: " + renderOrderPriority; + } + + // 只要是继承了BaseRaycaster的都会自动加到raycaster的列表里面 + protected override void OnEnable() + { + base.OnEnable(); + RaycasterManager.AddRaycaster(this); + } + + protected override void OnDisable() + { + RaycasterManager.RemoveRaycasters(this); + base.OnDisable(); + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/BaseRaycaster.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/BaseRaycaster.cs.meta new file mode 100644 index 0000000..eb8b89a --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/BaseRaycaster.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 89c5c6ccaa917c94a864388cc962e706 +timeCreated: 1602119379 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/Physics2DRaycaster.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/Physics2DRaycaster.cs new file mode 100644 index 0000000..6346938 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/Physics2DRaycaster.cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; +using UnityEngine.UI; + +namespace UnityEngine.EventSystems +{ + /// <summary> + /// Simple event system using physics raycasts. + /// </summary> + [AddComponentMenu("Event/Physics 2D Raycaster")] + [RequireComponent(typeof(Camera))] + public class Physics2DRaycaster : PhysicsRaycaster + { + protected Physics2DRaycaster() + {} + + // 发射线 + public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList) + { + if (eventCamera == null) + return; + + Ray ray; + float distanceToClipPlane; + ComputeRayAndDistance(eventData, out ray, out distanceToClipPlane); + + if (ReflectionMethodsCache.Singleton.getRayIntersectionAll == null) + return; + + var hits = ReflectionMethodsCache.Singleton.getRayIntersectionAll(ray, distanceToClipPlane, finalEventMask); + + if (hits.Length != 0) + { + for (int b = 0, bmax = hits.Length; b < bmax; ++b) + { + var sr = hits[b].collider.gameObject.GetComponent<SpriteRenderer>(); + + var result = new RaycastResult + { + gameObject = hits[b].collider.gameObject, + module = this, + distance = Vector3.Distance(eventCamera.transform.position, hits[b].point), + worldPosition = hits[b].point, + worldNormal = hits[b].normal, + screenPosition = eventData.position, + index = resultAppendList.Count, + sortingLayer = sr != null ? sr.sortingLayerID : 0, + sortingOrder = sr != null ? sr.sortingOrder : 0 + }; + resultAppendList.Add(result); + } + } + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/Physics2DRaycaster.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/Physics2DRaycaster.cs.meta new file mode 100644 index 0000000..828be37 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/Physics2DRaycaster.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 055a0cdd9d5d3e0489f854f7bdf22ded +timeCreated: 1602119377 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/PhysicsRaycaster.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/PhysicsRaycaster.cs new file mode 100644 index 0000000..c7d431c --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/PhysicsRaycaster.cs @@ -0,0 +1,117 @@ +using System.Collections.Generic; +using UnityEngine.UI; + +namespace UnityEngine.EventSystems +{ + //c 投射3D物体的相机挂这个脚本 + + /// <summary> + /// Simple event system using physics raycasts. + /// </summary> + [AddComponentMenu("Event/Physics Raycaster")] + [RequireComponent(typeof(Camera))] + public class PhysicsRaycaster : BaseRaycaster + { + /// <summary> + /// Const to use for clarity when no event mask is set + /// </summary> + protected const int kNoEventMaskSet = -1; + + protected Camera m_EventCamera; + + /// <summary> + /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used. + /// </summary> + [SerializeField] + protected LayerMask m_EventMask = kNoEventMaskSet; + + protected PhysicsRaycaster() + {} + + public override Camera eventCamera + { + get + { + if (m_EventCamera == null) + m_EventCamera = GetComponent<Camera>(); + return m_EventCamera ?? Camera.main; + } + } + + + /// <summary> + /// Depth used to determine the order of event processing. + /// </summary> + public virtual int depth + { + get { return (eventCamera != null) ? (int)eventCamera.depth : 0xFFFFFF; } + } + + /// <summary> + /// Event mask used to determine which objects will receive events. + /// </summary> + public int finalEventMask + { + get { return (eventCamera != null) ? eventCamera.cullingMask & m_EventMask : kNoEventMaskSet; } + } + + /// <summary> + /// Layer mask used to filter events. Always combined with the camera's culling mask if a camera is used. + /// </summary> + public LayerMask eventMask + { + get { return m_EventMask; } + set { m_EventMask = value; } + } + + protected void ComputeRayAndDistance(PointerEventData eventData, out Ray ray, out float distanceToClipPlane) + { + ray = eventCamera.ScreenPointToRay(eventData.position); + // compensate far plane distance - see MouseEvents.cs + float projectionDirection = ray.direction.z; // ray.direction是归一化了的 + distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection) + ? Mathf.Infinity + : Mathf.Abs((eventCamera.farClipPlane - eventCamera.nearClipPlane) / projectionDirection); + } + + public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList) + { + // Cull ray casts that are outside of the view rect. (case 636595) + if (eventCamera == null || !eventCamera.pixelRect.Contains(eventData.position)) + return; + + // 根据触摸数据拿到射线 + Ray ray; + float distanceToClipPlane; + ComputeRayAndDistance(eventData, out ray, out distanceToClipPlane); + + if (ReflectionMethodsCache.Singleton.raycast3DAll == null) + return; + + var hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, finalEventMask); + + if (hits.Length > 1) + System.Array.Sort(hits, (r1, r2) => r1.distance.CompareTo(r2.distance)); + + if (hits.Length != 0) + { + for (int b = 0, bmax = hits.Length; b < bmax; ++b) + { + var result = new RaycastResult + { + gameObject = hits[b].collider.gameObject, + module = this, + distance = hits[b].distance, + worldPosition = hits[b].point, + worldNormal = hits[b].normal, + screenPosition = eventData.position, + index = resultAppendList.Count, + sortingLayer = 0, + sortingOrder = 0 + }; + resultAppendList.Add(result); + } + } + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/PhysicsRaycaster.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/PhysicsRaycaster.cs.meta new file mode 100644 index 0000000..848c302 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/PhysicsRaycaster.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: b2bb18305a46ea44686ad8cdffbb3a58 +timeCreated: 1602119379 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycastResult.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycastResult.cs new file mode 100644 index 0000000..666ef8d --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycastResult.cs @@ -0,0 +1,65 @@ +namespace UnityEngine.EventSystems +{ + // UGUI射线检测结果 + public struct RaycastResult + { + private GameObject m_GameObject; // Game object hit by the raycast + + public GameObject gameObject + { + get { return m_GameObject; } + set { m_GameObject = value; } + } + + public BaseRaycaster module; // Event system that hit this object + public float distance; // The distance from the origin this hit was. + public float index; // The index this element is in the raycastList (used for sorting) + public int depth; + public int sortingLayer; + public int sortingOrder; + // World-space position where a ray cast into the screen hits something + public Vector3 worldPosition; + // World-space normal where a ray cast into the screen hits something + public Vector3 worldNormal; + + public Vector2 screenPosition; + + public bool isValid + { + get { return module != null && gameObject != null; } + } + + public void Clear() + { + gameObject = null; + module = null; + distance = 0; + index = 0; + depth = 0; + sortingLayer = 0; + sortingOrder = 0; + worldNormal = Vector3.up; + worldPosition = Vector3.zero; + screenPosition = Vector2.zero; + } + + public override string ToString() + { + if (!isValid) + return ""; + + return "Name: " + gameObject + "\n" + + "module: " + module + "\n" + + "distance: " + distance + "\n" + + "index: " + index + "\n" + + "depth: " + depth + "\n" + + "worldNormal: " + worldNormal + "\n" + + "worldPosition: " + worldPosition + "\n" + + "screenPosition: " + screenPosition + "\n" + + "module.sortOrderPriority: " + module.sortOrderPriority + "\n" + + "module.renderOrderPriority: " + module.renderOrderPriority + "\n" + + "sortingLayer: " + sortingLayer + "\n" + + "sortingOrder: " + sortingOrder; + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycastResult.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycastResult.cs.meta new file mode 100644 index 0000000..c2d01ba --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycastResult.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5f5a2c96e65809c43ad8a958f062ef1f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycasterManager.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycasterManager.cs new file mode 100644 index 0000000..c3480b4 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycasterManager.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; + +namespace UnityEngine.EventSystems +{ + internal static class RaycasterManager + { + private static readonly List<BaseRaycaster> s_Raycasters = new List<BaseRaycaster>(); + + public static void AddRaycaster(BaseRaycaster baseRaycaster) + { + if (s_Raycasters.Contains(baseRaycaster)) + return; + + s_Raycasters.Add(baseRaycaster); + } + + public static List<BaseRaycaster> GetRaycasters() + { + return s_Raycasters; + } + + public static void RemoveRaycasters(BaseRaycaster baseRaycaster) + { + if (!s_Raycasters.Contains(baseRaycaster)) + return; + s_Raycasters.Remove(baseRaycaster); + } + } +} diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycasterManager.cs.meta b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycasterManager.cs.meta new file mode 100644 index 0000000..594b8d2 --- /dev/null +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/Raycasters/RaycasterManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5c12b7bf7a4800945ba532802a36b165 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |