Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Ordered raycaster for casting against 3D Physics components.
/* Original code[1] Copyright (c) 2019 Shane Celis[2]
Licensed under the MIT License[3]
This comment generated by code-cite[4].
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
namespace UnityEngine.EventSystems {
/// <summary>
/// Simple event system using ordered physics raycasts.
/// </summary>
[AddComponentMenu("Event/Ordered Physics Raycaster")]
/// <summary>
/// Ordered raycaster for casting against 3D Physics components.
/// </summary>
public class OrderedPhysicsRaycaster : PhysicsRaycaster {
[Header("Set sorting order on raycasts.")]
public int sortingOrder = 1;
protected OrderedPhysicsRaycaster() { }
public override void Raycast(PointerEventData eventData, List<RaycastResult> resultAppendList) {
int initialCount = resultAppendList.Count;
base.Raycast(eventData, resultAppendList);
for (int i = initialCount; i < resultAppendList.Count; i++) {
// Do our struct dance do-si-do.
var raycastResult = resultAppendList[i];
raycastResult.sortingOrder = sortingOrder;
resultAppendList[i] = raycastResult;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment