Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by TheReaper99999 · Jun 25, 2018 at 06:36 PM · menuitem

How do you add text to a scroll view programatically,How do I add text items to a scroll view?

I've only been using unity for about a week so sorry for the n00b question, I have a Scene that's supposed to act as a score screen and display all the score data for different players in the game, I have a Scroll view with a vertical layout group on the content, I have a text file with t he data separated by lines, and I have a stream reader to pull from the text file, but I can't seem to get it to load into the scroll view properly,I just started using Unity about a week ago so sorry for the n00b question, So I have a Scene to view score data, I added a scroll view with a vertical layout group as a child, have a text file with each entry separated on a new line, have a stream reader set up to read the text file, but haven't been able to get the scores loaded into the scroll view properly.

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

Answer by binaryuniverse · Jun 25, 2018 at 10:55 PM

One thing to realize is that the scrollview can hold anything. Not just text. That makes it powerful but also means you need to work a little to get it to work the way you want it to.

If your scrollview is set up in unity, make sure to add the following components: Layout -> Vertical Layout Group and Layout -> Content size fitter. The vertical layout group will child anything under it and place it nicely in the box while the content size fitter helps make the size of the children appropriate (you'll probably find that in the second one, you need to set "child force expand horizontal and child control size vertical, but experiment.)

Save a reference to the scrollview. You'll need it later:

     RectTransform SomeScrollRect = GameObject.Find(" ").GetComponent<ScrollRect>();


In quotes, you'll use the name that you give the object in the inspector. Make it unique.

Then when you want to generate a text option in code, do this:

 DefaultControls.Resources TempResource = new DefaultControls.Resources();
 GameObject NewText = DefaultControls.CreateText(TempResource);



(You could also use CreateButton if you want the list to be interactive.) Finally, you'll need to add a LayoutElement to your object:

 NewText.AddComponent<LayoutElement>();


Finally, add the new text to the scrollview:
Edit: You need to parent the content box, not the ScrollView, I wrote a function for this that I use myself. Sorry I put the wrong parent here: I call my function FindContent because it returns a reference to the content box of the scrollview. /Edit

 public RectTransform FindContent (GameObject ScrollViewObject) {
     RectTransform RetVal = null;
     Transform[] Temp = ScrollViewObject.GetComponentsInChildren<Transform>();
     foreach (Transform Child in Temp) {
         if (Child.name == "Content") { RetVal = Child.gameObject.GetComponent<RectTransform>(); }
     }
     return RetVal;
 }


Then you can set the parent like this:

 NewText.transform.SetParent(FindContent(SomeScrollRect));
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 TheReaper99999 · Jun 26, 2018 at 01:26 AM 0
Share

ok so that's how I make a new text element, so then I just have to set the text to the line I pulled from the file, right? so my function might look something like
void GetData() {
Probe = new StreamReader("Data.txt");
while (!Probe.EndOfStream)
{ temp = Probe.ReadLine();
DefaultControls.Resources TempResource = new DefaultControls.Resources();
GameObject NewText = DefaultControls.CreateText(TempResource);
NewText.AddComponent ();
NewText.GetComponent ().text = temp;
} }

avatar image binaryuniverse · Jun 26, 2018 at 01:54 AM 0
Share

That looks like it should mostly work. You'll also want to mess around with style options, potentially. One last thing I forgot though. You need to save a reference to the scrollview and then set it as the parent of your new object. I'll update my answer to reflect that.

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

143 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

Related Questions

Unity UI Prevent Layout component Update 0 Answers

Creating 'menu toggle' class 0 Answers

Classic window menuin Unity? 1 Answer

how to add custom menu item in hierarchy pop menu? 0 Answers

Dropdown content out of bounds 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