Unity:UI lerp将对象传送到位置

时间:2018-12-01 20:07:34

标签: c# unity3d vector

我正在尝试创建一个菜单,该菜单在按下按钮时从屏幕的侧面滑动,我希望它可以滑动并缓和。问题是我的代码无法正常工作。它总是总是立即传送到最终位置。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonPress : MonoBehaviour
{
    public RectTransform rect;
    private Toggle tog;

    public void Start ()
    {
        tog = GetComponent<Toggle>();
        tog.onValueChanged.AddListener(delegate { ToggleChanged(rect, tog); });
    }

    public void SmoothMove (Vector2 startpos, Vector2 endpos, float seconds)
    {
        var t = 0.0f;
        while (t <= 1.0f)
        {
            t += Time.deltaTime / seconds;
            rect.anchoredPosition = Vector2.Lerp(startpos, endpos, Mathf.SmoothStep(0.0f, 1.0f, Mathf.SmoothStep(0.0f, 1.0f, t)));
        }
    }

    public void ToggleChanged(RectTransform rect, Toggle tog)
    {
        if (tog.isOn)
        {
            SmoothMove(new Vector2(-870, 0), new Vector2(-200, 0), 0.05f);
        }
        else
        {
            SmoothMove(new Vector2(-200, 0), new Vector2(-870, 0), 0.05f);
        }
    }
}

因此,基本上,我正在为切换更改创建一个侦听器,然后在切换切换时跟踪位置。我从另一个线程获得了SmoothMove()函数,但是没有用。另外,我在SmoothMove()的最后一个参数中尝试了很多值,但始终相同。

感谢您的评论!

编辑:

好吧,我弄清楚了运动部件。我不得不将SmoothMove()的修饰符更改为IEnumerator,然后在调用该函数时必须使用StartCoroutine()和voilà!运动很平稳,但是仍然不是“轻松”。我现在只需要忍受它。

固定代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonPress : MonoBehaviour
{
    public RectTransform rect;
    private Toggle tog;

    public void Start ()
    {
        tog = GetComponent<Toggle>();
        tog.onValueChanged.AddListener(delegate { ToggleChanged(rect, tog); });
    }

    IEnumerator SmoothMove (Vector2 startpos, Vector2 endpos, float seconds)
    {
        var t = 0.0f;
        while (t <= 1.0f)
        {
            t += Time.deltaTime / seconds;
            rect.anchoredPosition = Vector2.Lerp(startpos, endpos, Mathf.SmoothStep(0.0f, 1.0f, Mathf.SmoothStep(0.0f, 1.0f, t)));
            yield return null;
        }
    }

    public void ToggleChanged(RectTransform rect, Toggle tog)
    {
        if (tog.isOn)
        {
            StartCoroutine(SmoothMove(new Vector2(-874, 0), new Vector2(-200, 0), 0.3f));
        }
        else
        {
            StartCoroutine(SmoothMove(new Vector2(-200, 0), new Vector2(-874, 0), 0.3f));
        }
    }
}

好的。经过数小时的测试,这是我能想到的最佳解决方案。我做了一些奇怪的动作,因为我可以在协程完成执行之前按一下按钮,所以我也把StopCoroutine放在了那里。通过更改t值的增量,我也获得了缓解的效果,但是即使我自己也不知道它是如何工作的,它仍然可以工作。

这是最终代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonPress : MonoBehaviour
{
    public RectTransform rect;
    public float ToggleOnCoordinateX;
    public float ToggleOnCoordinateY;
    public float ToggleOffCoordinateX;
    public float ToggleOffCoordinateY;
    public float TimeToMove;
    private Toggle tog;
    private Coroutine co;

    public void Start()
    {
        tog = GetComponent<Toggle>();
        tog.onValueChanged.AddListener(delegate { ToggleChanged(rect, tog); });
    }

    IEnumerator SmoothMove(Vector2 endpos, float seconds)
    {
        var t = 0.0f;
        while (t <= 1.0f)
        {
            t += Time.deltaTime * (Time.deltaTime / seconds);
            rect.anchoredPosition = Vector2.Lerp(rect.anchoredPosition, endpos, t);
            yield return null;
        }
    }

    public void ToggleChanged(RectTransform rect, Toggle tog)
    {
        if (tog.isOn)
        {
            try
            {
                StopCoroutine(co);
            }
            catch
            {
                co = StartCoroutine(SmoothMove(new Vector2(ToggleOnCoordinateX, ToggleOnCoordinateY), TimeToMove));
                 return;
            }
            co = StartCoroutine(SmoothMove(new Vector2(ToggleOnCoordinateX, ToggleOnCoordinateY), TimeToMove));
        }
        else
        {
            try
            {
                StopCoroutine(co);
            }
            catch
            {
                co = StartCoroutine(SmoothMove(new Vector2(ToggleOffCoordinateX, ToggleOffCoordinateY), TimeToMove));
                return;
            }
            co = StartCoroutine(SmoothMove(new Vector2(ToggleOffCoordinateX, ToggleOffCoordinateY), TimeToMove));
        }
    }
}

0 个答案:

没有答案
相关问题