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(); if (this.transform.childCount == 0) { slider.size = 1; } else { ChangeScollbarLong(); } } //让此页面的滚动跟随右侧的滑动条 void ChangePage() { if (this.transform.childCount != 0) { float all=0; for (int n = 0; n < this.transform.childCount; n++) { if (n != 0) { all += this.GetComponent().spacing; } all += this.transform.GetChild(n).GetComponent().sizeDelta.y; } //Debug.Log(all); if (all < this.transform.GetComponent().sizeDelta.y) { return; } this.GetComponent().padding.top = Convert.ToInt32(Math.Floor(-slider.value * (all - this.transform.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.06f; } if (slider.value > 1) { slider.value = 1; } if (slider.value < 0) { slider.value = 0; } } void ChangeScollbarLong() { float all = 0; for (int n = 0; n < this.transform.childCount; n++) { all += this.transform.GetChild(n).GetComponent().sizeDelta.y; } if (all < this.transform.GetComponent().sizeDelta.y) { slider.size = 1; } else { slider.size = this.transform.GetComponent().sizeDelta.y/all; } } }