Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by paulsk8 · Aug 22, 2015 at 11:50 AM · uiguiimagepanel

Panel content size fitter not working

I have a panel in my canvas. alt text

I've added the content size fitter script. In this panel I will instantiate prefabs (which are also panels, that represent level boxes)

With this code

 GameObject levelBox = Resources.Load < GameObject > ("Prefabs/Level Box");
 
 int y = 0;
 for (int i = 1; i <= 10; i++) {
     GameObject a = (GameObject) Instantiate(levelBox);
     a.transform.SetParent(transform, false);
     RectTransform rectTransform = a.GetComponent < RectTransform > ();
 
     rectTransform.anchoredPosition = new Vector2(0, y);
     y -= 300;
 }

The problem is that the panel won't change its size to fit all the child objects. I need to do this, because the panel parent has a Scroll Rect.

How it looks at runtime: alt text

capture.png (32.8 kB)
capture.png (128.2 kB)
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
5

Answer by nuwan-nara · Oct 28, 2015 at 11:17 AM

Mark the parent panel RectTransform as needing it's layout to be recalculated during the next layout pass.

 LayoutRebuilder.MarkLayoutForRebuild(this.transform as RectTransform);

http://docs.unity3d.com/460/Documentation/ScriptReference/UI.LayoutRebuilder.MarkLayoutForRebuild.html

Comment
Add comment · Show 1 · 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 redonwhite · Aug 12, 2016 at 03:55 PM 0
Share

This is the solution that actually works. Thanks man, I was going crazy!

avatar image
0

Answer by RafiXWPT · Aug 22, 2015 at 12:51 PM

Add layout element component to lvlpanels or add grid/vertical/horizontal layout to that content fitter panel.

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

28 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

Related Questions

How do I use anchors in UI? 0 Answers

How to create scene with multiple panels? 0 Answers

Change UI image in a conversation 1 Answer

How to change the position of a button? 1 Answer

Displaying GUIText on top of Unity UI Image? 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