using System; using System.Collections; using System.Collections.Generic; using System.Reflection; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class PageTurning : MonoBehaviour, IPointerClickHandler,IPointerEnterHandler,IPointerExitHandler { public Scrollbar slider; //检测鼠标是否在此UI上的布尔值 public bool Isin; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { ChangePage(); MouseWheel(); } //让此页面的滚动跟随右侧的滑动条 void ChangePage() { if (this.transform.childCount != 0) { this.GetComponent().padding.top = Convert.ToInt32(Math.Floor(-slider.value * (this.transform.childCount * this.transform.GetChild(0).GetComponent().sizeDelta.y - this.GetComponent().sizeDelta.y))); LayoutRebuilder.ForceRebuildLayoutImmediate(this.GetComponent()); } } public void OnPointerClick(PointerEventData pointerEventData) { } public void OnPointerEnter(PointerEventData pointerEventData) { Isin = true; } public void OnPointerExit(PointerEventData pointerEventData) { Isin = false; } void MouseWheel() { if (Isin) { slider.value += Input.GetAxis("Mouse ScrollWheel")*0.01f; } if (slider.value > 1) { slider.value = 1; } if (slider.value < 0) { slider.value = 0; } } }