Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create scrollbars in Unity3D

    • 0
    • 4
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 1.43k
    Comment on it

    Scrollbar in Unity3D

    Scroll bars let a user choose the direction and distance to scroll through information in a related window or list box.

    Scrollbars are very common on all languages..

    In Unity we can also build our own scrollbars. Its depends upon the pixels. So you should be very care fully create these.

    It comes under the GUI section, So you need to write the code in OnGUI function.

    Here is some code packet to create the scrollbars.

    // Define some variables..
    private int selected = -1;
    private float scrollVelocity = 0f;
    private float timeTouchPhaseEnded = 0f;
    private const float inertiaDuration = 0.5f;
    
    public int numRows;
    public Vector2 rowSize;
    public Vector2 windowMargin;
    public Vector2 windowPosition;
    public Vector2 listMargin;
    
    
    void OnGUI ()
    {
        GUI.skin = null;
        windowRect = new Rect(windowMargin.x, windowMargin.y, windowPosition.x,windowPosition.y);
        GUI.Window(0, windowRect, (GUI.WindowFunction)DoWindow, "");
    }
    

    This will call another function:-

    void DoWindow (int windowID)
    {
        Vector2 listSize = rowSize;
        Rect rScrollFrame = new Rect(listMargin.x, listMargin.y, listSize.x, listSize.y);
        Rect rList = new Rect(0, 0, rowSize.x, numRows*rowSize.y);
        scrollPosition = GUI.BeginScrollView (rScrollFrame, scrollPosition, rList, false, false);
        Rect rBtn = new Rect(0, 0, rowSize.x, rowSize.y);
        for (int iRow = 0; iRow < numRows; iRow++)
        {
            // draw call optimization: don't actually draw the row if it is not visible
            if ( rBtn.yMax >= scrollPosition.y && rBtn.yMin <= (scrollPosition.y + rScrollFrame.height) )
            {
                bool fClicked = false;
    
    
                if ( iRow == selected )
                {
                    fClicked = GUI.Button(rBtn, iRow .ToString());
                }
                else
                {
                    fClicked = GUI.Button(rBtn, iRow .ToString());
                }
    
                // Allow mouse selection, if not running on iPhone.
                // Note: this code will be triggered
                if ( fClicked && Application.platform != RuntimePlatform.IPhonePlayer )
                {
                    Debug.Log("Clicked on :- "+iRow);
                }
            }
            rBtn.y += rowSize.y+1;
        }
        GUI.EndScrollView();
    }
    

    Use this code for the Scrollbars in Unity.. Be free to code...... :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: