Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Last active July 5, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecelis/7881b64ce07ee399f9c2928d977de8c6 to your computer and use it in GitHub Desktop.
Save shanecelis/7881b64ce07ee399f9c2928d977de8c6 to your computer and use it in GitHub Desktop.
Ordered raycaster for casting against 3D Physics components. https://twitter.com/shanecelis/status/1146822575978373120
/* Original code[1] Copyright (c) 2019 Shane Celis[2]
Licensed under the MIT License[3]
This comment generated by code-cite[4].
[1]: https://gist.github.com/shanecelis/7881b64ce07ee399f9c2928d977de8c6
[2]: https://github.com/shanecelis
[3]: https://opensource.org/licenses/MIT
[4]: https://github.com/shanecelis/code-cite
*/
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")]
[RequireComponent(typeof(Camera))]
/// <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