P
P
polyakovyevgeniy2017-03-19 17:48:14
Unity
polyakovyevgeniy, 2017-03-19 17:48:14

How to cast a beam on the user interface in Unity3D?

It is necessary to implement the ability, through throwing a beam, to determine the user interface object on which the user clicked. On the usual objects of the scene, the beam can be thrown and it determines which object it hit. How can this be done with the user interface? I know that there are handlers, but it is necessary to throw a beam, since the logic is the same for calculating the click object.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2017-03-19
@polyakovyevgeniy

PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
EventSystem.current.RaycastAll(eventDataCurrentPosition, raycastResults);

After that, raycastResults will contain where the raycast hit exactly in the UI. Well, replace mousePosition with whatever you need - with a wheelbarrow, for example.

D
Daniil Basmanov, 2017-03-19
@BasmanovDaniil

You can use EventSystem.currentSelectedGameObject to define the selected UI element .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question