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 cj31387 · Dec 28, 2012 at 04:53 AM · guitextnguilabel

Ngui Text Vertical Scrollbar?

I want to create a scroll bar text field in NGUI where I can put as much text in a certain area but be able to scroll down with a vertical bar to see all of it. I haven't been able to find a NGUI tutorial that seems to do this. Also I just got Ngui pro can someone recommend some good starting tutorials? I checked out the 2 video tutorials they don't seem to cover things like I'm asking right now.

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

Answer by petrucio · Jan 24, 2013 at 06:27 AM

Yeah, it turned out to be a bit of a pain in the ass to do this. I'm sure there are better ways to do it, but here's my solution (it scrolls a parent object with anything in it, it can be text or other stuff). Should be pretty easy to use and self-explanatory:

 using UnityEngine;
 using System.Collections;
 
 //=============================================================================
 // Manages the scrolling of the contents inside a clipped panel
 // Just put all objects to be scrolled inside a common parent, and then setup
 // the panel, the scrollbar, and the common parent object in the inspector.
 //
 // Make sure this script does not sit on the scrollbar gameobject itself, since
 // we will be disabling it when the scrollbar is not needed.
 //=============================================================================
 public class UIPanelScroller : MonoBehaviour {
     
     public UIScrollBar scrollBar;
     public UIPanel     clippingPanel;
     public Transform   panelContentsParent;
     
     public float checkContentsInterval = 0.1f;
     public bool  alwaysShowScroller = false;
     
     private float contentsZeroY;
     
     //========================================================================
     void Start () {
         this.scrollBar.onChange += this.OnScrollbarChange;
         
         this.scrollBar.scrollValue = 0;
         this.contentsZeroY = this.panelContentsParent.localPosition.y;
         
         this.UpdateStuff();
         this.StartCoroutine(this.CheckContentsChanges());
     }
 
     //========================================================================
     IEnumerator CheckContentsChanges()
     {
         while (this.enabled) {
             yield return new WaitForSeconds(this.checkContentsInterval);
             this.UpdateStuff();
         }
     }    
 
     //========================================================================
     private float _getContentsHeight() {
         float relative = NGUIMath.CalculateRelativeWidgetBounds(this.panelContentsParent.transform).size.y;
         return relative * this.panelContentsParent.transform.localScale.y;
     }
     
     //========================================================================
     void UpdateStuff()
     {
         float panelH   = this.clippingPanel.clipRange.w;
         float contentH = this._getContentsHeight();
         this.scrollBar.barSize = panelH / contentH;
         
         this.scrollBar.gameObject.SetActive(true);
         if (contentH < panelH && !this.alwaysShowScroller) {
             this.scrollBar.gameObject.SetActive(false);
         }
     }
     
     //========================================================================
     void OnScrollbarChange(UIScrollBar ignored)
     {
         float nonVisibleHeight = this._getContentsHeight() - this.clippingPanel.clipRange.w + this.clippingPanel.clipSoftness.y;
         float contentPos = this.contentsZeroY + (nonVisibleHeight * this.scrollBar.scrollValue);
         this.panelContentsParent.transform.SetLocalPosY(contentPos);
     }
     
 }

SetLocalPosY is a simple extension method I use to avoid having to make a copy of stuff everytime I want to change a single value:

 public static void SetLocalPosZ(this Transform trans, float z) {
     Vector3 vector = trans.localPosition;
     vector.z = z;
     trans.localPosition = vector;
 }

I had some issues with the clipping panel refusing to show it's contents at startup, because everything was cached up in NGUI, so I had to make this hack to shake things up:

 public class UIPanelClipRefreshHack : MonoBehaviour {
 
     public void GO() {
         foreach(UIWidget widget in this.GetComponentsInChildren<UIWidget>()) {
             widget.enabled = false;
             widget.enabled = true;
         }
     }
 }

Drop it on the panel, and call Go when displaying your window.

Phew!

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

10 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

Related Questions

Can't draw GUI.Label text on subpixel values 0 Answers

GUI label/text appear/disappear 1 Answer

OnGUI called after LateUpdate screwing up debug text database 1 Answer

GUI.Label question about Resolution 1 Answer

Unity GUI text displaying as noise 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