Initial Commit
This commit is contained in:
parent
53eb92e9af
commit
270ab7d11f
15341 changed files with 700234 additions and 0 deletions
|
@ -0,0 +1,101 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.EventSystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for any RayCaster.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A Raycaster is responsible for raycasting against scene elements to determine if the cursor is over them. Default Raycasters include PhysicsRaycaster, Physics2DRaycaster, GraphicRaycaster.
|
||||
/// Custom raycasters can be added by extending this class.
|
||||
/// </remarks>
|
||||
public abstract class BaseRaycaster : UIBehaviour
|
||||
{
|
||||
private BaseRaycaster m_RootRaycaster;
|
||||
|
||||
/// <summary>
|
||||
/// Raycast against the scene.
|
||||
/// </summary>
|
||||
/// <param name="eventData">Current event data.</param>
|
||||
/// <param name="resultAppendList">List of hit Objects.</param>
|
||||
public abstract void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList);
|
||||
|
||||
/// <summary>
|
||||
/// The camera that will generate rays for this raycaster.
|
||||
/// </summary>
|
||||
public abstract Camera eventCamera { get; }
|
||||
|
||||
[Obsolete("Please use sortOrderPriority and renderOrderPriority", false)]
|
||||
public virtual int priority
|
||||
{
|
||||
get { return 0; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Priority of the raycaster based upon sort order.
|
||||
/// </summary>
|
||||
public virtual int sortOrderPriority
|
||||
{
|
||||
get { return int.MinValue; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Priority of the raycaster based upon render order.
|
||||
/// </summary>
|
||||
public virtual int renderOrderPriority
|
||||
{
|
||||
get { return int.MinValue; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raycaster on root canvas
|
||||
/// </summary>
|
||||
public BaseRaycaster rootRaycaster
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_RootRaycaster == null)
|
||||
{
|
||||
var baseRaycasters = GetComponentsInParent<BaseRaycaster>();
|
||||
if (baseRaycasters.Length != 0)
|
||||
m_RootRaycaster = baseRaycasters[baseRaycasters.Length - 1];
|
||||
}
|
||||
|
||||
return m_RootRaycaster;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "Name: " + gameObject + "\n" +
|
||||
"eventCamera: " + eventCamera + "\n" +
|
||||
"sortOrderPriority: " + sortOrderPriority + "\n" +
|
||||
"renderOrderPriority: " + renderOrderPriority;
|
||||
}
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
RaycasterManager.AddRaycaster(this);
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
RaycasterManager.RemoveRaycasters(this);
|
||||
base.OnDisable();
|
||||
}
|
||||
|
||||
protected override void OnCanvasHierarchyChanged()
|
||||
{
|
||||
base.OnCanvasHierarchyChanged();
|
||||
m_RootRaycaster = null;
|
||||
}
|
||||
|
||||
protected override void OnTransformParentChanged()
|
||||
{
|
||||
base.OnTransformParentChanged();
|
||||
m_RootRaycaster = null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 426106349a0ff964fa4e7178c1d3a4f5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,107 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine.UI;
|
||||
|
||||
#if PACKAGE_TILEMAP
|
||||
using UnityEngine.Tilemaps;
|
||||
#endif
|
||||
using UnityEngine.U2D;
|
||||
|
||||
namespace UnityEngine.EventSystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple event system using physics raycasts.
|
||||
/// </summary>
|
||||
[AddComponentMenu("Event/Physics 2D Raycaster")]
|
||||
[RequireComponent(typeof(Camera))]
|
||||
/// <summary>
|
||||
/// Raycaster for casting against 2D Physics components.
|
||||
/// </summary>
|
||||
public class Physics2DRaycaster : PhysicsRaycaster
|
||||
{
|
||||
#if PACKAGE_PHYSICS2D
|
||||
RaycastHit2D[] m_Hits;
|
||||
#endif
|
||||
|
||||
protected Physics2DRaycaster()
|
||||
{}
|
||||
|
||||
/// <summary>
|
||||
/// Raycast against 2D elements in the scene.
|
||||
/// </summary>
|
||||
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
|
||||
{
|
||||
#if PACKAGE_PHYSICS2D
|
||||
Ray ray = new Ray();
|
||||
float distanceToClipPlane = 0;
|
||||
int displayIndex = 0;
|
||||
if (!ComputeRayAndDistance(eventData, ref ray, ref displayIndex, ref distanceToClipPlane))
|
||||
return;
|
||||
|
||||
int hitCount = 0;
|
||||
|
||||
if (maxRayIntersections == 0)
|
||||
{
|
||||
if (ReflectionMethodsCache.Singleton.getRayIntersectionAll == null)
|
||||
return;
|
||||
m_Hits = ReflectionMethodsCache.Singleton.getRayIntersectionAll(ray, distanceToClipPlane, finalEventMask);
|
||||
hitCount = m_Hits.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ReflectionMethodsCache.Singleton.getRayIntersectionAllNonAlloc == null)
|
||||
return;
|
||||
|
||||
if (m_LastMaxRayIntersections != m_MaxRayIntersections)
|
||||
{
|
||||
m_Hits = new RaycastHit2D[maxRayIntersections];
|
||||
m_LastMaxRayIntersections = m_MaxRayIntersections;
|
||||
}
|
||||
|
||||
hitCount = ReflectionMethodsCache.Singleton.getRayIntersectionAllNonAlloc(ray, m_Hits, distanceToClipPlane, finalEventMask);
|
||||
}
|
||||
|
||||
if (hitCount != 0)
|
||||
{
|
||||
for (int b = 0, bmax = hitCount; b < bmax; ++b)
|
||||
{
|
||||
Renderer r2d = null;
|
||||
// Case 1198442: Check for 2D renderers when filling in RaycastResults
|
||||
var rendererResult = m_Hits[b].collider.gameObject.GetComponent<Renderer>();
|
||||
if (rendererResult != null)
|
||||
{
|
||||
if (rendererResult is SpriteRenderer)
|
||||
{
|
||||
r2d = rendererResult;
|
||||
}
|
||||
#if PACKAGE_TILEMAP
|
||||
if (rendererResult is TilemapRenderer)
|
||||
{
|
||||
r2d = rendererResult;
|
||||
}
|
||||
#endif
|
||||
if (rendererResult is SpriteShapeRenderer)
|
||||
{
|
||||
r2d = rendererResult;
|
||||
}
|
||||
}
|
||||
|
||||
var result = new RaycastResult
|
||||
{
|
||||
gameObject = m_Hits[b].collider.gameObject,
|
||||
module = this,
|
||||
distance = Vector3.Distance(eventCamera.transform.position, m_Hits[b].point),
|
||||
worldPosition = m_Hits[b].point,
|
||||
worldNormal = m_Hits[b].normal,
|
||||
screenPosition = eventData.position,
|
||||
displayIndex = displayIndex,
|
||||
index = resultAppendList.Count,
|
||||
sortingLayer = r2d != null ? r2d.sortingLayerID : 0,
|
||||
sortingOrder = r2d != null ? r2d.sortingOrder : 0
|
||||
};
|
||||
resultAppendList.Add(result);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 56666c5a40171f54783dd416a44f42bf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,203 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UnityEngine.EventSystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple event system using physics raycasts.
|
||||
/// </summary>
|
||||
[AddComponentMenu("Event/Physics Raycaster")]
|
||||
[RequireComponent(typeof(Camera))]
|
||||
/// <summary>
|
||||
/// Raycaster for casting against 3D Physics components.
|
||||
/// </summary>
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// The max number of intersections allowed. 0 = allocating version anything else is non alloc.
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
protected int m_MaxRayIntersections = 0;
|
||||
protected int m_LastMaxRayIntersections = 0;
|
||||
|
||||
#if PACKAGE_PHYSICS
|
||||
RaycastHit[] m_Hits;
|
||||
#endif
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Max number of ray intersection allowed to be found.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A value of zero will represent using the allocating version of the raycast function where as any other value will use the non allocating version.
|
||||
/// </remarks>
|
||||
public int maxRayIntersections
|
||||
{
|
||||
get { return m_MaxRayIntersections; }
|
||||
set { m_MaxRayIntersections = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a ray going from camera through the event position and the distance between the near and far clipping planes along that ray.
|
||||
/// </summary>
|
||||
/// <param name="eventData">The pointer event for which we will cast a ray.</param>
|
||||
/// <param name="ray">The ray to use.</param>
|
||||
/// <param name="eventDisplayIndex">The display index used.</param>
|
||||
/// <param name="distanceToClipPlane">The distance between the near and far clipping planes along the ray.</param>
|
||||
/// <returns>True if the operation was successful. false if it was not possible to compute, such as the eventPosition being outside of the view.</returns>
|
||||
protected bool ComputeRayAndDistance(PointerEventData eventData, ref Ray ray, ref int eventDisplayIndex, ref float distanceToClipPlane)
|
||||
{
|
||||
if (eventCamera == null)
|
||||
return false;
|
||||
|
||||
var eventPosition = Display.RelativeMouseAt(eventData.position);
|
||||
if (eventPosition != Vector3.zero)
|
||||
{
|
||||
// We support multiple display and display identification based on event position.
|
||||
eventDisplayIndex = (int)eventPosition.z;
|
||||
|
||||
// Discard events that are not part of this display so the user does not interact with multiple displays at once.
|
||||
if (eventDisplayIndex != eventCamera.targetDisplay)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// The multiple display system is not supported on all platforms, when it is not supported the returned position
|
||||
// will be all zeros so when the returned index is 0 we will default to the event data to be safe.
|
||||
eventPosition = eventData.position;
|
||||
}
|
||||
|
||||
// Cull ray casts that are outside of the view rect. (case 636595)
|
||||
if (!eventCamera.pixelRect.Contains(eventPosition))
|
||||
return false;
|
||||
|
||||
ray = eventCamera.ScreenPointToRay(eventPosition);
|
||||
// compensate far plane distance - see MouseEvents.cs
|
||||
float projectionDirection = ray.direction.z;
|
||||
distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection)
|
||||
? Mathf.Infinity
|
||||
: Mathf.Abs((eventCamera.farClipPlane - eventCamera.nearClipPlane) / projectionDirection);
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
|
||||
{
|
||||
#if PACKAGE_PHYSICS
|
||||
Ray ray = new Ray();
|
||||
int displayIndex = 0;
|
||||
float distanceToClipPlane = 0;
|
||||
if (!ComputeRayAndDistance(eventData, ref ray, ref displayIndex, ref distanceToClipPlane))
|
||||
return;
|
||||
|
||||
int hitCount = 0;
|
||||
|
||||
if (m_MaxRayIntersections == 0)
|
||||
{
|
||||
if (ReflectionMethodsCache.Singleton.raycast3DAll == null)
|
||||
return;
|
||||
|
||||
m_Hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, finalEventMask);
|
||||
hitCount = m_Hits.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ReflectionMethodsCache.Singleton.getRaycastNonAlloc == null)
|
||||
return;
|
||||
if (m_LastMaxRayIntersections != m_MaxRayIntersections)
|
||||
{
|
||||
m_Hits = new RaycastHit[m_MaxRayIntersections];
|
||||
m_LastMaxRayIntersections = m_MaxRayIntersections;
|
||||
}
|
||||
|
||||
hitCount = ReflectionMethodsCache.Singleton.getRaycastNonAlloc(ray, m_Hits, distanceToClipPlane, finalEventMask);
|
||||
}
|
||||
|
||||
if (hitCount != 0)
|
||||
{
|
||||
if (hitCount > 1)
|
||||
System.Array.Sort(m_Hits, 0, hitCount, RaycastHitComparer.instance);
|
||||
|
||||
for (int b = 0, bmax = hitCount; b < bmax; ++b)
|
||||
{
|
||||
var result = new RaycastResult
|
||||
{
|
||||
gameObject = m_Hits[b].collider.gameObject,
|
||||
module = this,
|
||||
distance = m_Hits[b].distance,
|
||||
worldPosition = m_Hits[b].point,
|
||||
worldNormal = m_Hits[b].normal,
|
||||
screenPosition = eventData.position,
|
||||
displayIndex = displayIndex,
|
||||
index = resultAppendList.Count,
|
||||
sortingLayer = 0,
|
||||
sortingOrder = 0
|
||||
};
|
||||
resultAppendList.Add(result);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if PACKAGE_PHYSICS
|
||||
private class RaycastHitComparer : IComparer<RaycastHit>
|
||||
{
|
||||
public static RaycastHitComparer instance = new RaycastHitComparer();
|
||||
public int Compare(RaycastHit x, RaycastHit y)
|
||||
{
|
||||
return x.distance.CompareTo(y.distance);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c49b4cc203aa6414fae5c798d1d0e7d6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue