1、如图,在Unity场景中,我们创建了一个Scroll View,当内容滑到下面时,可通过点击Back Top按钮快速返回顶部。

2、创建控制脚本ScrollViewController.cs。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScrollViewController : MonoBehaviour
{
public ScrollRect scrollRect;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// 点击按钮,ScrollRect返回到顶部
public void BackTop()
{
scrollRect.normalizedPosition = new Vector2(0, 1);
}
}
把脚本拉到场景中,并把Scroll View组件付给脚本scrollRect变量。
Back Top按钮添加脚本中BackTop()方法为点击事件。
运行测试:

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



