Ordered raycaster for casting against 3D Physics components. https://twitter.com/shanecelis/status/1146822575978373120
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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