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 Essential · Dec 12, 2014 at 07:10 PM · uirecttransform4.6

How to position UI object relative to parent

With the new UI system, is there a way to position a child (via script) to a relative percentage of its parent's dimensions?

E.g., Say I want to position child ⅓ of the way down its parent, which is 150 points in height, I could write: child.anchoredPosition.y = 0.333;

I realize I could grab the parent's size and calculate it myself, but just wondering if there's a built-in way with this new UI system.

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
0

Answer by fermmmm · May 12, 2016 at 06:23 AM

You cannot grab the parent size, because the size is relative to the anchors. Your "parent size" will be available as soon as the anchors of the parent are placed joined.

You can move the anchors of the object (anchorMin and anchorMax) these values also moves the object and the value range goes from 0 to 1, so if you place your anchors in the middle of the object in the editor and then in code you move them to (0.5, 0.5) using anchorMin and anchorMax, you will get your object in the center of the parent (for example).

If you can't move the anchors or you find it over complicated, you are going to need some math. Try the "Rect Transform Extended" asset, you can move and scale things around ignoring anchors: https://www.assetstore.unity3d.com/#!/content/41927

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 daleth90 · May 12, 2016 at 04:29 AM

 [SerializeField]
 private RectTransform childPrefab;
 [SerializeField]
 private Vector2 childAnchor = new Vector2( 0.5f, 0.3f );
 
 private void CreateChild() {
     RectTransform theChild = Instantiate( childPrefab );
     theChild.SetParent( transform, false );
     theChild.anchorMax = childAnchor;
     theChild.anchorMin = childAnchor;
 }
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 Piranha771 · Dec 04, 2018 at 04:58 PM 0
Share

This works exactly like it should be.

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

28 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

Related Questions

EventSystem causes a NullRef error on restarting a level 1 Answer

RectTransform.TransformPoint returning zero everytime after Unity 4.6.3 0 Answers

UI element moves down when another UI elements gets too close? 0 Answers

Showing text on top of a Slider, but not blocking Handler? 1 Answer

Stacked UI Panels do not position as expected 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