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 darkhog · Jan 25, 2015 at 06:03 PM · uinoobfailure

[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?

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

1 Reply

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

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 ;)

Comment
Add comment · Show 2 · 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 darkhog · Jan 25, 2015 at 07:44 PM 0
Share

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.

avatar image troien · Jan 25, 2015 at 08:01 PM 1
Share

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

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

20 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

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


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