简体   繁体   English

如何防止某些对象接收光线投射?

[英]How do I prevent certain objects from receiving the raycast?

I'm using the following code to perform a raycast and detect the GameObjects that are hit:我正在使用以下代码执行光线投射并检测被击中的游戏对象:

var eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = screenPosition;
var results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);

However, I want only certain objects to be hit by the EventSystem.current.RaycastAll .但是,我只希望EventSystem.current.RaycastAll击中某些对象。 Is there any way to allow only certain GameObjects to receive the raycast?有没有办法只允许某些游戏对象接收光线投射?

You can try adding a CanvasGroup component to the UI elements you don't want to hit with the raycast.您可以尝试将 CanvasGroup 组件添加到您不想被光线投射击中的 UI 元素。 CanvasGroup has the option "block raycasts". CanvasGroup 具有“阻止光线投射”选项。 Just set it to false and it should do the job.只需将其设置为 false 即可完成工作。

Two ways, one you can separate each UI categories to different Canvases and raycast from different graphicRaycaster component every time your need changes.两种方式,一种是您可以在每次需要更改时将每个 UI 类别分离到不同的画布和来自不同 graphicsRaycaster 组件的光线投射。

Or, I think the graphicRaycaster and EventSystem.current.RaycastAl both return an array of all UI objects hit, so why not just filter out manually by script based on their tags or so.或者,我认为 graphicsRaycaster 和 EventSystem.current.RaycastAl 都返回所有命中的 UI 对象的数组,那么为什么不根据它们的标签左右通过脚本手动过滤掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM