CustomScrollView

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "==Description== This is just a custom implementation of a ScrollView like GUI.BeginScrollView but you have full control whether the scrollbars are shown or not. ==Usage== Almost...")
 
Line 6: Line 6:
  
 
<csharp>
 
<csharp>
     pos = GUIX.BeginScrollView(new Rect(10,10,100,100), pos, new Rect(0,0,1000,1000), true, true, GUI.skin.horizontalScrollbar, GUI.skin.verticalScrollbar);
+
     pos = GUIX.BeginScrollView(new Rect(10,10,100,100), pos, new Rect(0,0,1000,1000), true, true);
 
     // content here
 
     // content here
 
     GUIX.EndScrollView();
 
     GUIX.EndScrollView();
 +
   
 +
    // or with custom styles
 +
    pos = GUIX.BeginScrollView(new Rect(10,10,100,100), pos, new Rect(0,0,1000,1000), true, true,GUI.skin.horizontalScrollbar, GUI.skin.verticalScrollbar);
 
</csharp>
 
</csharp>
  
Line 53: Line 56:
 
         return scrollPosition;
 
         return scrollPosition;
 
     }
 
     }
 +
 +
    public static Vector2 BeginScrollView(
 +
        Rect position,
 +
        Vector2 scrollPosition,
 +
        Rect contentRect,
 +
        bool useHorizontal,
 +
        bool useVertical)
 +
    {
 +
        return BeginScrollView(position, scrollPosition, contentRect, useHorizontal, useVertical, GUI.skin.horizontalScrollbar, GUI.skin.verticalScrollbar);
 +
    }
 +
 
     public static void EndScrollView()
 
     public static void EndScrollView()
 
     {
 
     {

Revision as of 21:17, 29 June 2011

Description

This is just a custom implementation of a ScrollView like GUI.BeginScrollView but you have full control whether the scrollbars are shown or not.

Usage

Almost like the original function:

<csharp>

   pos = GUIX.BeginScrollView(new Rect(10,10,100,100), pos, new Rect(0,0,1000,1000), true, true);
   // content here
   GUIX.EndScrollView();
   
   // or with custom styles
   pos = GUIX.BeginScrollView(new Rect(10,10,100,100), pos, new Rect(0,0,1000,1000), true, true,GUI.skin.horizontalScrollbar, GUI.skin.verticalScrollbar);

</csharp>


Code

<csharp> using UnityEngine; using System; using System.Collections.Generic;

public static class GUIX {

   public static Vector2 BeginScrollView(
       Rect position,
       Vector2 scrollPosition,
       Rect contentRect,
       bool useHorizontal,
       bool useVertical,
       GUIStyle hStyle,
       GUIStyle vStyle)
   {
       
       Vector2 scrollbarSize = new Vector2(hStyle.CalcSize(GUIContent.none).y,vStyle.CalcSize(GUIContent.none).x);
       Rect viewArea = position;
       if (useHorizontal)
           viewArea.height -= scrollbarSize.x;
       if (useVertical)
           viewArea.width -= scrollbarSize.y;
       if (useHorizontal)
       {
           Rect hScrRect = new Rect(position.x, position.y + viewArea.height, viewArea.width, scrollbarSize.x);
           scrollPosition.x = GUI.HorizontalScrollbar(hScrRect,scrollPosition.x,viewArea.width,0,contentRect.width);
       }
       if (useVertical)
       {
           Rect vScrRect = new Rect(position.x + viewArea.width, position.y, scrollbarSize.y, viewArea.height);
           scrollPosition.y = GUI.VerticalScrollbar(vScrRect,scrollPosition.y,viewArea.height,0,contentRect.height);
       }
       GUI.BeginGroup(viewArea);
       contentRect.x = -scrollPosition.x;
       contentRect.y = -scrollPosition.y;
       GUI.BeginGroup(contentRect);
       return scrollPosition;
   }
   public static Vector2 BeginScrollView(
       Rect position,
       Vector2 scrollPosition,
       Rect contentRect,
       bool useHorizontal,
       bool useVertical)
   {
       return BeginScrollView(position, scrollPosition, contentRect, useHorizontal, useVertical, GUI.skin.horizontalScrollbar, GUI.skin.verticalScrollbar);
   }
   public static void EndScrollView()
   {
       GUI.EndGroup();
       GUI.EndGroup();
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox