using UnityEngine;
using UnityEngine.UI;
///
/// 持续按住
///
public class ScrollViewHorizontalDownMove : MonoBehaviour
{
ScrollRect scrollRect; // 引用ScrollView组件
public float scrollSpeed = 0.5f; // 控制滚动的速度
private bool isScrollingLeft = false; // 是否正在向左滚动
private bool isScrollingRight = false; // 是否正在向右滚动
private void Start()
{
scrollRect = GetComponent();
}
// 检查按钮是否被按下,并设置相应的滚动标志
public void CheckLeftButton()
{
isScrollingLeft = true;
}
public void UncheckLeftButton()
{
isScrollingLeft = false;
}
public void CheckRightButton()
{
isScrollingRight = true;
}
public void UncheckRightButton()
{
isScrollingRight = false;
}
void Update()
{
// 根据按钮的按下状态来移动ScrollView
if (isScrollingLeft)
{
ScrollLeft();
}
else if (isScrollingRight)
{
ScrollRight();
}
}
// 滚动ScrollView到左边
private void ScrollLeft()
{
float newPosition = scrollRect.horizontalNormalizedPosition - (scrollSpeed * Time.deltaTime);
newPosition = Mathf.Clamp(newPosition, 0f, 1f); // 确保位置在有效范围内
scrollRect.horizontalNormalizedPosition = newPosition;
}
// 滚动ScrollView到右边
private void ScrollRight()
{
float newPosition = scrollRect.horizontalNormalizedPosition + (scrollSpeed * Time.deltaTime);
newPosition = Mathf.Clamp(newPosition, 0f, 1f); // 确保位置在有效范围内
scrollRect.horizontalNormalizedPosition = newPosition;
}
}