Advertisement
zero3growlithe

UIScrollToSelection [Unity 3D script]

Apr 10th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6.  
  7. public class UIScrollToSelection : MonoBehaviour {
  8.  
  9. //*** ATTRIBUTES ***//
  10.     [Header("[ Settings ]")]
  11.     [SerializeField]
  12.     private ScrollType scrollDirection;
  13.     [SerializeField]
  14.     private float scrollSpeed = 10f;
  15.  
  16.     [Header("[ Input ]")]
  17.     [SerializeField]
  18.     private bool cancelScrollOnInput = false;
  19.     [SerializeField]
  20.     private List<KeyCode> cancelScrollKeycodes = new List<KeyCode>();
  21.  
  22. //*** PROPERTIES ***//
  23.     // REFERENCES
  24.     protected RectTransform LayoutListGroup {
  25.         get {return TargetScrollRect != null ? TargetScrollRect.content : null;}
  26.     }
  27.  
  28.     // SETTINGS
  29.     protected ScrollType ScrollDirection {
  30.         get {return scrollDirection;}
  31.     }
  32.     protected float ScrollSpeed {
  33.         get {return scrollSpeed;}
  34.     }
  35.  
  36.     // INPUT
  37.     protected bool CancelScrollOnInput {
  38.         get {return cancelScrollOnInput;}
  39.     }
  40.     protected List<KeyCode> CancelScrollKeycodes {
  41.         get {return cancelScrollKeycodes;}
  42.     }
  43.  
  44.     // CACHED REFERENCES
  45.     protected RectTransform ScrollWindow {get; set;}
  46.     protected ScrollRect TargetScrollRect {get; set;}
  47.  
  48.     // SCROLLING
  49.     protected EventSystem CurrentEventSystem {
  50.         get {return EventSystem.current;}
  51.     }
  52.     protected GameObject LastCheckedGameObject {get; set;}
  53.     protected GameObject CurrentSelectedGameObject {
  54.         get {return EventSystem.current.currentSelectedGameObject;}
  55.     }
  56.     protected RectTransform CurrentTargetRectTransform {get; set;}
  57.     protected bool IsManualScrollingAvailable {get; set;}
  58.  
  59. //*** METHODS - PUBLIC ***//
  60.  
  61.  
  62. //*** METHODS - PROTECTED ***//
  63.     protected virtual void Awake (){
  64.         TargetScrollRect = GetComponent<ScrollRect>();
  65.         ScrollWindow = TargetScrollRect.GetComponent<RectTransform>();
  66.     }
  67.  
  68.     protected virtual void Start (){
  69.  
  70.     }
  71.    
  72.     protected virtual void Update (){
  73.         UpdateReferences();
  74.         CheckIfScrollingShouldBeLocked();
  75.         ScrollRectToLevelSelection();
  76.     }
  77.  
  78. //*** METHODS - PRIVATE ***//
  79.     private void UpdateReferences (){
  80.         // update current selected rect transform
  81.         if (CurrentSelectedGameObject != LastCheckedGameObject){
  82.             CurrentTargetRectTransform = (CurrentSelectedGameObject != null) ?
  83.                 CurrentSelectedGameObject.GetComponent<RectTransform>() :
  84.                 null;
  85.  
  86.             // unlock automatic scrolling
  87.             if (CurrentSelectedGameObject != null &&
  88.                 CurrentSelectedGameObject.transform.parent == LayoutListGroup.transform)
  89.             {
  90.                 IsManualScrollingAvailable = false;
  91.             }
  92.         }
  93.  
  94.         LastCheckedGameObject = CurrentSelectedGameObject;
  95.     }
  96.  
  97.     private void CheckIfScrollingShouldBeLocked (){
  98.         if (CancelScrollOnInput == false || IsManualScrollingAvailable == true){
  99.             return;
  100.         }
  101.  
  102.         for (int i = 0; i < CancelScrollKeycodes.Count; i++){
  103.             if (Input.GetKeyDown(CancelScrollKeycodes[i]) == true){
  104.                 IsManualScrollingAvailable = true;
  105.  
  106.                 break;
  107.             }
  108.         }
  109.     }
  110.  
  111.     private void ScrollRectToLevelSelection (){
  112.         // check main references
  113.         bool referencesAreIncorrect = (TargetScrollRect == null || LayoutListGroup == null || ScrollWindow == null);
  114.  
  115.         if (referencesAreIncorrect == true || IsManualScrollingAvailable == true){
  116.             return;
  117.         }
  118.  
  119.         RectTransform selection = CurrentTargetRectTransform;
  120.  
  121.         // check if scrolling is possible
  122.         if (selection == null || selection.transform.parent != LayoutListGroup.transform){
  123.             return;
  124.         }
  125.  
  126.         // depending on selected scroll direction move the scroll rect to selection
  127.         switch (ScrollDirection){
  128.             case ScrollType.VERTICAL:
  129.                 UpdateVerticalScrollPosition(selection);
  130.                 break;
  131.             case ScrollType.HORIZONTAL:
  132.                 UpdateHorizontalScrollPosition(selection);
  133.                 break;
  134.             case ScrollType.BOTH:
  135.                 UpdateVerticalScrollPosition(selection);
  136.                 UpdateHorizontalScrollPosition(selection);
  137.                 break;
  138.         }
  139.     }
  140.  
  141.     private void UpdateVerticalScrollPosition (RectTransform selection){
  142.         // move the current scroll rect to correct position
  143.         float selectionPosition = -selection.anchoredPosition.y;
  144.  
  145.         float elementHeight = selection.rect.height;
  146.         float maskHeight = ScrollWindow.rect.height;
  147.         float listAnchorPosition = LayoutListGroup.anchoredPosition.y;
  148.  
  149.         // get the element offset value depending on the cursor move direction
  150.         float offlimitsValue = GetScrollOffset(selectionPosition, listAnchorPosition, elementHeight, maskHeight);
  151.  
  152.         // move the target scroll rect
  153.         TargetScrollRect.verticalNormalizedPosition +=
  154.             (offlimitsValue / LayoutListGroup.rect.height) * Time.deltaTime * scrollSpeed;
  155.     }
  156.  
  157.     private void UpdateHorizontalScrollPosition (RectTransform selection){
  158.         // move the current scroll rect to correct position
  159.         float selectionPosition = selection.anchoredPosition.x;
  160.  
  161.         float elementWidth = selection.rect.width;
  162.         float maskWidth = ScrollWindow.rect.width;
  163.         float listAnchorPosition = -LayoutListGroup.anchoredPosition.x;
  164.  
  165.         // get the element offset value depending on the cursor move direction
  166.         float offlimitsValue = -GetScrollOffset(selectionPosition, listAnchorPosition, elementWidth, maskWidth);
  167.  
  168.         // move the target scroll rect
  169.         TargetScrollRect.horizontalNormalizedPosition +=
  170.             (offlimitsValue / LayoutListGroup.rect.width) * Time.deltaTime * scrollSpeed;
  171.     }
  172.  
  173.     private float GetScrollOffset (float position, float listAnchorPosition, float targetLength, float maskLength){
  174.         if (position < listAnchorPosition){
  175.             return listAnchorPosition - position;
  176.         } else if (position + targetLength > listAnchorPosition + maskLength){
  177.             return (listAnchorPosition + maskLength) - (position + targetLength);
  178.         }
  179.  
  180.         return 0;
  181.     }
  182.  
  183. //*** ENUMS ***//
  184.     public enum ScrollType {
  185.         VERTICAL,
  186.         HORIZONTAL,
  187.         BOTH
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement