2025年检测鼠标滚轮控制ScrollView滚动

检测鼠标滚轮控制ScrollView滚动using System Collections using System Collections Generic using UnityEngine using UnityEngine UI public class ScrollViewCt MonoBehaviou ScrollRect

大家好,我是讯享网,很高兴认识大家。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScrollViewCtrl : MonoBehaviour { 
    ScrollRect _ScrollRect; RectTransform _Content; private float _Speed = 35; // Start is called before the first frame update void Start() { 
    _ScrollRect = transform.GetComponent<ScrollRect>(); } // Update is called once per frame void Update() { 
    float mouseCenter = Input.GetAxis("Mouse ScrollWheel"); //Debug.Log("mouseCenter:" + mouseCenter); if (mouseCenter == 0) { 
    return; } //鼠标是否在ui内 if (RectTransformUtility.RectangleContainsScreenPoint(transform.GetComponent<RectTransform>(),Input.mousePosition)) { 
    //Debug.Log("在遮挡范围内"); OnScroll(Input.mouseScrollDelta); } } /// <summary> /// 用Unity源码改写的组件滚动操作 /// </summary> /// <param name="vector2"></param> public virtual void OnScroll(Vector2 vector2) { 
    Vector2 delta = vector2; // Down is positive for scroll events, while in UI system up is positive. delta.y *= -_Speed; if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y)) delta.y = delta.x; delta.x = 0; Vector2 position = _ScrollRect.content.anchoredPosition; position += delta * _ScrollRect.scrollSensitivity; _ScrollRect.content.anchoredPosition = position; } } 

讯享网
小讯
上一篇 2025-03-29 15:13
下一篇 2025-03-12 17:56

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/122039.html