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
1
Question by MolochSim · Dec 04, 2014 at 02:14 PM · ui

[4.6 UI] Is it possible to alter the content of a Scroll Rect at runtime?

I am trying to transition from legacy GUI to the new UI. I need a Scroll Rect (using a ScrollBar) to display a set of Buttons, but the button functions and text strings are decided at runtime. Has anyone figured out if it is possible and how? I did not see it in the new UI tutorials. Thanks, guys.

Comment
Add comment · Show 1
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 SaraCecilia · Dec 04, 2014 at 02:45 PM 0
Share

Find any help in this thread? http://forum.unity3d.com/threads/scrollrect-runtime-content.263815/

1 Reply

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

Answer by AyAMrau · Dec 04, 2014 at 02:48 PM

Yes, it's possible. Once you created the scroll structure all you need to be doing is adding the created objects as children of the object, which the ScrollRect points to as Content.

 [SerializeField]
 private GameObject buttonPrefab = null; // Assign button prefab in editor
 
 [SerializeField]
 private GameObject listContainer = null; // Assign Contents object in editor
 
 private List<GameObject> items; // initialise this and fill with content that will display in the scroller
 
 private void PopulateScroller()
 {
    foreach (GameObject item in items)
    {
        // Create button and put it in the scroll rect
        GameObject button = Instantiate(buttonPrefab) as GameObject;
        button.transform.SetParent(listContainer.transform, false);
 
        // Customise the button with object from list
        GameObject captured = item;
        button.GetComponentInChildren<Text>().text = captured.name;
 button.GetComponent<Button>().onClick.AddListener(() => OnSelected(captured.name));
         }
     }
 
 private void OnSelected(string name)
 { 
    // What to do when button was clicked
    Debug.Log(name);
 }
Comment
Add comment · Show 5 · 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 MolochSim · Dec 04, 2014 at 02:57 PM 0
Share

Thank you so much. :)

avatar image MolochSim · Dec 04, 2014 at 03:42 PM 0
Share

Is the WorldPositionStays bool responsible for the button position in the ScrollView? Whether it is true or false, all buttons seem to appear on top of the previous one.

avatar image AyAMrau · Dec 04, 2014 at 03:49 PM 0
Share

it's responsible for making the button take the position of the parent (previously you had to set the local position to 0 after parenting).

Does your content object have a Vertical or Horizontal Layout Group? Cause that's a way to keep them all next to each other.

avatar image MolochSim · Dec 04, 2014 at 04:01 PM 0
Share

Will use a layout group. Thank you again, you helped a lot. Cheers. :)

avatar image Burkard · Sep 10, 2015 at 12:36 AM 0
Share

Plus 1 to the "AddListener(() => OnSelected(captured.name))" part... learned a lot from this! Thanks!

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

27 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

Related Questions

Scroll Rect Problem 1 Answer

unity 4.6 new UI Android soft buttons 1 Answer

Event Trigger and Scripting, how to avoid duplicate initialization? 1 Answer

UI world space text resolution 1 Answer

Unity 4.6 New UI Touch Detection 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