我有一个无限的垂直滚动条,当达到一个(可变)阈值时,我想突然限制滚动(只在一个方向上)。
public GameObject MyScrollRectContent;
public float limit = 300;
void Update () {
if(MyScrollRectContent.transform.localPosition.y >= limit){
//make it ONLY possible to scroll backwards or not beyond the limit and stop elasticity
}有什么办法限制无限滚动吗?
发布于 2018-03-12 16:29:31
最简单的事情就是使用现有的ScrollRect。它允许在定义的矩形中滚动。你将不得不在一个方向上设置一个非常大的尺寸来模拟无限大,或者找到一种方法来在用户走得太远时无缝地重置位置(很难在看不到跳转的情况下做到这一点,但根据你的内容可能是可能的)。
如果这不是一个可接受的解决方案:
要限制滚动,您可以在用户走得太高时将位置设置为限制:
void Update ()
{
if(MyScrollRectContent.transform.localPosition.y >= limit)
{
var currentPos = MyScrollRectContent.transform.localPosition;
MyScrollRectContent.transform.localPosition = new Vector3(currentPos.x, limit, currentPos.z);
}
}最重要的是,如果你想要弹性,那就有点棘手了:在某个地方,你的脚本必须当前设置MyScrollRectContent.transform.localPosition。与其这样做,不如设置一个targetYPosition。您可以将其想象为一个可移动的点,在该点上滚动条由弹性件连接。此targetYPosition受您的限制限制。然后,在您的更新中,让ScrollRectContent跟随。
public void OnUserInput(float y) // I don't know how you actually do this. this is an example
{
_targetYposition = y;
if(_targetYposition > limit)
{
_targetYPosition = limit;
}
}
private void Update()
{
var currentPos = MyScrollRectContent.transform.localPosition;
var y =
Vector3.Lerp(currentPos.y,
_targetYPosition, Time.deltaTime*_speed);
MyScrollRectContent.transform.localPosition = new Vector3(currentPos.x, y, currentPos.z);
}请注意,这是一个简单的示例,旨在提示您正确的方向。您必须使其适应您的代码。弹性可能也不会像您想要的那样,因此您可能需要修改Update函数以创建其他效果
https://stackoverflow.com/questions/49228321
复制相似问题