How to implement the interaction of the cursor with the 'UI' components, the selection of which occurs by their 'Tag'?

Tried to implement it like this:

Ray _ray = MyCamera.ScreenPointToRay(Input.mousePosition); if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) Debug.Log("Complete!"); 

Result: The cursor responds to all UI components with a Boolean 'Raycast Target' which is positive.

  • one
    And you are not satisfied with the banal EventTrigger for the elements, which the UI button has by default, and just add the rest? and no beam is needed - Alexey Shimansky
  • one
    And also, it seems that the standard OnMouseOver/OnMouseEnter/OnMouseDown etc. events should work. docs.unity3d.com/ScriptReference/MonoBehaviour.html - Alexey Shimansky

1 answer 1

What is the meaning of the beam in this code? If it is to determine with which object the interaction takes place with it, then:

 Ray _ray = MyCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(_ray, out hit)) { if (hit.gameObject.tag == "something") { if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) Debug.Log("Complete!"); } } 

Not very cool, but was that the meaning of the beam?

UPD: Then you can leave it through the beam like this:

 Ray _ray = MyCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(_ray, out hit) && hit.gameObject.tag == "something") { //if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) более не требуется. Debug.Log("Complete!"); } 

But in my opinion it is better to avoid using the beam even with simple events, or EventTrigger, as suggested in the comments, or somehow.

  • You absolutely correctly defined the meaning of the implementation of the beam in this context. If there is a way that can more meaningfully and logically implement the result I need, I want to use it with your help, perhaps, an explanation. - Solvent
  • If we already use the beam, then we can not work with the event system at all, because with the help of the beam there is already an object checked for the tag and you can proceed to action. What part of the life cycle is this code in? - DanielOlivo