Advertisement
Guest User

Untitled

a guest
Nov 18th, 2014
5,266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. [RequireComponent(typeof (ScrollRect))]
  2. public class ScrollRectEnsureVisible : MonoBehaviour
  3. {
  4.     public float AnimTime = 0.15f;
  5.     public bool Log = true;
  6.     public RectTransform MaskTransform;
  7.  
  8.     private RectTransform _content;
  9.     private ScrollRect _sr;
  10.  
  11.     private void Awake()
  12.     {
  13.         _sr = GetComponent<ScrollRect>();
  14.         _content = _sr.content;
  15.     }
  16.  
  17.     public void CenterOnItem(RectTransform target)
  18.     {
  19.         if (Log) Debug.Log("Updating scrollrect for item: " + target);
  20.  
  21.         //this is the center point of the visible area
  22.         var maskHalfSize = MaskTransform.rect.size*0.5f;
  23.         var contentSize = _content.rect.size;
  24.         //get object position inside content
  25.         var targetRelativePosition =
  26.             _content.InverseTransformPoint(target.position);
  27.         //adjust for item size
  28.         targetRelativePosition += new Vector3(target.rect.size.x, target.rect.size.y, 0f)*0.25f;
  29.         //get the normalized position inside content
  30.         var normalizedPosition = new Vector2(
  31.             Mathf.Clamp01(targetRelativePosition.x/(contentSize.x - maskHalfSize.x)),
  32.             1f - Mathf.Clamp01(targetRelativePosition.y/-(contentSize.y - maskHalfSize.y))
  33.             );
  34.         //we want the position to be at the middle of the visible area
  35.         //so get the normalized center offset based on the visible area width and height
  36.         var normalizedOffsetPosition = new Vector2(maskHalfSize.x / contentSize.x, maskHalfSize.y / contentSize.y);
  37.         //and apply it
  38.         normalizedPosition.x -= (1f -normalizedPosition.x)*normalizedOffsetPosition.x;
  39.         normalizedPosition.y += normalizedPosition.y*normalizedOffsetPosition.y;
  40.  
  41.         normalizedPosition.x = Mathf.Clamp01(normalizedPosition.x);
  42.         normalizedPosition.y = Mathf.Clamp01(normalizedPosition.y);
  43.  
  44.         if (Log)
  45.             Debug.Log(string.Format(
  46.                 @"Target normalized position [{3}]
  47. Mask half size [{0}]
  48. Content size: [{1}]
  49. Target relative position [{2}]",
  50.                 maskHalfSize,
  51.                 contentSize,
  52.                 targetRelativePosition,
  53.                 normalizedPosition
  54.                 ));
  55.  
  56.         //_sr.normalizedPosition = normalizedPosition;
  57.         Go.to(_sr, AnimTime, new GoTweenConfig().vector2Prop("normalizedPosition", normalizedPosition));
  58.     }
  59.  
  60.     /// <summary>
  61.     /// Takes a float value from a [0f,1f] range and translates it to a [-1f,1f] range
  62.     /// </summary>
  63.     /// <param name="value"></param>
  64.     /// <returns></returns>
  65.     float Transtale01RangeToMinus11Range(float value)
  66.     {
  67.         return (value + ((1f - value)*-1f));
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement