Can we make s_RaycastComparer public or protected in EventSystem?
Right now, for 2.5D games, EventSystem doesn’t sort the raycast results to respect
the transparency sort mode.
Right now I can either change the z-position of my objects (doesn’t make much sense in a 2D game),
or I have to do reflection black magic:
void Awake()
{
// Set Raycast Comparer with Black Magic
typeof(EventSystem)
.GetField("s_RaycastComparer", BindingFlags.Static | BindingFlags.NonPublic)
.SetValue(eventSystem, (Comparison<RaycastResult>)CustomRaycastComparer);
}
private static int CustomRaycastComparer(RaycastResult lhs, RaycastResult rhs)
{
if (lhs.module != rhs.module)
{
var lhsEventCamera = lhs.module.eventCamera;
var rhsEventCamera = rhs.module.eventCamera;
if (lhsEventCamera != null && rhsEventCamera != null && lhsEventCamera.depth != rhsEventCamera.depth)
{
// need to reverse the standard compareTo
if (lhsEventCamera.depth < rhsEventCamera.depth)
return 1;
if (lhsEventCamera.depth == rhsEventCamera.depth)
return 0;
return -1;
}
if (lhs.module.sortOrderPriority != rhs.module.sortOrderPriority)
return rhs.module.sortOrderPriority.CompareTo(lhs.module.sortOrderPriority);
if (lhs.module.renderOrderPriority != rhs.module.renderOrderPriority)
return rhs.module.renderOrderPriority.CompareTo(lhs.module.renderOrderPriority);
}
if (lhs.sortingLayer != rhs.sortingLayer)
{
// Uses the layer value to properly compare the relative order of the layers.
var rid = SortingLayer.GetLayerValueFromID(rhs.sortingLayer);
var lid = SortingLayer.GetLayerValueFromID(lhs.sortingLayer);
return rid.CompareTo(lid);
}
if (lhs.sortingOrder != rhs.sortingOrder)
return rhs.sortingOrder.CompareTo(lhs.sortingOrder);
// Transparency Sort Order - START
if (lhs.gameObject.layer != UILayer && rhs.gameObject.layer != UILayer)
return lhs.gameObject.transform.position.y.CompareTo(rhs.gameObject.transform.position.y);
// Transparency Sort Order - END
// comparing depth only makes sense if the two raycast results have the same root canvas (case 912396)
if (lhs.depth != rhs.depth && lhs.module.rootRaycaster == rhs.module.rootRaycaster)
return rhs.depth.CompareTo(lhs.depth);
if (lhs.distance != rhs.distance)
return lhs.distance.CompareTo(rhs.distance);
return lhs.index.CompareTo(rhs.index);
}
If there’s a better forum for requesting features, please let me know, but don’t just delete my post without explaining to me how I’m supposed to make these requests.