Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by levondov · Aug 02, 2021 at 06:54 AM · canvasuser interfacehierarchymouse-dragmouseover

UI element highlight OnPointerEnter effected by Hierarchy order of children?

I have made a simple inventory system that lets you drag and drop items around in different spots. I'm using the OnDrag, OnEndDrag, OnPointerEnter, and OnPointerExit events for most the code.

I am having this weird issue where I try to drag and drop/move around a selected item, the mouse/pointer will only trigger OnPointerEnter for inventory spots that appear after the original inventory spot clicked in the game Hierarchy system. e.g. dragging an item from inventory slot #6 (child object #6) will only allow my mouse pointer to enter inventory slots from #6+ and won't trigger on slots #1-#5.

This is illustrated in the gif below where my inventory slots (buttons) change to their highlighted button colors when the mouse hovers over them. This seems to only work for slots (buttons) that appear after the original slot clicked in the game Hierarchy.

Is there some kind of canvas/children ordering of OnPointerEnter events when tied with OnDrag events?

Gif demo of problem

If it helps, here is the simple code attached to the various items. The Inventory system is a set of UI button gameobjects with each having a UI Image gameobject attached as a child.



Attached to every UI image gameobject within each inventory slot:

 public class ItemDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler
 {
     public void OnDrag(PointerEventData eventData)
     {
         // drag UI image object around the screen
         transform.position = Input.mousePosition;
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         // ask the inventory manager to decide where this item will end up going
         transform.parent.parent.GetComponent<InventoryManager>().ItemDropEvent(transform.parent.GetComponent<InventorySlotHoverHandler>().InvSlotId, transform);        
     }
 }



Attached to each inventory slot (UI button object):

 public class InventorySlotHoverHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
     int invSlotId;
     public int InvSlotId
     {
         get { return invSlotId; }
     }
 
     void Start()
     {
         // assign inventory slot ID number based on gameObject name
         invSlotId = int.Parse(Regex.Replace(gameObject.name, "[^0-9]", ""));
     }
     public void OnPointerEnter(PointerEventData eventData)
     {
         // tell the inventory manager that the mouse is hovering over this slot
         transform.parent.gameObject.GetComponent<InventoryManager>().UpdateInventoryHoverStatus(invSlotId, 1);
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         // tell the inventory manager that the mouse is no longer hovering over this slot
         transform.parent.gameObject.GetComponent<InventoryManager>().UpdateInventoryHoverStatus(invSlotId, 0);
     }
 }



I have an inventory manager script that just keeps track of which slots are occupied or not as well as which slots currently have the mouse hovering over them.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by levondov · Aug 03, 2021 at 05:42 AM

If anyone is still interested, I figured it out.

Basically while the image/item is being dragged, we need to turn on the Raycast Target:

  public class ItemDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler
  {
      public void OnDrag(PointerEventData eventData)
      {
          // drag UI image object around the screen
          transform.GetComponent<Image>().raycastTarget = false;
          transform.position = Input.mousePosition;
      }
  
      public void OnEndDrag(PointerEventData eventData)
      {
          // ask the inventory manager to decide where this item will end up going
          transform.GetComponent<Image>().raycastTarget = true;
          transform.parent.parent.GetComponent<InventoryManager>().ItemDropEvent(transform.parent.GetComponent<InventorySlotHoverHandler>().InvSlotId, transform);        
      }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to render canvas with screen space - camera on top of canvas with screen space - overlay? 2 Answers

Camera preview different from when actually running the game? 0 Answers

Hidden Canvas, doesn't disable interaction with buttons. 1 Answer

Insane amount of CPU power used to activate UI inventory window 2 Answers

How to scale according to different mobile resolutions? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges