首页 > 其他分享 >随笔-Unity中的ScrollView如何跳转到指定的Item位置

随笔-Unity中的ScrollView如何跳转到指定的Item位置

时间:2024-03-18 17:29:48浏览次数:13  
标签:normalizedPosition target scrollRect ScrollView Item newNormalizedPosition 跳转 re

         在我们平时开发滑动列表的UI时,虽然UGUI的ScrollView并不是很好用,但是有时一些非常简单的列表我们没有必要加入一些很复杂的列表插件,用简单的ScrollView就可以完成我们的需求。

        我们可以通过计算列表中有多少个Item,再利用ScrollView中Content的长度来计算Item的具体位置,得到位置之后直接调整ScrollView中normalizedPosition的值做到跳转的目的。但是每次都计算显得麻烦,也不美观,我们可以把这套计算写成扩展方法。

        直接贴上代码:整个计算方式很简单,代码量也很少,计算方式就像上面说的一样。完成扩展方法之后只需要像这样直接调用,传入想要跳转位置的item的RectTransform即可:scrollView.normalizedPosition = scrollView.UpdateTargetPos(item.rect);

using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 扩展
/// </summary>
public static class Extension
{
    public static Vector2 UpdateTargetPos(this ScrollRect scrollRect, RectTransform item)
    {
        Vector3 itemCurrentLocalPostion = scrollRect.GetComponent<RectTransform>().InverseTransformVector(ConvertLocalPosToWorldPos(item));
        Vector3 itemTargetLocalPos = scrollRect.GetComponent<RectTransform>().InverseTransformVector(ConvertLocalPosToWorldPos(scrollRect.viewport));

        Vector3 diff = itemTargetLocalPos - itemCurrentLocalPostion;
        diff.z = 0.0f;

        var newNormalizedPosition = new Vector2(
            diff.x / (scrollRect.content.rect.width - scrollRect.viewport.rect.width),
            diff.y / (scrollRect.content.rect.height - scrollRect.viewport.rect.height)
            );

        newNormalizedPosition = scrollRect.normalizedPosition - newNormalizedPosition;

        newNormalizedPosition.x = Mathf.Clamp01(newNormalizedPosition.x);
        newNormalizedPosition.y = Mathf.Clamp01(newNormalizedPosition.y);

        //有DOTween时使用
        //DOTween.To(() => scrollRect.normalizedPosition, x => scrollRect.normalizedPosition = x, newNormalizedPosition, 0.8f);
        //无DOTween时使用
        //scrollRect.normalizedPosition = newNormalizedPosition;

        return newNormalizedPosition;
    }

    private static Vector3 ConvertLocalPosToWorldPos(RectTransform target)
    {
        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);
    }

}

标签:normalizedPosition,target,scrollRect,ScrollView,Item,newNormalizedPosition,跳转,re
From: https://blog.csdn.net/zhangchong5522/article/details/136803580

相关文章

  • C#上位机登录界面设计-界面跳转(二)
        C#上位机登录界面设计(一)是讲述的如何设计自己的登录界面,下一步是设计如何登入主界面,通过程序,判断用户的账号和密码是否正确,正确,则进入主界面,错误,弹出错误信息。C#上位机登录界面设计-界面设计(一)一、登录按键    具体需要实现的功能:登陆时,密码账号正确显......
  • vue router-view 路由跳转时,页面如何滚动到顶部
    在Vue中使用路由进行页面切换后,我们可以通过设置scrollBehavior来控制页面的滚动位置。importVuefrom'vue'importRouterfrom'vue-router'//导入组件importHomePagefrom'./components/HomePage.vue'importAboutPagefrom'./components/AboutPage.vue'......
  • Javaweb项目使用本地servlet启动,可以弹出主页,跳转到controller报404解决方案
    首先检查项目的资源路径,以及tomcat配置,有没有部署,上下文配置好如果问题依然出现,那么可以考虑tomcat版本与依赖不匹配,我用的是tomcat10,使用使用这个依赖,就解决了这个问题,jakarta.servletjakarta.servlet-api5.0.0provided,相应的匹配版本可以查询到。......
  • linux 环境下嵌入式开发vim 工具编辑跳转
    参考资料:https://www.cnblogs.com/dpf-learn/p/14326000.htmlvim安装nerdtreenerdtree效果安装ctagsaptinstallexuberant-ctagsyuminstallctags用法接着,在源文件目录树(这里是在/home/ballack/test/目录下)执行如下命令:ctags-R.其他编辑器配置在Linux下用VI编......
  • QT 自定义QGraphicsItem 缩放后旋转 图形出现漂移问题
    实现自定义QGraphicsItem缩放和旋转时,遇到了这样一个问题:将item旋转一个角度,然后拖拽放大,再次进行旋转时图像会发生漂移。原本以为是放大后中心点位置没有改变,导致旋转时以原中心的旋转出现了偏移,但是重新设置旋转中心setTransformOriginPoint(rect.center());并没有起作用,图像......
  • 完美解决浏览器输入http被自动跳转至https问题
    查阅相关资料,发现这是浏览器的HSTS(HTTPStrictTransportSecurity)功能引起的。在安装配置SSL证书时,可以使用一种能使数据传输更加安全的Web安全协议,即在服务器端上开启HSTS,它会告诉浏览器只能通过HTTPS访问,而绝对禁止HTTP方式。因此,只要关闭浏览器的HSTS功能就可以解决这个问题......
  • 【WEEK2】 【DAY5】数据处理及跳转之数据处理【中文版】
    2024.3.8Friday接上文【WEEK2】【DAY4】数据处理及跳转之结果跳转方式【中文版】目录5.2.数据处理5.2.1.提交处理数据5.2.1.1.提交的域名称和处理方法的参数名一致5.2.1.2.提交的域名称和处理方法的参数名不一致5.2.1.3.实例:新建文件1.UserController.java2.运......
  • 解决表格文件上传无法删除临时文件的问题Failed to perform cleanup of multipart ite
    java.io.UncheckedIOException:CannotdeleteC:\Users\hasee\AppData\Local\Temp\tomcat如图所示,刚开始以为是apifox没删除的问题,换了之后依旧这样 尝试方案1-失败 方法二-失败 方法三-成功 原文链接报错:StandardServletMultipartResolver:Failedtoperform......
  • ItemsControl 控件
    一、ItemsControl简介   ItemsControl是用来表示一些条目集合的控件,它的成员是一些其它控件的集合。   其继承关系如下:   在这里插入图片描述   其常用的派生控件为:ListBox、ListView、ComboBox,为ItemsControl的具体实现。   ItemsControl的成员条目可以为......
  • nginx 从一个路径访问另一个路径怎么跳转
    访问stap的路径跳转到根目录下,并且带上之前的参数#rewrite^/stap/(.*)$/$1permanent;访问stap目录代理到下面目录#location/stap/{#rewrite^/stap/(.*)$https://abgg.fxxxuuuppppmppyyai.com/$1permanent;#}访问stap目录代理到下面目录#location/s......