context: using UnityEngine;
  using System.Collections;
  
  /// <summary>
  /// Scrollable Area Control. Can be actually by changing Value, external scrollbar or swipe gesture
  /// </summary>
  [ExecuteInEditMode]
  [AddComponentMenu("2D Toolkit/UI/tk2dUIScrollableArea")]
  public class tk2dUIScrollableArea : MonoBehaviour
  {
      /// <summary>
      /// XAxis - horizontal, YAxis - vertical
      /// </summary>
      public enum Axes { XAxis, YAxis }
  
      /// <summary>
      /// Length of all the content in the scrollable area
      /// </summary>
      [SerializeField]
      private float contentLength = 1;
  
      public float ContentLength
      {
          get { return contentLength; }
          set 
          {
              ContentLengthVisibleAreaLengthChange(contentLength, value,visibleAreaLength,visibleAreaLength);
          }
      }
  
      /// <summary>
      /// Length of visible area of content, what can be seen
      /// </summary>
      [SerializeField]
      private float visibleAreaLength = 1;
  
      public float VisibleAreaLength
      {
          get { return visibleAreaLength; }
          set 
          {
              ContentLengthVisibleAreaLengthChange(contentLength, contentLength, visibleAreaLength, value);
          }
      }
  
      /// <summary>
      /// Transform the will be moved to scroll content. All content needs to be a child of this Transform
      /// </summary>
      public GameObject contentContainer;
  
      /// <summary>
      /// Scrollbar to be attached. Not required.
      /// </summary>
      public tk2dUIScrollbar scrollBar;
  
      /// <summary>
      /// Used to record swipe scrolling events
      /// </summary>
      public tk2dUIItem backgroundUIItem;
  
      /// <summary>
      /// Axes scrolling will happen on
      /// </summary>
      public Axes scrollAxes = Axes.YAxis;
  
      /// <summary>
      /// If swipe (gesture) scrolling is enabled
      /// </summary>
      public bool allowSwipeScrolling = true;
  
      /// <summary>
      /// If mouse will is enabled, hover needs to be active
      /// </summary>
      public bool allowScrollWheel = true;
  
  	[SerializeField]
  	[HideInInspector]
  	private tk2dUILayout backgroundLayoutItem = null;
  
  	public tk2dUILayout BackgroundLayoutItem {
  		get { return backgroundLayoutItem; }
  		set {
  			if (backgroundLayoutItem != value) {
  				if (backgroundLayoutItem != null) {
  					backgroundLayoutItem.OnReshape -= LayoutReshaped;
  				}
  				backgroundLayoutItem = value;
  				if (backgroundLayoutItem != null) {
  					backgroundLayoutItem.OnReshape += LayoutReshaped;
  				}
  			}
  		}
  	}
  
  	[SerializeField]
  	[HideInInspector]
  	private tk2dUILayoutContainer contentLayoutContainer = null;
  
  	public tk2dUILayoutContainer ContentLayoutContainer {
  		get { return contentLayoutContainer; }
  		set {
  			if (contentLayoutContainer != value) {
  				if (contentLayoutContainer != null) {
  					contentLayoutContainer.OnChangeContent -= ContentLayoutChangeCallback;
  				}
  				contentLayoutContainer = value;
  				if (contentLayoutContainer != null) {
  					contentLayoutContainer.OnChangeContent += ContentLayoutChangeCallback;
  				}
  			}
  		}
  	}
  
      private bool isBackgroundButtonDown = false;
      private bool isBackgroundButtonOver = false;
  
      private Vector3 swipeScrollingPressDownStartLocalPos = Vector3.zero;
      private Vector3 swipeScrollingContentStartLocalPos = Vector3.zero;
      private Vector3 swipeScrollingContentDestLocalPos = Vector3.zero;
      private bool isSwipeScrollingInProgress = false;
      private const float SWIPE_SCROLLING_FIRST_SCROLL_THRESHOLD = .02f; //at what point swipe scrolling will start moving the list
      private const float WITHOUT_SCROLLBAR_FIXED_SCROLL_WHEEL_PERCENT = .1f; //if not scrollbar attached how much scroll wheel will move list
      private Vector3 swipePrevScrollingContentPressLocalPos = Vector3.zero;
      private float swipeCurrVelocity = 0; //velocity of current frame (used for inertia swipe scrolling)
      private float snapBackVelocity = 0;
  
      public GameObject SendMessageTarget
      {
          get
          {
              if (backgroundUIItem != null)
              {
                  return backgroundUIItem.sendMessageTarget;
              }
              else return null;
          }
          set
          {
              if (backgroundUIItem != null && backgroundUIItem.sendMessageTarget != value)
              {
                  backgroundUIItem.sendMessageTarget = value;
              
                  #if UNITY_EDITOR
                      UnityEditor.EditorUtility.SetDirty(backgroundUIItem);
                  #endif
              }
          }
      }
  
      /// <summary>
      /// If scrollable area is being scrolled
      /// </summary>
      public event System.Action<tk2dUIScrollableArea> OnScroll;
  
      public string SendMessageOnScrollMethodName = "";
  
      private float percent = 0; //0-1
  
      /// <summary>
      /// Scroll position percent 0-1
      /// </summary>
      public float Value
      {
          get { return Mathf.Clamp01( percent ); }
          set
          {
              value = Mathf.Clamp(value, 0f, 1f);
              if (value != percent)
              {
                  UnpressAllUIItemChildren();
                  percent = value;
                  if (OnScroll != null) { OnScroll(this); }
  
                  if (isBackgroundButtonDown || isSwipeScrollingInProgress) {
                      if (tk2dUIManager.Instance__NoCreate != null) {
                          tk2dUIManager.Instance.OnInputUpdate -= BackgroundOverUpdate;
                      }
                      isBackgroundButtonDown = false;
                      isSwipeScrollingInProgress = false;
                  }
  
                  TargetOnScrollCallback();
              }
              if (scrollBar != null) { scrollBar.SetScrollPercentWithoutEvent(percent); }
              SetContentPosition();
          }
      }
  
      /// <summary>
      /// Manually set scrolling percent without firing OnScroll event
      /// </summary>
      public void SetScrollPercentWithoutEvent(float newScrollPercent)
      {
          percent = Mathf.Clamp(newScrollPercent, 0f, 1f);
          UnpressAllUIItemChildren();
          if (scrollBar != null) { scrollBar.SetScrollPercentWithoutEvent(percent); }
          SetContentPosition();
      }
  
      /// <summary>
      /// Measures the content length. This isn't very fast, so if you know the content length
      /// it is often more efficient to tell it rather than asking it to measure the content.
      /// Returns the height in Unity units, of everything under the Content contentContainer.
      /// </summary>
      public float MeasureContentLength() {
          Vector3 vector3Min = new Vector3(float.MinValue, float.MinValue, float.MinValue);
          Vector3 vector3Max = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
          Vector3[] minMax = new Vector3[] {
              vector3Max,
              vector3Min
          };
          Transform t = contentContainer.transform;
          GetRendererBoundsInChildren(t.worldToLocalMatrix, minMax, t);
          if (minMax[0] != vector3Max && minMax[1] != vector3Min) {
              minMax[0] = Vector3.Min(minMax[0], Vector3.zero);
              minMax[1] = Vector3.Max(minMax[1], Vector3.zero);
              return (scrollAxes == Axes.YAxis) ? (minMax[1].y - minMax[0].y) : (minMax[1].x - minMax[0].x);
          }
          else {
              Debug.LogError("Unable to measure content length");
              return VisibleAreaLength * 0.9f;
          }
      }
  
      void OnEnable()
      {
          if (scrollBar != null)
          {
              scrollBar.OnScroll += ScrollBarMove;
          }
  
          if (backgroundUIItem != null)
          {
              backgroundUIItem.OnDown += BackgroundButtonDown;
              backgroundUIItem.OnRelease += BackgroundButtonRelease;
              backgroundUIItem.OnHoverOver += BackgroundButtonHoverOver;
              backgroundUIItem.OnHoverOut += BackgroundButtonHoverOut;
          }
  
  
gt: 		if (backgroundLayoutItem != null)