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 Baalhug · Feb 12, 2014 at 02:56 AM · androidguidragscrollscrollbar

Is there a way to scroll GUI text WITHOUT scrollbars?

Hi, guys:

I need a way to scroll text without a scroll bar for my android app, simply dragging the text with a finger on it.

The problem of scrollview is scrollbars are really thin and hard to press with a finger, that's why nobody uses them on mobile platforms. So i need a simple way to make scrollbar disappear or another GUI control that can work for this. Any workaround could fit my needs, because my app is crap if users must try 5 times to press the scrollbar in order to read the whole text.

Any help is appreciated.

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 Fattie · Nov 15, 2014 at 04:40 AM 0
Share

There's a complete working answer here, very simple. It's only a few lines of code: http://answers.unity3d.com/questions/17347/any-examples-of-an-iphone-like-scrollview-using-un.html the selected answer by fudingyu

2 Replies

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

Answer by Bunny83 · Feb 12, 2014 at 04:29 AM

Sure, but you would have to implement the scrolling yourself. Some time ago i've posted a scrollview where you can explicitly hide the scrollbars if you need this. The scrolling is done with two nested GUI groups.

For the scolling you need to utilise the Touch input. Something like this:

 //C#
 int scrollFinger = -1;
 Vector2 scrollPos = Vector2.zero;
 
 Vector2 TouchScrollView(Rect aScreenRect, Vector2 aScrollPos, Rect aContentRect, ref int aFingerID)
 {
     aScrollPos = GUI.BeginScrollView(aScreenRect, aScrollPos, aContentRect);
     foreach(Touch T in Input.touches)
     {
         if (T.phase == TouchPhase.Began)
         {
             Vector2 pos = T.GetGUIPosition();
             if (aScreenRect.Contains(pos))
             {
                 aFingerID = T.fingerId;
             }
         }
         else if (afingerID == T.fingerId)
         {
             Vector2 delta = T.FixedTouchDelta();
             aScrollPos.y += delta.y;
             aScrollPos.x -= delta.x;
             if(T.phase == TouchPhase.Ended || T.phase == TouchPhase.Canceled)
                 afingerID = -1;
         }
     }
     return aScrollPos;
 }
 
 
 void OnGUI()
 {
     scrollPos = TouchScrollView(new Rect(10,10,100,100), scrollPos, new Rect(0,0,100,500), ref scrollFinger);
     GUI.Label(new Rect(0,0,100,500), "Some long text to scroll");
     GUI.EndScrollView();
 }

For this you need those two Touch extension methods which can also be found on the wiki:

 public static class GUIHelpers
 {
     public static Vector2 GetGUIPosition(this Touch aTouch)
     {
         var tmp = aTouch.position;
         tmp.y = Screen.height - tmp.y;
         return tmp;
     }
     
     // returns the true touch delta in pixels
     public static Vector2 FixedTouchDelta(this Touch aTouch)
     {
         float dt = Time.deltaTime / aTouch.deltaTime;
         if (dt == 0 || float.IsNaN(dt) || float.IsInfinity(dt))
             dt = 1.0f;
         return aTouch.deltaPosition * dt;
     }
 }

ps: I wrote this from scratch and should only be an example. Haven't tried this exact implementation in Unity but used a similar one quite a lot. If you want to try this one, please report back if anything doesn't work.

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 Baalhug · Feb 12, 2014 at 05:35 AM 0
Share

Thank you very much, man. I'm watching your code in you are god I have already implemented a drag function in my code for dragging and zoo$$anonymous$$g all GUI controls on my app, so when i remove the scrollbar and GUI styles of your code i think i'll be ready to go. Thank you very much again, i'll give you a thumb cause i cant give you 1000.

avatar image Baalhug · Feb 12, 2014 at 11:39 PM 0
Share

After a few tries on your code, using only this:

 public class GUIX {
     public static Vector2 BeginScrollView(Rect position, Vector2 scrollPosition, Rect contentRect) {
         Rect viewArea = position;
         GUI.BeginGroup(viewArea);
         contentRect.x = -scrollPosition.x;
         contentRect.y = -scrollPosition.y;
         GUI.BeginGroup(contentRect);
         return scrollPosition;
     }
  
     public static void EndScrollView() {
         GUI.EndGroup();
         GUI.EndGroup();
     }    
 }

as the GUIX, because i dont need scrollbars nor styles at all, and implementing drag function based on mouseup and mousedown; i've come to delete all.

Now i'm working on your code as it is, and it works good, only had to change 2 afingerID for aFingerID. But when i change the line 7:

 aScrollPos = GUI.BeginScrollView(aScreenRect, aScrollPos, aContentRect);

for the GUIX correspondent:

 aScrollPos = GUIX.BeginScrollView(aScreenRect, aScrollPos, aContentRect, false, false);

the other GUI controls on my program are called in Begingroup rects, and i can´t find the problem, as both endgroups are called after the exection in every frame for OnGUI.

avatar image Fattie · Nov 15, 2014 at 04:44 AM 0
Share

Again just click here for an extremely simple version of Bunn's solution, works perfectly http://answers.unity3d.com/questions/17347/any-examples-of-an-iphone-like-scrollview-using-un.html#answer-736286

avatar image
0

Answer by Baalhug · Feb 15, 2014 at 08:58 PM

After long searches and debugging i finally found a perfect solution.

The next code will create a scrolling text without scrollbars which users can scroll by simply dragging the text, with mouse drag or touch drag. It automatically calculates the height of contenttext, so it will end dragging when end of text is reached. NOTE it is only defined the vertical drag, but the horizontal can be made easily by adding the correspondent .x functions.

Code for your GUI or window function:

     public Vector2 scrollPos=Vector2.zero;

     void OnGUI(){
         string text1 = "A long text which will be dragged inside a smaller 'screen rect'...";
         GUIcontent labeltext = new GUIContent (text1);
         float textheight = GUIStyle4Label.CalcHeight(labeltext, 340f); //340f is the width of this example content label, use your own values here and in the other rects.
         scrollPos = CustomScrollView(new Rect(20, 30, 340, 400), scrollPos, new Rect(0, 0, 340, textheight+20));
         GUI.Label (new Rect (0, 0, 340, textheight+20), labelText, GUIStyle4Label);
         GUIX.EndScrollView();
 }


Function to scroll (inside your class):

 Vector2 ClickScrollView(Rect aScreenRect, Vector2 aScrollPos, Rect aContentRect)
 {        
     float difHeightRects=aContentRect.height-aScreenRect.height;
     if (Event.current.type==EventType.MouseDrag){
         if (aScreenRect.Contains(Event.current.mousePosition)){
             if (aScrollPos.y<=difHeightRects && aScrollPos.y>=0) aScrollPos.y -= Event.current.delta.y;
             if (aScrollPos.y>difHeightRects) aScrollPos.y=difHeightRects;
             if (aScrollPos.y<0) aScrollPos.y=0;
         }
     }
     aScrollPos = GUIX.BeginScrollView(aScreenRect, aScrollPos, aContentRect);
 return aScrollPos;
 }




Class GUIX (outside your class, but inside your script)

 public class GUIX {
     public static Vector2 BeginScrollView(Rect viewArea, Vector2 scrollPosition, Rect contentRect) {
         GUI.BeginGroup(viewArea);
         contentRect.y = -scrollPosition.y;
         GUI.BeginGroup(contentRect);
         return scrollPosition;
     }
  
     public static void EndScrollView() {
         GUI.EndGroup();
         GUI.EndGroup();
     }    
 }
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 Count_Rugen · May 26, 2014 at 07:19 PM 0
Share

@Baalhug - I must be having a derp moment. :) I cannot get your code to work. Taking the code you provided, creating a blank Android project with an empty game object, and attaching your script to it (show below in totality), produces this: link text Which is not scrollable in any way. Additionally, when I attempt to add more labels, it shrinks the font down to a microscopic size. Can you please point me in the right direction? Thanks!

 using UnityEngine;
 using System.Collections;
 
 public class ScrollingListView : $$anonymous$$onoBehaviour
 {
     public GUIStyle GUIStyle4Label;
     public Vector2 scrollPos = Vector2.zero;
 
     void OnGUI()
     {
         string text1 = "A long text which will be dragged inside a smaller 'screen rect'...";
         GUIContent labelText = new GUIContent(text1);
         float textheight = GUIStyle4Label.CalcHeight(labelText, 340f); //340f is the width of this example content label, use your own values here and in the other rects.
         scrollPos = ClickScrollView(new Rect(20, 30, 340, 400), scrollPos, new Rect(0, 0, 340, textheight + 20));
         GUI.Label(new Rect(0, 0, 340, textheight + 20), labelText, GUIStyle4Label);
         GUIX.EndScrollView();
     }
 
     Vector2 ClickScrollView(Rect aScreenRect, Vector2 aScrollPos, Rect aContentRect)
     {
         float difHeightRects = aContentRect.height - aScreenRect.height;
         if (Event.current.type == EventType.$$anonymous$$ouseDrag)
         {
             if (aScreenRect.Contains(Event.current.mousePosition))
             {
                 if (aScrollPos.y <= difHeightRects && aScrollPos.y >= 0) aScrollPos.y -= Event.current.delta.y;
                 if (aScrollPos.y > difHeightRects) aScrollPos.y = difHeightRects;
                 if (aScrollPos.y < 0) aScrollPos.y = 0;
             }
         }
         aScrollPos = GUIX.BeginScrollView(aScreenRect, aScrollPos, aContentRect);
         return aScrollPos;
     }
 }
 
 public class GUIX
 {
     public static Vector2 BeginScrollView(Rect viewArea, Vector2 scrollPosition, Rect contentRect)
     {
         GUI.BeginGroup(viewArea);
         contentRect.y = -scrollPosition.y;
         GUI.BeginGroup(contentRect);
         return scrollPosition;
     }
 
     public static void EndScrollView()
     {
         GUI.EndGroup();
         GUI.EndGroup();
     }
 }
 

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

21 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

Related Questions

Vertical Scrollbar GUI.Label 1 Answer

Scrollview button very small? 0 Answers

Problem with modifying scrollPosition on android 1 Answer

How to disable ScrollView dragging 4 Answers

Need help creating a scrollable text area for displaying EULA. 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