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 absameen · May 20, 2012 at 09:50 PM · scrollviewlabeltextarea

SelectableLabel or TextArea in Scrollview

While using a Textarea with a Scrollview works great in Unity 3.5, it does not work well in Unity 3.4. I am developing a script that requires a scrollable label that is selectable. A standard label works well with the scrollview, but a selectable label or text area will not expand the scrollview controls. Any ideas on how this can be achieved in Unity 3.4?

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
2
Best Answer

Answer by absameen · May 20, 2012 at 11:44 PM

Was finally able to figure this one out. Create a GUIStyle called textStyle and use textStyle.CalcSize to size the TextArea based on the content inside of it. This will allow you to scroll it with a scroll view:

 scrollView = EditorGUILayout.BeginScrollView(scrollView, false, false);
 Vector2 textSize = textStyle.CalcSize(new GUIContent(myString));
 EditorGUILayout.TextArea(codeCache, textStyle, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true), GUILayout.MinWidth(textSize.x), GUILayout.MinHeight(textSize.y));
 EditorGUILayout.EndScrollView();
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
1

Answer by yoyo · May 09, 2014 at 12:43 AM

Thanks @absameen for putting me on the right track. In my case I wanted to deal with word wrap as well, so I needed to use CalcHeight, not CalcSize. This requires knowing the width of the current draw area, which required jumping through a couple of extra hoops.

Here is the final result, a helper method for drawing multi-line selectable text inside a scroll area:

     private Vector3 ScrollableSelectableLabel(Vector3 position, string text, GUIStyle style)
     {
         // Extract scroll position and width from position vector.
         Vector2 scrollPos = new Vector2(position.x, position.y);
         float width = position.z;

         scrollPos = GUILayout.BeginScrollView(scrollPos);

         // Calculate height of text.
         float pixelHeight = style.CalcHeight(new GUIContent(text), width);
         EditorGUILayout.SelectableLabel(text, Styles.info, GUILayout.MinHeight(pixelHeight));

         // Update the width on repaint, based on width of the SelectableLabel's rectangle.
         if (Event.current.type == EventType.Repaint)
         {
             width = GUILayoutUtility.GetLastRect().width;
         }

         GUILayout.EndScrollView();

         // Put scroll position and width back into the Vector3 used to track position.
         return new Vector3(scrollPos.x, scrollPos.y, width);
     }
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 JuliuszK · May 07, 2017 at 11:43 AM 0
Share

Thanks for posting this. I'm also trying to get a scrollable SelectableLabel, but it doesn't seem to be displaying the scroll bars, for some reason. I think the key is to use a correct style. What style are you using? 'Styles.info' doesn't seem to be anything recognizable for me - it must be your type, right? If so, what styling did you exactly use? I based my style on textArea, which works fine with scrollbars, but I don't want the editing features.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scrollable textarea in CustomEditor brakes scroll event from the inspektor 0 Answers

Text Asset not displaying correctly. 0 Answers

Problem with GUI.Button and other GUI items 1 Answer

Unity Scrolling Textbox 2 Answers

Getting Rect from scrollview according to position in scrollview 2 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