- Home /
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:
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:
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!
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.
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?
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.
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!
Your answer
Follow this Question
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