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 shoffing · May 07, 2015 at 10:46 PM · uiinputfieldscroll

Add scrollbar to multiline input field???

I'm trying to add a scrollbar to a multiline input field so the user can input a nice big block of text and be able to scroll through it without having to drag-select text to scroll (or use the arrow keys). I've tried everything I can think of, content size fitters, horizontal layout groups with content size fitters, nothing works. Here's my current layout that kind of works, but for some reason the text disappears into the vertical ether and I have to drag-select text to be able to see it all everytime I type something:

http://puu.sh/hFgnH/d379aef11e.png

http://gfycat.com/IndolentHideousEel

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dibbie · May 08, 2015 at 12:29 AM

I actually just figured this out using elements instead of text (for my uses), but itll work the same.

  1. Create a new UI Panel

  2. Size it to whatever youd like

  3. Create a new UI Scrollbar

  4. Place it wherever ud like, mess with the settings to your preferences

  5. Create a new UI Text/Input Field/Whatever you want to use

  6. On that new UI Text, add a "Scroll Rect" component (UI > Scroll Rect)

  7. Set the "Content" to be WHAT you want to have scrolled, so if its a InputBox, you can use the Text child of that for example

  8. Depending which way you want to scroll (I assume conventionally, so Vertical) set the Horizontal, Vertical, or both scroll bars to the scroll bar you created

  9. Be sure to check the correct rotation of your scrollbar under the "Content", to be "Vertical" (in your case I assume, for conventional ways), or "Horizontal", or both, if you are using both directions

  10. Mess with the other settings (mainly movement type and the settings that follow the selected type of movement) how youd like

  11. Create some text that goes beyond the set boundaries defined at edit time (so write a bunch of text...), then use the scroll bar

It didnt work for me with scrolling using my mouse wheel, but it could be that I dont have it focused when I do it, you could always make a simple script that if the mouse is scrolled up or down, then just chance the value of the scrollbar by code with a simple += or -= 1 kind of thing.

Hope that works out for you.

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 Dibbie · May 08, 2015 at 02:58 PM 0
Share
  • Sorry, in addition to this, I also found one problem, and a solution to it... You may notice that if you type too much, it wont hide the text properly... If you add a "$$anonymous$$ask" component to the text, it should fix that.

avatar image drewdough · Oct 18, 2016 at 09:20 PM 0
Share

thank you... I tried eahc step but it doesn't work for me. text doesn't scroll at all....

avatar image
0

Answer by illa3d · Feb 09, 2017 at 02:04 PM

Posted a hacky solution to InputField with scrollbar here: http://answers.unity3d.com/questions/932607/putting-a-multiline-inputfield-in-a-scroll-rect.html

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
0

Answer by SweatyChair · Nov 05, 2018 at 06:36 AM

This is my modified version of InputFieldMod, removing the highlight and take into account for canvas scale, etc:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using UnityEngine.Events;
 using System.Collections;
 
 namespace SweatyChair.UI
 {
 
     [ExecuteInEditMode]
     [RequireComponent(typeof(InputField))]
     public class InputFieldScroller : UIBehaviour
     {
 
         [Tooltip("The default row count in  InputField, this will be ignored if a ScrollRect is assigned")]
         [Range(1, 50)] [SerializeField] private int _minRowCount = 1;
 
         [Tooltip("Scroll rect parent")]
         [SerializeField] private ScrollRect _scrollRect = null;
 
         private InputField _inputField;
         private RectTransform _rectTransform;
 
         // Layout
         private LayoutElement _layoutElement;
         private HorizontalOrVerticalLayoutGroup _parentLayoutGroup;
 
         private Vector2 _scrollRectSize;
         private float _canvasScaleFactor = 1;
 
         protected override void Awake()
         {
             _inputField = GetComponent<InputField>();
             _inputField.onValueChanged.AddListener(new UnityAction<string>(ResizeInput));
             _rectTransform = GetComponent<RectTransform>();
             CanvasScaler canvasScaler = GetComponentInParent<CanvasScaler>();
             if (canvasScaler)
                 _canvasScaleFactor = canvasScaler.scaleFactor;
             _layoutElement = GetComponent<LayoutElement>();
             _parentLayoutGroup = transform.parent.GetComponent<HorizontalOrVerticalLayoutGroup>();
         }
 
         // Resize input field recttransform
         private void ResizeInput()
         {
             ResizeInput(_inputField.text);
         }
 
         private void ResizeInput(string text)
         {
             // Current text settings
             TextGenerationSettings settings = _inputField.textComponent.GetGenerationSettings(_inputField.textComponent.rectTransform.rect.size);
             settings.generateOutOfBounds = false;
             settings.scaleFactor = _canvasScaleFactor; // HACK: scale factor of settings not following the global scale factor... make sure it do
 
             // Get text padding (min max vertical offset for size calculation)
             float vecticalOffset = _inputField.placeholder.rectTransform.offsetMin.y - _inputField.placeholder.rectTransform.offsetMax.y;
 
             // Preferred text rect height
             float preferredHeight = (new TextGenerator().GetPreferredHeight(text, settings) / _canvasScaleFactor) + vecticalOffset + 10;
             float minHeight;
 
             // Default text rect height (fit to scroll parent or expand to fit text)
             if (_scrollRect)
                 minHeight = _scrollRect.GetComponent<RectTransform>().rect.size.y;
             else
                 minHeight = ((new TextGenerator().GetPreferredHeight("", settings) * _minRowCount) / _canvasScaleFactor) + vecticalOffset;
 
             // Current text rect height
             float currentHeight = _inputField.textComponent.rectTransform.rect.height;
 
             // Force resize
             if (Mathf.Abs(currentHeight - preferredHeight) > Mathf.Epsilon) {
                 float newHeight = Mathf.Max(preferredHeight, minHeight); // At least min height
                 if (_parentLayoutGroup && _layoutElement)
                     _layoutElement.preferredHeight = newHeight;
                 else
                     _rectTransform.sizeDelta = new Vector2(_rectTransform.rect.width, newHeight);
             }
 
             // Scroll to bottom if just added new line
             if (gameObject.activeInHierarchy && _inputField.caretPosition == _inputField.text.Length && _inputField.text.Length > 0 && _inputField.text[_inputField.text.Length - 1] == '\n')
                 StartCoroutine(ScrollToBottomCoroutine());
         }
 
         // Update scroll rect position (after Layout was rebuilt)
         private IEnumerator ScrollToBottomCoroutine()
         {
             yield return new WaitForEndOfFrame();
             if (_scrollRect != null)
                 _scrollRect.verticalNormalizedPosition = 0;
         }
 
     }
 
 }
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

24 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

Related Questions

Putting a multiline InputField in a Scroll Rect? 4 Answers

How to disable character movement while editing InputField? 1 Answer

In an Input Field, I want to remove the whole word, if my user deletes on character of it 1 Answer

InputField null refrence exception 1 Answer

NullReferenceException after passing InputField gameObject into EventSystem.SetSelectedGameObject() 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