Quiero poder hacer una matriz, que tenga todos los elementos de la interfaz de usuario, debajo del cursor, dentro de la matriz. Esto se debe a que necesito poder saber si hay un elemento de la interfaz de usuario debajo de varios otros elementos de la interfaz de usuario. Probé otras técnicas, pero solo muestra el elemento de la interfaz de usuario en la parte superior.
Editar: para ser más claro, estoy tratando de hacer una lista de todas las piezas de la interfaz de usuario que están en capas debajo del cursor, y no solo puedo decir qué interfaz de usuario está en la parte superior.
Parece que está buscando UI.GraphicRaycaster.Raycast .
El GraphicRaycaster generalmente se adjunta a cada Canvas (de lo contrario, no podría interactuar con nada en ellos).
Entonces, sin hacer referencia a un lienzo específico, puede ir y obtenerlos todos:
// Because this is dynamically changing I recommend to stick to a List instead of an array private List<RaycastResult> _hits = new List<RaycastResult>(); private EventSystem _eventSystem; private void Awake() { //Fetch the Event System from the Scene _eventSystem = FindObjectOfType<EventSystem>(); } private void Update() { //Check if the left Mouse button is clicked if (Input.GetKeyDown(KeyCode.Mouse0)) { //Set up the new Pointer Event var eventData = new PointerEventData(_eventSystem) {position = Input.mousePosition}; //Create a list of Raycast Results _hits.Clear(); // Get all existing graphicsRaycaster // NOTE: Of course you should rather cache this result eg in Awake // and only update it if a Canvas is added to or removed from the scene var graphicsRaycasters = FindObjectsOfType<GraphicRaycaster>(); foreach(var graphicsRaycaster in graphicsRaycasters) { graphicsRaycaster.Raycast(eventData, _hits); } // Do something with the hits foreach (var result in _hits) { Debug.Log(result.gameObject.name, result.gameObject); } } }La superposición del espacio de la pantalla del modo de procesamiento siempre mostrará la interfaz de usuario al frente. Una forma de lograr un objeto de juego frente a la interfaz de usuario es cambiar el modo de representación de la cámara a la cámara del espacio de la pantalla para que pueda usar sprite con una posición de valor z negativo más alta frente a la interfaz de usuario.
o puede usar GraphicRaycaster para hacer una lista de información sobre herramientas de raycast por nombre:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; using System.Linq; public class UI : MonoBehaviour { private GraphicRaycaster rc; private PointerEventData pt; EventSystem eventSystem; public TMP_Text text; public List<RaycastResult> results; private void Start() { //initialize rc = GetComponent<GraphicRaycaster>(); eventSystem = GetComponent<EventSystem>(); pt = new PointerEventData(eventSystem); results = new List<RaycastResult>(); } private void Update() { //get screen mouse position pt.position = Input.mousePosition; results.Clear(); rc.Raycast(pt, results); if (results.Count > 0) { //enable tooltip text.gameObject.SetActive(true); text.transform.position = pt.position; //reset text if results.count change text.text = ""; foreach (RaycastResult res in results) { text.text += res.gameObject.name + "\n"; } } else { text.gameObject.SetActive(false); text.text = ""; } } } hacer text mesh pro anclado en la parte superior izquierda 