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
1
Question by Catlard · May 07, 2015 at 05:15 AM · uipositionpositioningrecttransform

UI image position assignment not working correctly

I'm having a terrifically horrible time positioning UI elements using Unity UI. I'm attempting to center a number of objects in a list around a central point -- let's call it the "root". The root's x position is always in the middle of a row. I use the following for loop to determine the x positions for the UIObejects in a given row:

         float seedsInRow = currentRow.Count;
         float startX = ((seedsInRow-1) * _spaceBetweenSeeds) /2 * -1;
         for(int i = 0; i < seedsInRow; i++) {
             float xPos = startX + (i * _spaceBetweenSeeds);
             Vector3 newPos = new Vector3(xPos, _currYTop, 0);
             currentRow[i].seedUIObject.transform.localPosition = newPos;
             print (xPos);
         }
         print ("__________________________");

The printout from this is as expected, provided _spaceBetweenSeeds is 120:

alt text

However, this is where things start to get weird. In the editor, my seeds (the green circles) are positioned to the right of the "root", and off center, as follows:

alt text

Then, their transforms say they are at various x positions, but these positions do not match the readout. It's as though they're being repositioned by Unity after I set the localposition in my for loop. For example, in the third iteration in the console, it says the objects' x positions should be at -120, 0, and 120. That's great. However, when I look at them in the inspector after setting this stuff up, the x positions are instead 0, 60, and 120. I'm not doing anything to them otherwise -- this is in a scene where this is the only script running. The UI objects' parents are all zeroed out, in scale and position. The root is the only one with a Y offset, so it will sit at the bottom of the screen.

Why, o why, does this happen? Help me, Obi Wan Kenobis, you are my only hope!

untitled-2.png (9.6 kB)
untitled-1.png (50.1 kB)
Comment
Add comment · Show 3
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 fafase · May 07, 2015 at 05:43 AM 1
Share

I find the new UI extremely easy to use for extremely easy task and extremely painful for complex task.I guess this is Unity willing to attract the masses.

One thing you could do to simplify is using a Horizontal Layout Group. Specify the child alignment, position it and start adding items. They will naturally align.

if you tell child alignement center left, any new addtion will come to the right. Now there is a catch, your container should be of the side you need it. That is if you have three buttons, make it

  size.x = btnSize * 3;
  size.y = btnSize;

or they will start to shrink to fit.

avatar image Catlard · May 07, 2015 at 05:46 AM 0
Share

This is a potential solution, but eventually I'll have to move the objects TO their positions, not just automatically place them. It doesn't seem like this solution will allow for that, though...will it?

avatar image fafase · May 07, 2015 at 06:32 AM 0
Share

If move the object to their position means you need to reorder the button order you can use SetSiblingIndex(int index) which will reset the order of the children of the HLG and Unity automatically puts the first one (top) as most left.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Catlard · May 08, 2015 at 05:14 AM

Solved it ! It turned out that my RectTransforms were behaving oddly because I was storing them in a class I wrote to keep them organized, called EventRow. Event row did not even inherit from MonoBehavior, but storing the RectTransforms in a System.Collections.Generic list inside that object caused them to position strangely. So, when I started storing them as children of other Rect Transforms, instead of keeping track of them in my EventRow class, it worked just fine! Strange, no? Have you ever seen anything like this? The original purpose of my EventRow class was to allow me to have a list of event lists -- a grid of events. My guess is that RectTransform doesn't like being a list of lists of this variety, and that it somehow cares about whether it's in a collection or not. Go figure!

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

19 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

Related Questions

How to move Unity UI Panel out of screen? 1 Answer

Code thinks RectTransforms position is (0,0,0) 1 Answer

Get position of specific letter in UI Text 4 Answers

RectTransform returning incorrect rect bounds 0 Answers

Set Rect Transform Screen Position 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