Unity3d 如何以与画布中心的距离统一缩放对象?

Unity3d 如何以与画布中心的距离统一缩放对象?,unity3d,scrollview,unity5,scaling,Unity3d,Scrollview,Unity5,Scaling,我需要为滚动视图实现跑步机效果,即当滚动中心对象时,需要在远离中心时减小比例,反之亦然?有人能给我指出正确的方向吗?我已经在从元素计算滚动视图的中心,以实现捕捉效果 *用非常便宜的方法解决可能不适合每个人。这个过程是通过获取从滚动视图中的按钮/元素到中心的距离,并根据所需的比例使用所述距离进行学习 istReposition = center.GetComponent<RectTransform>().position.x - bttn.GetComponent<RectTra

我需要为滚动视图实现跑步机效果,即当滚动中心对象时,需要在远离中心时减小比例,反之亦然?有人能给我指出正确的方向吗?我已经在从元素计算滚动视图的中心,以实现捕捉效果

*用非常便宜的方法解决可能不适合每个人。这个过程是通过获取从滚动视图中的按钮/元素到中心的距离,并根据所需的比例使用所述距离进行学习

istReposition = center.GetComponent<RectTransform>().position.x - bttn.GetComponent<RectTransform>().position.x;
distance = Mathf.Abs(distReposition);
Vector3 minscale = Vector3.one * maximundistanceScale;
Vector3 maxScale = Vector3.one * minimundistanceScale;
bttn[i].GetComponent<RectTransform>().localScale = Vector3.Lerp(maxScale,minscale, distance);

堆栈溢出不是脚本服务:下次请尝试发布您已经尝试过的内容

无论如何,你可以这样做:

仅当跑步机水平时,获取从UI元素到x轴滚动视图中心的距离;如果跑步机垂直,获取从y轴滚动视图中心的距离 将该值除以画布宽度的一半 获取结果的绝对值 现在,当元素位于画布的中间时,值的范围为0到1,当元素位于画布的侧面时,值的范围为0 这方面的快速实施将是:

[RequireComponent(typeof(RectTransform))]
public class TestScript : MonoBehaviour
{
    [SerializeField]
    private float m_CenterScale;
    [SerializeField]
    private float m_BorderScale;
    [SerializeField]
    private AnimationCurve m_ScalingCurve; // Must be from 1 to 0 on y axis and from 0 to 1 on x axis

    protected override void Update()
    {
        float scrollviewCenterPosition = GetComponentInParent<ScrollRect>().GetComponent<RectTransform>().position.x;
        float distanceFromCenter = transform.position.x - scrollviewCenterPosition ;
        float ratio = Mathf.Abs(distanceFromCenter / (GetComponentInParent<ScrollRect>().GetComponent<RectTransform>().rect.width * 0.5f));
        float scaleValue = m_BorderScale + (m_CenterScale - m_BorderScale) * m_ScalingCurve.Evaluate(ratio);
        (transform as RectTransform).localScale = Vector3.one * scaleValue;
    }
}
请注意,我添加了AnimationCurve属性,以便能够轻松控制缩放效果:曲线应在[0:1]开始,并在[1:0]结束,如下所示:


希望这能有所帮助,

谢谢你。。事实上,我已经开始工作了,谢谢你的回复,很抱歉没有附上我的脚本,因为它是意大利面编码的,甚至我都不知道它的头尾。。我会把你的答案记为正确的,虽然我没有试过,但它会对其他人有帮助,我想你是受欢迎的!如果你有时间考虑把你的工作代码作为你的问题或答案的编辑,这样它也可以帮助未来的用户。祝你的其他项目好运:完成后,我只粘贴了关键部分,希望一切顺利