目的:
不对canvas内部ui进行排序,直接拿最上层可点击UI
不检测屏幕外的UI
拖拽UI不进入检测点击事件
使用 : 把下文代码,直接新建个脚本,把canvas上的 Graphic Raycaster 替换为 M_GraphicRaycaster ,即可达到优化效果
using System;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
namespace UnityEngine.UI
{
[AddComponentMenu("Event/M Graphic Raycaster")]
[RequireComponent(typeof(Canvas))]
public class M_GraphicRaycaster : BaseRaycaster
{
protected const int kNoEventMaskSet = -1;
/// <summary>
/// Type of raycasters to check against to check for canvas blocking elements.
/// </summary>
public enum BlockingObjects
{
/// <summary>
/// Perform no raycasts.
/// </summary>
None = 0,
/// <summary>
/// Perform a 2D raycast check to check for blocking 2D elements
/// </summary>
TwoD = 1,
/// <summary>
/// Perform a 3D raycast check to check for blocking 3D elements
/// </summary>
ThreeD = 2,
/// <summary>
/// Perform a 2D and a 3D raycasts to check for blocking 2D and 3D elements.
/// </summary>
All = 3,
}
/// <summary>
/// Priority of the raycaster based upon sort order.
/// </summary>
/// <returns>
/// The sortOrder priority.
/// </returns>
public override int sortOrderPriority
{
get
{
// We need to return the sorting order here as distance will all be 0 for overlay.
if (canvas.renderMode == RenderMode.ScreenSpaceOverlay)
return canvas.sortingOrder;
return base.sortOrderPriority;
}
}
/// <summary>
/// Priority of the raycaster based upon render order.
/// </summary>
/// <returns>
/// The renderOrder priority.
/// </returns>
public override int renderOrderPriority
{
get
{
// We need to return the sorting order here as distance will all be 0 for overlay.
if (canvas.renderMode == RenderMode.ScreenSpaceOverlay)
return canvas.rootCanvas.renderOrder;
return base.renderOrderPriority;
}
}
[FormerlySerializedAs("ignoreReversedGraphics")]
[SerializeField]
private bool m_IgnoreReversedGraphics = true;
[FormerlySerializedAs("blockingObjects")]
[SerializeField]
private BlockingObjects m_BlockingObjects = BlockingObjects.None;
/// <summary>
/// Whether Graphics facing away from the raycaster are checked for raycasts.
/// </summary>
public bool ignoreReversedGraphics { get { return m_IgnoreReversedGraphics; } set { m_IgnoreReversedGraphics = value; } }
/// <summary>
/// The type of objects that are checked to determine if they block graphic raycasts.
/// </summary>
public BlockingObjects blockingObjects { get { return m_BlockingObjects; } set { m_BlockingObjects = value; } }
[SerializeField]
protected LayerMask m_BlockingMask = kNoEventMaskSet;
private Canvas m_Canvas;
protected M_GraphicRaycaster()
{ }
protected virtual Canvas canvas
{
get
{
if (m_Canvas != null)
return m_Canvas;
m_Canvas = GetComponent<Canvas>();
return m_Canvas;
}
}
/// <summary>
/// The camera that will generate rays for this raycaster.
/// </summary>
/// <returns>
/// - Null if Camera mode is ScreenSpaceOverlay or ScreenSpaceCamera and has no camera.
/// - canvas.worldCanvas if not null
/// - Camera.main.
/// </returns>
public override Camera eventCamera
{
get
{
if (canvas.renderMode == RenderMode.ScreenSpaceOverlay || (canvas.renderMode == RenderMode.ScreenSpaceCamera && canvas.worldCamera == null))
return null;
return canvas.worldCamera != null ? canvas.worldCamera : Camera.main;
}
}
#region
[NonSerialized] private List<Graphic> m_RaycastResults = new List<Graphic>();
/// <summary>
/// Perform the raycast against the list of graphics associated with the Canvas.
/// </summary>
/// <param name="eventData">Current event data</param>
/// <param name="resultAppendList">List of hit objects to append new results to.</param>
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList)
{
if (eventData.dragging) return; //不会触发OnPointerIn/OnPointerOut
if (canvas == null)
return;
if (IsOutScreen) return;
var canvasGraphics = GraphicRegistry.GetGraphicsForCanvas(canvas);
if (canvasGraphics == null || canvasGraphics.Count == 0)
return;
int displayIndex;
var currentEventCamera = eventCamera; // Propery can call Camera.main, so cache the reference
if (canvas.renderMode == RenderMode.ScreenSpaceOverlay || currentEventCamera == null)
displayIndex = canvas.targetDisplay;
else
displayIndex = currentEventCamera.targetDisplay;
var eventPosition = Display.RelativeMouseAt(eventData.position);
if (eventPosition != Vector3.zero)
{
int eventDisplayIndex = (int)eventPosition.z;
if (eventDisplayIndex != displayIndex)
return;
}
else
{
eventPosition = eventData.position;
}
// Convert to view space
Vector2 pos;
if (currentEventCamera == null)
{
float w = Screen.width;
float h = Screen.height;
if (displayIndex > 0 && displayIndex < Display.displays.Length)
{
w = Display.displays[displayIndex].systemWidth;
h = Display.displays[displayIndex].systemHeight;
}
pos = new Vector2(eventPosition.x / w, eventPosition.y / h);
}
else
pos = currentEventCamera.ScreenToViewportPoint(eventPosition);
if (pos.x < 0f || pos.x > 1f || pos.y < 0f || pos.y > 1f)
return;
float hitDistance = float.MaxValue;
Ray ray = new Ray();
if (currentEventCamera != null)
ray = currentEventCamera.ScreenPointToRay(eventPosition);
if (canvas.renderMode != RenderMode.ScreenSpaceOverlay && blockingObjects != BlockingObjects.None)
{
float distanceToClipPlane = 100.0f;
if (currentEventCamera != null)
{
float projectionDirection = ray.direction.z;
distanceToClipPlane = Mathf.Approximately(0.0f, projectionDirection)
? Mathf.Infinity
: Mathf.Abs((currentEventCamera.farClipPlane - currentEventCamera.nearClipPlane) / projectionDirection);
}
}
m_RaycastResults.Clear();
Raycast(canvas, currentEventCamera, eventPosition, canvasGraphics, m_RaycastResults);
int totalCount = m_RaycastResults.Count;
for (var index = 0; index < totalCount; index++)
{
var go = m_RaycastResults[index].gameObject;
bool appendGraphic = true;
if (ignoreReversedGraphics)
{
if (currentEventCamera == null)
{
var dir = go.transform.rotation * Vector3.forward;
appendGraphic = Vector3.Dot(Vector3.forward, dir) > 0;
}
else
{
var cameraFoward = currentEventCamera.transform.rotation * Vector3.forward;
var dir = go.transform.rotation * Vector3.forward;
appendGraphic = Vector3.Dot(cameraFoward, dir) > 0;
}
}
if (appendGraphic)
{
float distance = 0;
if (currentEventCamera == null || canvas.renderMode == RenderMode.ScreenSpaceOverlay)
distance = 0;
else
{
Transform trans = go.transform;
Vector3 transForward = trans.forward;
distance = (Vector3.Dot(transForward, trans.position - currentEventCamera.transform.position) / Vector3.Dot(transForward, ray.direction));
if (distance < 0)
continue;
}
if (distance >= hitDistance)
continue;
var castResult = new RaycastResult
{
gameObject = go,
module = this,
distance = distance,
screenPosition = eventPosition,
index = resultAppendList.Count,
depth = m_RaycastResults[index].depth,
sortingLayer = canvas.sortingLayerID,
sortingOrder = canvas.sortingOrder
};
resultAppendList.Add(castResult);
}
}
}
/// <summary>
/// Perform a raycast into the screen and collect all graphics underneath it.
/// </summary>
[NonSerialized] static readonly List<Graphic> s_SortedGraphics = new List<Graphic>();
private static void Raycast(Canvas canvas, Camera eventCamera, Vector2 pointerPosition, IList<Graphic> foundGraphics, List<Graphic> results)
{
int totalCount = foundGraphics.Count;
Graphic upGraphic = null;
int upIndex = -1;
for (int i = 0; i < totalCount; ++i)
{
Graphic graphic = foundGraphics[i];
int depth = graphic.depth;
if (depth == -1 || !graphic.raycastTarget || graphic.canvasRenderer.cull)
continue;
if (!RectTransformUtility.RectangleContainsScreenPoint(graphic.rectTransform, pointerPosition, eventCamera))
continue;
if (eventCamera != null && eventCamera.WorldToScreenPoint(graphic.rectTransform.position).z > eventCamera.farClipPlane)
continue;
if (graphic.Raycast(pointerPosition, eventCamera))
{
s_SortedGraphics.Add(graphic);
if (depth > upIndex)
{
upIndex = depth;
upGraphic = graphic;
}
}
}
if (upGraphic != null)
results.Add(upGraphic);
}
/// <summary>
/// 上一次检查的位置;
/// </summary>
Vector3 mLastCheckPostion = Vector3.zero;
private bool _IsOutScreen = false;
/// <summary>
/// 是否在频幕外;
/// </summary>
private bool IsOutScreen
{
get
{
var curPos = transform.position;
if (curPos.x != mLastCheckPostion.x || curPos.y != mLastCheckPostion.y || curPos.z != mLastCheckPostion.z)
{
mLastCheckPostion = curPos;
CheckOutScreenMethod(curPos);
}
return _IsOutScreen;
}
}
/// <summary>
/// 是否在屏幕外的具体检测方法;
/// </summary>
private void CheckOutScreenMethod(Vector3 curPos)
{
bool isOut = false;
var pos = eventCamera.WorldToViewportPoint(curPos);
if (pos.x < 0 || pos.y < 0 || pos.x > 1 || pos.y > 1)
isOut = true;
_IsOutScreen = isOut;
}
#endregion
}
}