Advertisement
Guest User

Untitled

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