简体   繁体   中英

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 . 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 has the option "block raycasts". Just set it to false and it should do the job.

Two ways, one you can separate each UI categories to different Canvases and raycast from different graphicRaycaster component every time your need changes.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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