【Unity-UGUI】ScrollView自动定位到指定的Item

前言

最近遇到了这样的需求:滑动列表自动定位到第一个未领取的奖励,翻译一下其实就是ScrollView的自动定位问题。

思路

ScrollView的滑动、定位问题一般都与ScrollRect.normalizedPosition这个字段有关,这个字段描述了ScrollView组件下Content(内容)和Viewport(视窗)的相对位置,滑动、定位问题本质上就是寻找新的normalizedPosition的问题。

Unity官方对这个字段的描述:
The scroll position as a Vector2 between (0,0) and (1,1) with (0,0) being the lower left corner.

normalizedPosition的计算方法:ScrollRect.normalizedPosition+偏差量(根据目标Item位置和当前viewport位置进行计算,详细见代码)

代码

using UnityEngine;
using UnityEngine.UI;

public class ScrollEx : MonoBehaviour
{
    private ScrollRect scrollRect;
    private RectTransform viewport;
    private RectTransform content;

    [SerializeField][Header("要定位到的目标物体")]
    private RectTransform targetItem;

    void Start() {

        if (scrollRect == null) scrollRect = this.GetComponent<ScrollRect>();
        if (viewport == null) viewport = this.transform.Find("Viewport").GetComponent<RectTransform>();
        if (content == null) content = this.transform.Find("Viewport/Content").GetComponent<RectTransform>();

        //如果content上面加了Layout和content_fitter组件就需要加上这一句,让content自适应在计算之前进行
        LayoutRebuilder.ForceRebuildLayoutImmediate(content.GetComponent<RectTransform>());
        Nevigate(targetItem);
    }

    public void Nevigate(RectTransform item) {
        //算出content需要偏移的量
        Vector3 itemCurrentLocalPostion = scrollRect.GetComponent<RectTransform>().InverseTransformVector(ConvertLocalPosToWorldPos(viewport));
        Vector3 itemTargetLocalPos = scrollRect.GetComponent<RectTransform>().InverseTransformVector(ConvertLocalPosToWorldPos(item));
        Vector3 diff = itemTargetLocalPos - itemCurrentLocalPostion;
        diff.z = 0.0f;
        var difPos = new Vector2(
            diff.x / (content.GetComponent<RectTransform>().rect.width - viewport.rect.width),
            diff.y / (content.GetComponent<RectTransform>().rect.height - viewport.rect.height)
        );
        var newNormalizedPos = scrollRect.GetComponent<ScrollRect>().normalizedPosition + difPos;
        newNormalizedPos = new Vector2(Mathf.Clamp01(newNormalizedPos.x), Mathf.Clamp01(newNormalizedPos.y));
        scrollRect.GetComponent<ScrollRect>().normalizedPosition = newNormalizedPos;
    }

    private Vector3 ConvertLocalPosToWorldPos(RectTransform target) {
    	//由于RectTransform的localPos受到pivot的影响,所以需要先计算偏差进行还原
        var pivotOffset = new Vector3(
            (0.5f - target.pivot.x) * target.rect.size.x,
            (0.5f - target.pivot.y) * target.rect.size.y,
            0f);

        var localPosition = target.localPosition + pivotOffset;

        return target.parent.TransformPoint(localPosition);
    }
}

代码里的计算思路可以用简单的方式验证其正确,我们简单地从y轴进行考虑,并利用normalizedPos的定义,就可以验证正确。
在这里插入图片描述

附加问题

Q:为什么在这里,我们要使用ConvertLocalPosToWorldPos获取对应UI元素的世界坐标,而不是通过RectTransform.position或者Transform.position来获取世界坐标?

A:我们使用方法获取到的世界坐标,是UI真正轴心点的坐标,而后面两个其实指的都是UI pivot的世界坐标(是的,即便是Transform.position在UI这里指的也是pivot的世界坐标)。

要实现Unity UGUI中的ScrollView滑动居中放大,其他的缩小,可以按照以下步骤进行操作: 1. 创建一个ScrollView,用于显示内容,并设置合适的大小和位置。 2. 在ScrollView中创建一个Content对象,用于放置所有需要显示的子对象,并设置Layout Group组件,以确保内容按照一定的布局排列。 3. 在每个子对象上添加一个自定义的脚本,用于控制子对象的缩放和位置。脚本中需要包含以下几个要点: a. 监听ScrollView的滑动事件,获取当前的滑动位置。 b. 根据当前滑动位置,计算每个子对象在滑动过程中应该设置的缩放比例。例如,距离居中的子对象应该更大,而距离边缘的子对象应该更小。 c. 根据计算得到的缩放比例,分别对每个子对象进行缩放设置。可以使用RectTransform的scale属性来实现缩放功能。 d. 根据子对象的缩放比例和位置信息,将子对象移动到ScrollView的合适位置。可以使用RectTransform的anchoredPosition属性来实现位置调整。 e. 可以根据需要,在脚本中添加其他的功能,例如点击子对象时的反应等。 4. 将自定义的脚本添加到所有的子对象上,确保每个子对象都能根据滑动进行缩放和位置调整。 通过以上步骤,我们可以实现在Unity UGUI中的ScrollView滑动过程中,距离居中的子对象放大,而距离边缘的子对象缩小的效果。具体的缩放比例和位置调整可以根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值