- Home /
[4.6 UI] Buttons spawn from the bottom of the panel
Here's the relevant part of the code:
void Start () {
GameObject tempObject;
for (int i =0;i<maxCategoryNum;i++){
tempObject = Instantiate(CategoryButton) as GameObject;
tempObject.GetComponent<RectTransform>().SetParent(this.GetComponent<RectTransform>(), false);
Vector3 temppos = tempObject.GetComponent<RectTransform>().position;
temppos.x=0;
temppos.y=i*tempObject.GetComponent<RectTransform>().rect.height+10;
tempObject.GetComponent<RectTransform>().position = temppos;
tempObject.GetComponent<SetCurrentCategory>().ID=i;
tempObject.transform.FindChild("Text").gameObject.GetComponent<SetText>().setText(Globals.categories[i]);
}
}
What is going on is that the buttons for categories seems to spawn from the bottom of the panel instead from the top. And to top it off they spawn in reverse order. How to fix that?
Answer by troien · Jan 25, 2015 at 07:08 PM
That is probably because you are setting the position. Which is generally not what you want to do with the Unity UI.
Instead most of the time you want to use Layout groups on the parent (HorizontalLayoutGroup, VeritcalLayoutGroup or GridLayoutGroup). Because then you don't have to worry about setting the position through code a all, just add them as a child and these Layout groups will handle the rest. (Might need a LayoutElement on the child with values other then 0 though, to make it work depending on the situation)
The reason why this happens is because RectTransform.position is the same as transform.position. And when you look in the scene view, you will see that position [0,0,0] is the bottom left corner of your canvas. [0,heihtOfCanvas,0] is the upper left corner. So you actually place them from bot to top a.t.m. ;)
If you don't want to use layout groups, I would suggest trying to set variables that belong to the RectTransfom, like anchoredPosition, sizeDelta, etc. etc. instead of position. Or just use position anyway keeping the previous paragraph in mind, but note that it will break if you change the Canvas RenderMode ;)
Thanks. Probably will google some tuts about layout groups as I don't understand them and while docs can tell me what methods/fields they have, they won't tell me how to use them properly ;).
Render mode of canvas this is used in is more or less set in stone now, so that shouldn't pose an issue.
I just wonder why Unity did this in such weird way ins$$anonymous$$d of making it so 0,0 in 2D calculations refer to top-left corner like in most 2D engines I know. Oh well, can't change it, so I'll adapt.
Oh that first one is actually a good point.
Some of my links regarding layout should have referred to the $$anonymous$$anual ins$$anonymous$$d...
Your answer
Follow this Question
Related Questions
[4.6 GUI] Resizing panel to fit it content 2 Answers
uGUI: Button that overlaps text 1 Answer
[4.6 UI] Layouts messes with my object with/height even though force width/height is unchecked 0 Answers
[4.6 gui] Getting dimensions of 2D UI object (button for example) 2 Answers
Setting GameObject in a script. 0 Answers