Facebook
From Blush Mousedeer, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 70
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7.  
  8. public class PlaceItem : MonoBehaviour
  9. {
  10.  
  11.         #region PRIVATE VARIABLES
  12.  
  13.         // Reference to canvas graphic raycaster
  14.         private GraphicRaycaster graphicRaycaster;
  15.         // Reference to pointer event data
  16.         private PointerEventData pointerEventData;
  17.         // Reference to event system in scene
  18.         private EventSystem eventSystem;
  19.  
  20.         // Reference to map
  21.         private GameObject map;
  22.  
  23.         #endregion
  24.  
  25.         #region MONOBEHAVIOUR CALLBACKS
  26.  
  27.         private void Start()
  28.         {
  29.                 // Find the Raycaster from the GameObject (the Canvas)
  30.                 graphicRaycaster = FindObjectOfType<GraphicRaycaster>();
  31.  
  32.                 // Find the Event System from the Scene
  33.                 eventSystem = FindObjectOfType<EventSystem>();
  34.  
  35.                 // Find the map game object in the scene
  36.                 map = GameObject.Find("Map");
  37.         }
  38.  
  39.         private void Update()
  40.         {
  41.                 if (Input.GetMouseButtonDown(0))
  42.                 {
  43.                         PlaceItemOnMap();
  44.                 }
  45.         }
  46.  
  47.         #endregion
  48.  
  49.         #region PLACE ITEM ON MAP
  50.  
  51.         private void PlaceItemOnMap()
  52.         {
  53.                 // Set up the new Pointer Event
  54.                 pointerEventData = new PointerEventData(eventSystem);
  55.                 // Set the Pointer Event Position to that of the mouse position
  56.                 pointerEventData.position = Input.mousePosition;
  57.  
  58.                 // Create a list of Raycast Results
  59.                 List<RaycastResult> results = new List<RaycastResult>();
  60.  
  61.                 // Raycast using the Graphics Raycaster and mouse click position
  62.                 graphicRaycaster.Raycast(pointerEventData, results);
  63.                
  64.                 foreach (RaycastResult result in results)
  65.                 {
  66.                         Debug.Log(results[0].gameObject);
  67.                 }
  68.  
  69.                 // For every result returned, output the name of the GameObject on the Canvas hit by the Ray (except for the item on mouse pointer)
  70.                 for (int i = 0; i < results.Count; i++)
  71.                 {
  72.                         // Check if number of results hit by the Ray is more than 0
  73.                         if (results.Count > 0)
  74.                         {
  75.                                 Debug.Log(i + " : " + results[i].gameObject);
  76.                         }
  77.                 }
  78.         }
  79.  
  80.         #endregion
  81.  
  82. }
  83.