Demo链接:
一、前言
Unity版本:2020.1.x
在Unity游戏开发中,滚动视图中元素的定位是一个常见需求。为了解决这个问题,我们可以编写一个名为 "ScrollLocate" 的脚本来实现这个功能。
二、效果
三、完整代码
using UnityEngine;
using UnityEngine.UI;
public class ScrollLocate : MonoBehaviour
{
public RectTransform itemTransform;
private ScrollRect _scrollRect;
private RectTransform _contentTransform;
private RectTransform _viewportTransform;
private void Start()
{
_scrollRect = GetComponent<ScrollRect>();
_contentTransform = _scrollRect.content;
_viewportTransform = _scrollRect.viewport;
}
public void LocateTargetInViewport()
{
// 获取目标RectTransform和viewport的局部坐标边界
Vector3[] itemCorners = new Vector3[4];
itemTransform.GetWorldCorners(itemCorners);
Vector3[] viewportCorners = new Vector3[4];
_viewportTransform.GetWorldCorners(viewportCorners);
// 转换世界坐标到内容局部坐标
for (int i = 0; i < 4; i++)
{
itemCorners[i] = _contentTransform.InverseTransformPoint(itemCorners[i]);
viewportCorners[i] = _contentTransform.InverseTransformPoint(viewportCorners[i]);
}
// 获取边界值
float viewportMinX = viewportCorners[0].x;
float viewportMaxX = viewportCorners[2].x;
float viewportMinY = viewportCorners[0].y;
float viewportMaxY = viewportCorners[2].y;
float itemMinX = itemCorners[0].x;
float itemMaxX = itemCorners[2].x;
float itemMinY = itemCorners[0].y;
float itemMaxY = itemCorners[2].y;
Vector2 contentPosition = _contentTransform.anchoredPosition;
// 水平滚动处理
if (_scrollRect.horizontal)
{
if (itemMaxX > viewportMaxX) // Item超出右边界
{
float difference = itemMaxX - viewportMaxX;
contentPosition.x -= difference;
}
else if (itemMinX < viewportMinX) // Item超出左边界
{
float difference = viewportMinX - itemMinX;
contentPosition.x += difference;
}
}
// 垂直滚动处理
if (_scrollRect.vertical)
{
if (itemMaxY > viewportMaxY) // Item超出上边界
{
float difference = itemMaxY - viewportMaxY;
contentPosition.y -= difference;
}
else if (itemMinY < viewportMinY) // Item超出下边界
{
float difference = viewportMinY - itemMinY;
contentPosition.y += difference;
}
}
_contentTransform.anchoredPosition = contentPosition;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
LocateTargetInViewport();
}
}
}
有什么不对的地方或者不明白的地方可以直接问我