Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 jeanf_le · Jan 13 at 01:12 AM · uibuttonunityeditorlayoutvertical

How to change the height of a button according to the height of the text in it?

Hello.

How would you do if you had a vertival layout composed of buttons and you wanted: - the witdth of the buttons to be the same witdth as the vertical layout - the height of each button to be driven by the text inside it

The solution has to work by code as I create each elements by script.

I have tried to follow the doc but it does not work: https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/HOWTO-UIFitContentSize.html

I tried to use "contentSizeFitter" I tried to use "LayoutElement" I tried to use a vertical or horizontal layout on each buttons and desactivate childforcesWidth I tried a lots of more stuff.

I run unity 2020.3.24f1 LTS on windows

Even manually I cannot seem to find a system that works.

Anywone?

Thanks in advance.

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 mf41z · Jan 13 at 11:53 AM

You need to add a content size fitter to the buttons as well, if you're adding different shapes of text, so the button will fit the size of the text component. Try to explain better how you've set up these objects, with pictures and such, so I can have a better idea of how you're doing things

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 jeanf_le · Jan 13 at 02:15 PM

alt text

There is a contentsize fitter both on the TMPUGUI and the Button It looks fine before play and when i press play it acts wierdly. When I tweak the values of the vertical layout, eg the padding, it gradually turn back to normal.

The problem seem to come to the content size fitter on the button but I did not find a better way to get this setup working.

I am trying to think that I am going to need to code my own grid system. Seems ridiculous.


uibug.jpg (452.7 kB)
Comment
Add comment · Show 4 · 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 jeanf_le · Jan 13 at 03:17 PM 0
Share

Oh, also I added a VerticalLayout Component on the Button, somewhow works better. childControlWidth is true, everything else is fasle.

The "trick" I use atm is to launch a coroutine to change/changeback the values of the top vertical layout like bellow.

private IEnumerator DoMagic(VerticalLayoutGroup layout) { layout.padding = new RectOffset(11, 11, 11, 11); yield return new WaitForSeconds(.01f); layout.padding = new RectOffset(10, 10, 10, 10); yield return new WaitForSeconds(.01f); layout.childForceExpandWidth = true; yield return new WaitForSeconds(.01f); layout.childForceExpandWidth = false; yield return new WaitForSeconds(.01f); layout.childForceExpandWidth = true; }

If i run this coroutine after i created the whole thing it kinda works but one can see the transitions so it is a very ugly solution.... would like to find a proper way so that I can animate each buttons afterwards.

avatar image mf41z · Jan 13 at 09:08 PM 0
Share

How are you parenting the buttons? Whenever you parent something, do transform.SetParent(Transform parentTransform, false); I had problems with bad fittings and when I started turning the WorldPositionStays argument to false, my fittings worked.

Also, if you're tweaking values during play, make sure you edit them back once you've stopped the run, because changes made during play aren't usually saved.

avatar image jeanf_le mf41z · Jan 13 at 09:28 PM 0
Share

Here is what I do when I create a gameObject that i need to make as Child

  public static GameObject AsChild(this GameObject tmp, GameObject parent, ref List<GameObject> instanciatedObjects)
         {
             tmp.transform.SetParent(parent.transform, false);
             //tmp.transform.Reset();
     
             tmp.layer = 5;
             instanciatedObjects.Add(tmp);
     
             return tmp;
         }
 

Then I call:

 GameObject canvas = new GameObject("title").AsChild(this.gameObject, ref aListToKeepTrackOfThem);
avatar image mf41z jeanf_le · Jan 13 at 10:27 PM 0
Share

Why do you have a horizontal fitter inside the button? I don't see why that's there. The content size fitter whould be enough. Also, I think the problem is that both content fitters, the parent and the one inside the button, are trying to modify the position. Try removing the Content size fitter from the Button, and if that doesn't work, remove the one from the Text. That's what I would do

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

243 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 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 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 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 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 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 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 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 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

Button does not fit inside content of scrolling list with vertical layout group,Button is enlarged when put in vertical layout group 0 Answers

How can I instantiate button like this? 2 Answers

How to lock element width or height in a vertical or horizontal layout? 1 Answer

SetBool and SetTrigger not work on UI button click in unity 0 Answers

Scrollable Achievement Tabs? 1 Answer


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