Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- [RequireComponent(typeof(ScrollRect))]
- public class MouseScrollFix : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
- {
- ScrollRect scrollRect;
- RectTransform content;
- float scrollSensitivity, step, viewportSize;
- bool isOver;
- private void Start()
- {
- scrollRect = GetComponent<ScrollRect>();
- content = scrollRect.content;
- scrollSensitivity = scrollRect.scrollSensitivity;
- viewportSize = scrollRect.viewport.rect.height;
- isOver = false;
- }
- private void Update()
- {
- if (isOver)
- {
- var d = Input.GetAxis("Mouse ScrollWheel");
- if (d > 0f)
- {
- step = Mathf.Abs(scrollSensitivity / (content.sizeDelta.y - viewportSize));
- scrollRect.verticalNormalizedPosition += step;
- if (scrollRect.verticalNormalizedPosition > 1)
- scrollRect.verticalNormalizedPosition = 1;
- }
- else if (d < 0f)
- {
- step = Mathf.Abs(scrollSensitivity / (content.sizeDelta.y - viewportSize));
- scrollRect.verticalNormalizedPosition -= step;
- if (scrollRect.verticalNormalizedPosition < 0)
- scrollRect.verticalNormalizedPosition = 0;
- }
- }
- }
- public void OnPointerEnter(PointerEventData eventData)
- {
- isOver = true;
- }
- public void OnPointerExit(PointerEventData eventData)
- {
- isOver = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement