Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Dalsia · May 23, 2019 at 05:05 AM · canvasui imageinventory systemsorting layers

How do I change the sorting layer of a UI image when it is being dragged?

I'm creating an inventory system, and am currently working on the UI display for it. However, I'm running into an issue in which implementing IDragHandler and dragging a UI image during play mode causes the image to appear behind other UI elements.

I understand that the way the Canvas sorts UI objects is from the top down, so that game objects at the bottom of the canvas hierarchy appear in front of objects that come earlier. My hierarchy looks like this:

Canvas
++Inventory
++++Slot Container
++++++Slot 1 (has a simple UI script with IDragHandler)
++++++++Item Image
++++++Slot 2 (has a simple UI script with IDragHandler)
++++++++Item Image

My slot UI script detects dragging, and then moves the slots' items to the location of my cursor when dragged. The only issue is that Slot 1's Item Image appears behind the entirety of Slot 2 when dragged near it; Slot 2's Item Image does not have this issue because it's lower in the hierarchy, and hence sorted in a higher layer.

How do I fix this? I would find it to be a pain if I had to create some jumbled hierarchy structure just to fix what seems to be a simple issue, and yet I am not aware of a good solution. To be clear, my desired behavior is to simply have any dragged item be visible above the entire inventory.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by johnathonjcramer · Feb 03, 2020 at 07:50 PM

If your objects never overlap, there is a quick fix with almost no additional overhead: transform.SetAsLastSibling

If your click-and-drag code is attached directly to the transform of your Slot 1 or Slot 2 object, it should look like this:

 void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
 {
     transform.SetAsLastSibling();
 }

If you aren't using the hierarchy of items inside Slot Container to do anything else code-wise, this will move whatever Slot you click on to the "front" of your UI by putting it at the "end" of your hierarchy.

Comment
Add comment · Show 1 · 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
avatar image Jonster · Jan 25 at 03:46 AM 0
Share

This solved my issue and was so easy! Thank you for sharing!

avatar image
0

Answer by Gervagared · Apr 22 at 07:40 PM

Fantastic. It also solved my problem. Thank you very much for sharing the solution.

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

116 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

Related Questions

UI object drawing order is different between Unity Editor and Standalone version 1 Answer

Flickering Image whith different resolution 0 Answers

Unity 4.6 UI How to put a sprite between two UI Images? 0 Answers

Issues rotating a UI image while the canvas is parented to the Main Camera 0 Answers

How change the camera orthographic size to the UI image top position? 1 Answer


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