Advertisement
Guest User

Untitled

a guest
Sep 29th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6.  
  7. [RequireComponent(typeof(ScrollRect))]
  8. public class MouseScrollFix : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  9. {
  10.     ScrollRect scrollRect;
  11.     RectTransform content;
  12.     float scrollSensitivity, step, viewportSize;
  13.     bool isOver;
  14.  
  15.     private void Start()
  16.     {
  17.         scrollRect = GetComponent<ScrollRect>();
  18.         content = scrollRect.content;
  19.         scrollSensitivity = scrollRect.scrollSensitivity;
  20.         viewportSize = scrollRect.viewport.rect.height;
  21.         isOver = false;
  22.     }
  23.  
  24.     private void Update()
  25.     {
  26.         if (isOver)
  27.         {
  28.             var d = Input.GetAxis("Mouse ScrollWheel");
  29.  
  30.             if (d > 0f)
  31.             {
  32.                 step = Mathf.Abs(scrollSensitivity / (content.sizeDelta.y - viewportSize));
  33.                 scrollRect.verticalNormalizedPosition += step;
  34.  
  35.                 if (scrollRect.verticalNormalizedPosition > 1)
  36.                     scrollRect.verticalNormalizedPosition = 1;
  37.             }
  38.             else if (d < 0f)
  39.             {
  40.                 step = Mathf.Abs(scrollSensitivity / (content.sizeDelta.y - viewportSize));
  41.                 scrollRect.verticalNormalizedPosition -= step;
  42.  
  43.                 if (scrollRect.verticalNormalizedPosition < 0)
  44.                     scrollRect.verticalNormalizedPosition = 0;
  45.             }
  46.         }
  47.     }
  48.  
  49.     public void OnPointerEnter(PointerEventData eventData)
  50.     {
  51.         isOver = true;
  52.     }
  53.  
  54.     public void OnPointerExit(PointerEventData eventData)
  55.     {
  56.         isOver = false;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement