看起来很复杂,但可以做到。
1 .Route 来自PointerEventData
您要取消阻止 3D GameObjects 光线投射的所有 UI 组件中的数据。
2PhysicsRaycaster
.获取with的所有实例FindObjectsOfType<PhysicsRaycaster>()
。在性能方面,缓存它是有意义的。
3 .执行 RaycastPhysicsRaycaster.Raycast
将返回所有附有 Collider 的 GameObjects。
4.使用ExecuteEvents.Execute
将相应的事件发送到结果中存储RaycastResult
。
RaycastForwarder
脚本:
public class RaycastForwarder : MonoBehaviour
{
List<PhysicsRaycaster> rayCast3D = new List<PhysicsRaycaster>();
List<RaycastResult> rayCast3DResult = new List<RaycastResult>();
private static RaycastForwarder localInstance;
public static RaycastForwarder Instance { get { return localInstance; } }
private void Awake()
{
if (localInstance != null && localInstance != this)
{
Destroy(this.gameObject);
}
else
{
localInstance = this;
}
}
public void notifyPointerDown(PointerEventData eventData)
{
findColliders(eventData, PointerEventType.Down);
}
public void notifyPointerUp(PointerEventData eventData)
{
findColliders(eventData, PointerEventType.Up);
}
public void notifyPointerDrag(PointerEventData eventData)
{
findColliders(eventData, PointerEventType.Drag);
}
private void findColliders(PointerEventData eventData, PointerEventType evType)
{
UpdateRaycaster();
//Loop Through All Normal Collider(3D/Mesh Renderer) and throw Raycast to each one
for (int i = 0; i < rayCast3D.Count; i++)
{
//Send Raycast to all GameObject with 3D Collider
rayCast3D[i].Raycast(eventData, rayCast3DResult);
sendRayCast(eventData, evType);
}
//Reset Result
rayCast3DResult.Clear();
}
private void sendRayCast(PointerEventData eventData, PointerEventType evType)
{
//Loop over the RaycastResult and simulate the pointer event
for (int i = 0; i < rayCast3DResult.Count; i++)
{
GameObject target = rayCast3DResult[i].gameObject;
PointerEventData evData = createEventData(rayCast3DResult[i]);
if (evType == PointerEventType.Drag)
{
ExecuteEvents.Execute<IDragHandler>(target,
evData,
ExecuteEvents.dragHandler);
}
if (evType == PointerEventType.Down)
{
ExecuteEvents.Execute<IPointerDownHandler>(target,
evData,
ExecuteEvents.pointerDownHandler);
}
if (evType == PointerEventType.Up)
{
ExecuteEvents.Execute<IPointerUpHandler>(target,
evData,
ExecuteEvents.pointerUpHandler);
}
}
}
private PointerEventData createEventData(RaycastResult rayResult)
{
PointerEventData evData = new PointerEventData(EventSystem.current);
evData.pointerCurrentRaycast = rayResult;
return evData;
}
//Get all PhysicsRaycaster in the scene
private void UpdateRaycaster()
{
convertToList(FindObjectsOfType<PhysicsRaycaster>(), rayCast3D);
}
private void convertToList(PhysicsRaycaster[] fromComponent, List<PhysicsRaycaster> toComponent)
{
//Clear and copy new Data
toComponent.Clear();
for (int i = 0; i < fromComponent.Length; i++)
{
toComponent.Add(fromComponent[i]);
}
}
public enum PointerEventType
{
Drag, Down, Up
}
}
RayCastRouter
脚本:
public class RayCastRouter : MonoBehaviour, IPointerDownHandler,
IPointerUpHandler,
IDragHandler
{
public void OnDrag(PointerEventData eventData)
{
RaycastForwarder.Instance.notifyPointerDrag(eventData);
}
public void OnPointerDown(PointerEventData eventData)
{
RaycastForwarder.Instance.notifyPointerDown(eventData);
}
public void OnPointerUp(PointerEventData eventData)
{
RaycastForwarder.Instance.notifyPointerUp(eventData);
}
}
用法:
A .AttachRaycastForwarder
到一个空的游戏对象。
B . 附加RayCastRouter
到您不想阻止 3D 游戏对象的任何 UI 组件,例如 Image。就是这样。任何附加到它的 UI 组件RayCastRouter
都将能够允许其后面的 3D 游戏对象接收光线投射。
现在将向 3D 对象发送一个事件,该对象具有一个脚本,该脚本实现来自和IPointerDownHandler
接口的函数。IPointerUpHandler
IDragHandler
不要忘记连接Physics Raycaster
到相机。