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
2
Question by stringa · Oct 19, 2010 at 02:52 AM · guiscrollview

Chat Window - BeginScrollView - AutoScroll

I was hoping to use GUILayout and ScrollView.

I want to create a chat window that autoscrolls the new messages, but I am not sure how to do this. I don't want to show any scrollbars either.

I was hoping that I could get away with some relatively simple code w/out calculating every position for every single line of text.

Is there an example of this?

Any hints?

THanks,

stringa

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 stringa · Oct 19, 2010 at 03:28 AM 2
Share

I figured it out. If you just set the Scroll Position to something rediculously high, it will always be scroll to the bottom of the scrollView. Also, I set the GUIStyle to none, and walla...I have what I was looking for.

At least I don't have to Calculate the size of every box and what the real scroll position would be.

2 Replies

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

Answer by Bampf · Oct 19, 2010 at 03:29 AM

Presumably each line of text is the same height, so all you need to know for each line's position is the line #. Multiply it by the line height; the top line will be at position 0 (typically.)

If you have an array of lines, the last position would be (array-length - 1) * lineHeight. (Make sure you handle the case where the array is empty though.)

If you use GUI.* for each line, you can set the Rect of each line yourself. Define lineWidth and lineHeight as public variables that you set in the inspector. Then loop over each line you want to display:

  • First rect: (0, 0 * lineHeight, lineWidth, lineHeight)
  • Second rect: (0, 1 * lineHeight, lineWidth, lineHeight)
  • Third rect: (0, 2 * lineHeight, lineWidth, lineHeight)
  • etc.

Set the scroll-view's position to the last line, and you're done. It gets a little trickier if you want to wrap each line of text, obviously. (see next paragraph for ideas)

If you were planning to use GUILayout. instead of GUI., you can do pretty much the same thing but you will either need to calculate each line's height ahead of time (using GUIStyle.CalcHeight), OR, use GUILayoutUtility.GetLastRect to find out how tall each line was after the fact. GetLastRect can be a little tricky, make sure you read up on it if you decide to go that way.

Comment
Add comment · Show 3 · 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 Bampf · Oct 19, 2010 at 03:30 AM 0
Share

Ha! I like your solution, it's very direct! But perhaps the methods I describe will help someone else some day, someone who needs to scroll to a particular line, for instance.

avatar image stringa · Oct 19, 2010 at 03:47 AM 0
Share

Thanks for the info though. I don't really think about GUIUtility class for some reason....

avatar image Bampf · Oct 19, 2010 at 09:11 PM 1
Share

Did you know that you can post (and accept) your own answer? I won't be offended if you do that- you came up with your own solution in the end.

avatar image
4

Answer by Mervill · May 23, 2013 at 12:04 AM

You want:

 public Vector3 scrollPosition;
 
 void OnGUI(){
     scrollPosition = GUILayout.BeginScrollView([...])
     {...}
     GUILayout.EndScrollView();
 }
 
 void SomeEvent(){
     // Force the scrollbar to the bottom position.
     scrollPosition.y = Mathf.Infinity;
 }
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 ceitel · Jul 18, 2017 at 10:09 PM 0
Share

Also works with: public Vector2 scrollPosition;

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

2 People are following this question.

avatar image avatar image

Related Questions

Unable to move scrollbar 1 Answer

Putting Dictionary/List using foreach as buttons in a scroll view? 3 Answers

Unity GUI fitting item list with max size 0 Answers

How To Auto Adjust height of GUI.BeginScrollView in unity 3d 1 Answer

How to drag a scrollView with TouchPhase.Moved? 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