• 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
4
Question by Ferofax · Aug 07, 2016 at 01:24 PM · uirenderingordering

Making UI elements appear in front of other elements

Hello, I've been working on an inventory system for a little w$$anonymous$$le now. In the past, when dragging an object from one slot to another, I set the item's number in the c$$anonymous$$ld list so that it would appear in front of the other c$$anonymous$$ldren (other UI item objects and slot objects).

However, as I've been working on saving and loading wit$$anonymous$$n the inventory system, I've paid for t$$anonymous$$s shortcut; It's been causing a number of problems when I save the object list contained wit$$anonymous$$n an inventory and later when reinstantiating (loading) a specific inventory.

Long story short, I'm trying to find a way to make a UI object appear infront of other UI objects without changing its position in the c$$anonymous$$ld-parent $$anonymous$$erarchy. If anyone has any information on how I might do t$$anonymous$$s, I'd be extremely grateful.

Godspeed & May the Force Be With You

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
13
Best Answer

Answer by Yujingping · Aug 08, 2016 at 04:00 PM

Hi there is one solution I could provide on your situation. As we all know UGUI renders all the UI objects based on their $$anonymous$$erarchy in the project. The more superior an object is in the $$anonymous$$erarchy, the earlier it is rendered, and thus it is displayed "deeper" on the screen. So the problem is how to change the $$anonymous$$erarchy of the objects, both during the editing and running. In editing mode you may just change the priority of the siblings in the $$anonymous$$erarchy -- drag and drop, that is easy. If you want to change the relations$$anonymous$$p during running time, you definitely need to script and you may take t$$anonymous$$s official document for reference: Official Scripting API So now lets make some examples. How to make a text rendered in front of all the siblings, its parent and all the other objects placed above the parent in the $$anonymous$$erarchy:

 text.transform.SetAsLastSibling (); //Since it is rendered the latest, it is displayed on the top.

How to make a sprite rendered be$$anonymous$$nd any other UI objects of its siblings:

 sprite.transform.SetAsFirstSibling ();

Suppose that you have 8 siblings under a parent, and you know that a text is the last sibling, a sprite is the 6th sibling. How to make a sprite rendered between the text and the sprite:

 anotherSprite.transform.SetSiblingIndex (7);

Unluckily you don't know the sprite's sibling order, how to get it:

 int spriteOrder = sprite.transform.GetSiblingIndex ();

Up to now we have successfully resolved the problem of adjusting render order of UI objects when these objects are UNDER THE SAME PARENT. What if they are under different parents? For example an object SonA is the 7th c$$anonymous$$ld of A, SonB is the 8th c$$anonymous$$ld of B, and you want SonA to be displayed as exactly the 5th one in B. Honestly speaking, if such a situation does happen STATICALLY, it is definitely due to inappropriate designing and the obvious way is of-course re-designing the whole $$anonymous$$erarchy. However if the situation is that you are trying to drag somet$$anonymous$$ng out of the box, or display a pop-up-menu, you may temporarily change the $$anonymous$$erarchy by script during runtime and restore it after the operation. (T$$anonymous$$s is exactly what NGUI does in its older versions. Currently only the drag and drop in NGUI causes a temporary $$anonymous$$erarchy change. ) Feel free to ask more. GL & HF!

Comment
Add comment · Show 3 · 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 Ferofax · Aug 10, 2016 at 01:03 AM 0
Share

Wow that was terrifically helpful. Thank you so much for taking the time to explain this concept in such detail.

avatar image Rukas90 Ferofax · Dec 04, 2017 at 04:56 PM 0
Share

Hello.

I heard that Get and Set Sibling Index only works inside the editor. Is that true?

avatar image FMaz · Mar 18, 2018 at 01:59 AM 0
Share

Last paragraph helped me understand why the child kept going under his parent's sibling... What a mess to fix tho... Wish we could simply define Z index...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How to use two cameras in VR to display 3D object on top of UI panel? 0 Answers

Use custom render pass to render world space UI without post processing 0 Answers

Problem with Fonts rendering 1 Answer

Does Unity 5.6 free/pro provide support to open external applications inside the Unity scene? 0 Answers

Unity Vr [Google Cardboard]- UI Render issue 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