Search Unity

Problem with OnPointerEnter - detecting child/ part of the object you are hovering

Discussion in 'Scripting' started by Afterbrain, Apr 29, 2017.

  1. Afterbrain

    Afterbrain

    Joined:
    Apr 23, 2017
    Posts:
    8
    So... I have something like this:

    upload_2017-4-29_15-39-56.png

    Every time you hover one of these squares I would like to have that description window follow your mouse with a description of the object you are hovering. The problem is - my code is attached to the inventory window, and I can't figure out how to get which of the individual slots is being hovered. I though I may be able to get to that with a lengthy math/ if statements mess, but is there a better/ simpler way to do that?

    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class I_Inventory : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler{
    8.  
    9.     public Image[] itemImages = new Image [itemSlots];
    10.     public I_Item[] items = new I_Item [itemSlots];
    11.     public const int itemSlots = 15;
    12.     public I_DetailWindow myDetailWindow;
    13.  
    14.     public void AddItem (I_Item itemToAdd)
    15.     {
    16.         for (int i = 0; i < items.Length; i++) {
    17.             if (items [i] == null) {
    18.                 items [i] = itemToAdd;
    19.                 itemImages [i].sprite = itemToAdd.sprite;
    20.                 itemImages [i].enabled = true;
    21.                 return;
    22.             }
    23.         }
    24.     }
    25.  
    26.     public void RemoveItem (I_Item itemToRemove)
    27.     {
    28.         for (int i = 0; i < items.Length; i++) {
    29.             if (items [i] == itemToRemove) {
    30.                 items [i] = null;
    31.                 itemImages [i].sprite = null;
    32.                 itemImages [i].enabled = false;
    33.                 return;
    34.             }
    35.         }
    36.     }
    37.  
    38.     public void OnPointerEnter(PointerEventData eventData){
    39.         int i = 0; // Here - This number needs to change based on where you are hovering.
    40.         if (items [i]) {
    41.             myDetailWindow.myDescription = items [i].myDetails;
    42.             myDetailWindow.BringWindow ();
    43.         }
    44.     }
    45.  
    46.     public void OnPointerExit(PointerEventData eventData){
    47.         myDetailWindow.EndWindow ();
    48.     }
    49.  
    (I_Item is a scriptable object holding the objects variables)

    (Thank you!)
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If it were me, an option I'd strongly consider is to put the pointer enter and exit events on the little boxes instead, reference the parent group, and tell it your index (pass your index to a function that displays the details in the window you want). :)
     
    CanisMajor and JoshuaMcKenzie like this.
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    Also if the window is to follow the cursor you might want to add a canvasgroup to that tooltip window and set it so that it doesn't block raycasts. otherwise you may get frames where the window will block your pointer calls and reset the entire state of the tooltip.
     
  4. Afterbrain

    Afterbrain

    Joined:
    Apr 23, 2017
    Posts:
    8
    Got that, thanks!

    Yeah, that is what I went with. After some struggle with all of the references between scripts, everything seems fine - thank you!