using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class PlaceItem : MonoBehaviour { #region PRIVATE VARIABLES // Reference to canvas graphic raycaster private GraphicRaycaster graphicRaycaster; // Reference to pointer event data private PointerEventData pointerEventData; // Reference to event system in scene private EventSystem eventSystem; // Reference to map private GameObject map; #endregion #region MONOBEHAVIOUR CALLBACKS private void Start() { // Find the Raycaster from the GameObject (the Canvas) graphicRaycaster = FindObjectOfType(); // Find the Event System from the Scene eventSystem = FindObjectOfType(); // Find the map game object in the scene map = GameObject.Find("Map"); } private void Update() { if (Input.GetMouseButtonDown(0)) { PlaceItemOnMap(); } } #endregion #region PLACE ITEM ON MAP private void PlaceItemOnMap() { // Set up the new Pointer Event pointerEventData = new PointerEventData(eventSystem); // Set the Pointer Event Position to that of the mouse position pointerEventData.position = Input.mousePosition; // Create a list of Raycast Results List results = new List(); // Raycast using the Graphics Raycaster and mouse click position graphicRaycaster.Raycast(pointerEventData, results); foreach (RaycastResult result in results) { Debug.Log(results[0].gameObject); } // For every result returned, output the name of the GameObject on the Canvas hit by the Ray (except for the item on mouse pointer) for (int i = 0; i < results.Count; i++) { // Check if number of results hit by the Ray is more than 0 if (results.Count > 0) { Debug.Log(i + " : " + results[i].gameObject); } } } #endregion }