本文主要是介绍Unity 滚动视图(ScrollRect)使某个元素在可视范围内(滚动到某个元素),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在使用UGUI制作滚动视图的过程中,可能会遇到需求说,要定位到其中的某个元素,把它显示在Viewport可视范围内,这要怎么做呢?话不多说,上代码:
namespace ZetanStudio.Extension
{public static class ScrollRectExtension{public static void EnsureVisibility(this ScrollRect scrollRect, RectTransform rectTransform, float? border = null){//角的顺序//1 2//0 3getBothCorners(out var ccorners/*Content的四个角*/, out var corners/*目标的四个角*/);if (ccorners[0] == corners[0] && ccorners[1] == corners[1] && ccorners[2] == corners[2] && ccorners[3] == corners[3]) return;//矩形和拖拽部分一样大,则什么也不做scrollRect.StopMovement();var vcorners = scrollRect.viewport.GetWorldCorners();//Viewport的四个角if (corners[0].y < vcorners[0].y)//如果下边被挡住了,要上移{scrollRect.content.position += new Vector3(0, vcorners[0].y - corners[0].y);getBothCorners(out ccorners, out corners);if (corners[0].y < ccorners[0].y)//如果下边超出了拖拽范围,要往上回移超出的距离{scrollRect.content.position += new Vector3(0, -(ccorners[0].y - corners[0].y));getCorners(out corners);}}if (corners[2].x > vcorners[2].x)//如果右边被挡住了,要左移{scrollRect.content.position += new Vector3(-(corners[2].x - vcorners[2].x), 0);getBothCorners(out ccorners, out corners);if (corners[2].x > ccorners[2].x)//如果右边超出了拖拽范围,要往右回移超出的距离{scrollRect.content.position += new Vector3((corners[2].x - ccorners[2].x), 0);getCorners(out corners);}}if (corners[1].y > vcorners[1].y)//如果上边被挡住了,要下移{scrollRect.content.position += new Vector3(0, -(corners[1].y - vcorners[1].y));getBothCorners(out ccorners, out corners);if (corners[1].y > ccorners[1].y)//如果上边超出了拖拽范围,要往下回移超出的距离{scrollRect.content.position += new Vector3(0, corners[1].y - ccorners[1].y);getCorners(out corners);}}if (corners[0].x < vcorners[0].x)//如果左边被挡住了,要右移{scrollRect.content.position += new Vector3(vcorners[0].x - corners[0].x, 0);getBothCorners(out ccorners, out corners);if (corners[0].x < ccorners[0].x)//如果左边超出了拖拽范围,要往左回移超出的距离scrollRect.content.position += new Vector3(-(ccorners[0].x - corners[0].x), 0);}void getBothCorners(out Vector3[] ccorners, out Vector3[] corners){ccorners = scrollRect.content.GetWorldCorners();getCorners(out corners);}void getCorners(out Vector3[] corners){corners = rectTransform.GetWorldCorners();if (border.HasValue){corners[0] = new Vector3(corners[0].x - border.Value, corners[0].y - border.Value);corners[1] = new Vector3(corners[1].x - border.Value, corners[1].y + border.Value);corners[2] = new Vector3(corners[2].x + border.Value, corners[2].y + border.Value);corners[3] = new Vector3(corners[3].x + border.Value, corners[3].y - border.Value);}}}}
}
这篇关于Unity 滚动视图(ScrollRect)使某个元素在可视范围内(滚动到某个元素)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!