滚动到制定位置

            ScrollRect scrollRect = GetComponentInChildren<ScrollRect>();
            //纵向
            scrollRect.verticalNormalizedPosition = 1f; //显示顶部
            //横向
            scrollRect.horizontalNormalizedPosition = 0;//显示最左侧

1 scrollRect是 scrollview的自带组件
2 值的范围是0-1,0是底部或左侧,1是顶部或右侧
3 如果出现创建新物体后再设置显示最后行,但是显示的却是倒数第二个物体,尝试写个协程,显示最后一行的代码下一帧执行

检测元素是否在viewPort可视范围内

public RectTransform viewPort;
public Transform item;
private void Update()
{
	Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(viewPort,item);
	bool isUpOut = bounds.max.y>0|| bounds.max.y <-viewPort.rect.height;
	bool isDownOut = bounds.min.y>0|| bounds.min.y <-viewPort.rect.height;
	if(isUpOut&& isDownOut) print("元素不在视野内");
}

RectTransformUtility.CalculateRelativeRectTransformBounds 计算的是元素到viewPort的距离

bounds.max是元素的上边缘到viewPort上边缘的距离

bounds.min是元素的下边缘到viewPort上边缘的距离 都是与上边缘的距离

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐