zero3growlithe

[Unity] UIScrollToSelection.cs

Oct 10th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using UnityEngine.EventSystems;
  5.  
  6. public class UIScrollToSelection : MonoBehaviour {
  7.  
  8. //*** ATTRIBUTES ***//
  9.     [Header("[ Settings ]")]
  10.     [SerializeField]
  11.     private float scrollSpeed = 10f;
  12.  
  13. //*** PROPERTIES ***//
  14.     // REFERENCES
  15.     protected RectTransform LayoutListGroup {
  16.         get {return TargetScrollRect != null ? TargetScrollRect.content : null;}
  17.     }
  18.  
  19.     // SETTINGS
  20.     protected float ScrollSpeed {
  21.         get {return scrollSpeed;}
  22.     }
  23.  
  24.     // VARIABLES
  25.     protected RectTransform TargetScrollObject {get; set;}
  26.     protected RectTransform ScrollWindow {get; set;}
  27.     protected ScrollRect TargetScrollRect {get; set;}
  28.  
  29. //*** METHODS - PUBLIC ***//
  30.  
  31.  
  32. //*** METHODS - PROTECTED ***//
  33.     protected virtual void Awake (){
  34.         TargetScrollRect = GetComponent<ScrollRect>();
  35.         ScrollWindow = TargetScrollRect.GetComponent<RectTransform>();
  36.     }
  37.  
  38.     protected virtual void Start (){
  39.  
  40.     }
  41.    
  42.     protected virtual void Update (){
  43.         ScrollRectToLevelSelection();
  44.     }
  45.  
  46. //*** METHODS - PRIVATE ***//
  47.     private void ScrollRectToLevelSelection (){
  48.         // check main references
  49.         bool referencesAreIncorrect = (TargetScrollRect == null || LayoutListGroup == null || ScrollWindow == null);
  50.  
  51.         if (referencesAreIncorrect == true){
  52.             return;
  53.         }
  54.  
  55.         // get calculation references
  56.         EventSystem events = EventSystem.current;
  57.         RectTransform selection =
  58.             events.currentSelectedGameObject != null ?
  59.             events.currentSelectedGameObject.GetComponent<RectTransform>() :
  60.             null;
  61.  
  62.         // check if scrolling is possible
  63.         if (selection == null ||
  64.             selection.transform.parent != LayoutListGroup.transform)
  65.         {
  66.             return;
  67.         }
  68.  
  69.         // move the current scroll rect to correct position
  70.         float selectionPos = -selection.anchoredPosition.y;
  71.  
  72.         float elementHeight = LayoutListGroup.rect.height / LayoutListGroup.transform.childCount;
  73.         float maskHeight = ScrollWindow.rect.height;
  74.         float listPixelAnchor = LayoutListGroup.anchoredPosition.y;
  75.  
  76.         // get the element offset value depending on the cursor move direction
  77.         float offlimitsValue = 0;
  78.  
  79.         if (selectionPos < listPixelAnchor){
  80.             offlimitsValue = listPixelAnchor - selectionPos;
  81.         } else if (selectionPos + elementHeight > listPixelAnchor + maskHeight){
  82.             offlimitsValue = (listPixelAnchor + maskHeight) - (selectionPos + elementHeight);
  83.         }
  84.  
  85.         // move the target scroll rect
  86.         TargetScrollRect.verticalNormalizedPosition +=
  87.             (offlimitsValue / LayoutListGroup.rect.height) * Time.deltaTime * scrollSpeed;
  88.        
  89.         // save last object we were "heading to" to prevent blocking
  90.         TargetScrollObject = selection;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment