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 deeds0l · Jan 08, 2015 at 03:48 AM · ui

Changing UGUI sorting layer via script

I am aware that it is possible to change the sorting layer in the order of hierarchy ordering, but i need to change the order layer of the ui in runtime so that whenever I rotate a panel 180 degrees the other panel in the back of that panel will be shown above since it has a closer z axis to the camera. How may I do this?

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

Answer by deeds0l · Apr 06, 2015 at 07:28 AM

I figured out that you can attach a Canvas component to the UI object and override its main parent's sorting layer there.

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
avatar image
0

Answer by asafsitner · Jan 08, 2015 at 08:41 AM

Since the order of the hierarchy is the order of drawing, Unity has provided us with a neat API calls to sort hierarchy elements. Take a look at the following:
SetParent
SetAsFirstSibling
SetAsLastSibling

With these, you can call - for example - SetAsLastSibling on the panel you want to move to the front, since uGUI draws elements from first to last so the last element is drawn above the others. Note that this happens regardless of z-depth, it doesn't matter when it comes to uGUI.

Comment
Add comment · Show 5 · 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 deeds0l · Jan 08, 2015 at 10:27 AM 0
Share

yes this is a good work around but I would need my child panel to be layered on top of its parent how may I do this?

avatar image asafsitner · Jan 08, 2015 at 06:48 PM 0
Share

Something like this?

 childPanel.SetParent(childPanel.parent.parent);
 childPanel.SetAsLastSibling();
avatar image deeds0l · Jan 12, 2015 at 03:27 AM 0
Share

Thanks for the idea but the problem with that is that it resizes the childPanel which I am moving around. How could i keep the current rect scale and position as I move it around?

avatar image asafsitner · Jan 12, 2015 at 08:57 AM 0
Share

The API documentation of SetParent mentions an overload (well not exactly an overload, looks more like a default false value on worldPositionStays but it behaves more or less the same for this purpose) for keeping the scale, rotation and position the same after changing the parent, like so:

 childPanel.SetParent(childPanel.parent.parent, true);

Notice the additional true parameter sent to the method - this is the option to make the transform keep its world scale, rotation, and position after switching parent.

avatar image deeds0l · Jan 12, 2015 at 10:15 AM 0
Share

The problem with this is that it does not really keep my scale but it follows the anchor point of the childPanel and still rescales it.

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

26 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

Related Questions

ugui, onclick, acceptable function signatures 2 Answers

[uGUI] Performance on Color Lerping 0 Answers

How to set PosX and PosY in runtime UGUI 4 Answers

Procedurally generated uGUI elements render under world space objects 1 Answer

Subtracting random numbers instead of 1 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