Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to make a stupid scrollview?

Discussion in 'Scripting' started by Epictickle, Apr 22, 2014.

  1. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    I've been at this for two hours now... Something that is supposed to be easy to implement has gotten the best of me....

    I'm trying to implement a scrollview into my window....

    Here's what I have in my window:

    Code (csharp):
    1.  
    2.  
    3. Vector2 scrollPosition = Vector2.zero;  //Global vector2...
    4.  
    5. void DoMyWindow(int windowID) {
    6.         scrollPosition = GUI.BeginScrollView(new Rect(10,50,480,100),
    7.                                              scrollPosition,
    8.                                              new Rect(0,0,400,400));
    9.         //GUI.skin.box.wordWrap = true;
    10.         GUI.TextArea(new Rect(0,0,400,400),displayText);
    11.         GUI.EndScrollView();
    12.  
    13.         GUI.Label(new Rect(10, 20, 100, 100), "Version: " + Version.x.ToString("F0") + "." + Version.y.ToString("F0") + "." + Version.z.ToString("F0") + "." + Version.z.ToString("F0"));
    14.         if (GUI.Button(new Rect(10, 300, 480, 35), "Send Mail"))
    15.         {
    16.             SendMail();
    17.         }
    18.        // GUILayout.BeginArea(new Rect(10, 45, 480, 100));
    19.  
    20.        // GUILayout.EndArea();
    21.         userComments = GUI.TextArea(new Rect(10,150,480,150), userComments, 1000);
    22.         Debug.Log(scrollPosition.ToString());
    23.         GUI.DragWindow(new Rect(0, 0, 10000, 10000));
    24.     }
    25.  
    I can literally SEE my scrollPosition change in the Debug.Log... But the GUI isn't changing for some reason....

    WHAT IS THE PROBLEM????

    For some reason it just will not work... I have to click the very bottom of the scrollbar and drag UP for it to scroll down... Why is this happening?

    I mean, I saw some posts from 2009 that said Unity knew about this bug.. I figured it would be fixed in 2014!
     
    Last edited: Apr 22, 2014
  2. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Bump... Seriously, I was thinking about buying pro until I found this.... I'm not going to buy a $2,000 piece of software unless it has a functional GUI system...
     
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I believe that GUI stuff has to be in the OnGUI method of a class. I'm guessing yours isn't, since the code works for me in an OnGUI method. Instead of calling in the scrollview with the DoMyWindow method, set up something like this:
    Code (csharp):
    1.  
    2. public class MyGuiStuff {
    3.     bool showScrollView = true;
    4.     Vector2 scrollPosition = Vector2.zero;  //Global vector2...
    5.     private void OnGUI()
    6.     {
    7.         if (showScrollView)
    8.         {
    9.             scrollPosition = GUI.BeginScrollView(new Rect(10, 50, 480, 100),
    10.                                                  scrollPosition,
    11.                                                  new Rect(0, 0, 400, 400));
    12.             //GUI.skin.box.wordWrap = true;
    13.             GUI.TextArea(new Rect(0, 0, 400, 400), displayText);
    14.             GUI.EndScrollView();
    15.  
    16.             GUI.Label(new Rect(10, 20, 100, 100), "Version: " + Version.x.ToString("F0") + "." + Version.y.ToString("F0") + "." + Version.z.ToString("F0") + "." + Version.z.ToString("F0"));
    17.             if (GUI.Button(new Rect(10, 300, 480, 35), "Send Mail"))
    18.             {
    19.                 SendMail();
    20.             }
    21.             // GUILayout.BeginArea(new Rect(10, 45, 480, 100));
    22.  
    23.             // GUILayout.EndArea();
    24.             userComments = GUI.TextArea(new Rect(10, 150, 480, 150), userComments, 1000);
    25.             Debug.Log(scrollPosition.ToString());
    26.             GUI.DragWindow(new Rect(0, 0, 10000, 10000));
    27.         }
    28.     }
    29. }
    30.  
    31.  
     
  4. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Right... I was definitely able to make it in OnGUI... But why wouldn't it work in a window? I mean, that kind of defeats the purpose of a window if you are unable to use controls inside of it... I figure if I make a thread with enough hype about it, UT will fix this 5-year-long bug..
     
    Last edited: Apr 22, 2014
  5. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I'm not sure what you mean about it not working in a window. If you mean why it won't work in a custom method, it's because Unity needs to know where your GUI code is going to be; it can't just tell where to look for updates to GUI elements and button clicks, it needs to be in a method that Unity knows will contain the GUI related code.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    It does work in a window. You must be doing something wrong.
    Heres a minimal sample that works.

    Code (csharp):
    1.     Rect windowRect = new Rect( 100, 100, 200, 300 );
    2.     Rect innerRect = new Rect( 0, 0, 400, 400 );
    3.     Rect outerRect = new Rect( 20, 50, 80, 80 );
    4.     Vector2 scroll;
    5.  
    6.     void OnGUI () {
    7.         windowRect = GUI.Window( 0, windowRect, WindowFunction, "Window!" );
    8.     }
    9.  
    10.     void WindowFunction ( int windowID ) {
    11.         GUILayout.Label( "Window label" );
    12.         scroll = GUI.BeginScrollView( outerRect, scroll, innerRect );
    13.         GUI.Label( new Rect( 0, 0, 200, 20 ), "Scroll label" );
    14.         GUI.Box( new Rect( 40, 40, 200, 200 ), "Scroll Box" );
    15.         GUI.EndScrollView();
    16.         GUI.DragWindow();
    17.     }
    18.  
     
  7. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    It is still not working for me lol. Even with your example.. Good to know it isn't a unity bug though.
     
    Last edited: Apr 23, 2014
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    Nothing is resetting scrollPosition elsewhere, is it? Or is an exception being thrown before it gets to GUI.EndScrollView()? What happens if you try using the default GUI skin?

    Does the Unity GUI quest log window work for you in the Dialogue System? It uses a scroll view inside a window. If it works, you could always grab the source out of Scripts/Source Code.unitypackage and pick it apart for any differences.
     
  9. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Ahh I did not know it used that. No, there is definitely no exceptions being thrown, and there is nothing resetting it anywhere. I actually had a debug in OnGUI to let me know the value of scrollPosition, and it was definitely giving me the correct numbers. For some reason, though, it fixed itself.. Don't ask how lol, I don't know. We were in the process of making our Bug Report window into an NGUI window since scrollview didn't work, but we tested it again, and it worked... It was weird lol

    EDIT:

    Figured out what the problem was. There were two of these scripts in the scene.
     
    Last edited: Apr 23, 2014