Unity animateWithDuration等效,动画gameobject到位置

时间:2016-06-13 16:04:12

标签: ios animation unity3d

来自iOS背景我很难在2D中将简单的GameObject从A移动到B. 有没有类似animateWithDuration(duration: NSTimeInterval, animations: () -> Void, completion: ((Bool) -> Void)?)

的内容

我认为在更新中更改转换很不方便。我无法使用动画视图和动画制作工具,因为无法使用此工具更改目标位置。

1 个答案:

答案 0 :(得分:0)

来自iOS背景, 我建议如下:

class AnimationModule: MonoBehaviour
{

    public delegate void AnimationBlock(float progress);

    public Task animateWithDuration(float duration, AnimationBlock animation, Action completion)
        {
            StartCoroutine(coroutine__animation(duration, animation, delegate
            {
                completion();
            }));

            return Task.Delay(millisecondsDelay: (int)(duration * 1000));
        }

    private IEnumerator coroutine__animation(float duration, AnimationBlock animation, Action completion)
        {
            float startingTime = Time.time;
            float timeCounter  = startingTime;

            while (timeCounter < (startingTime + duration))
            {
                float p = (timeCounter - startingTime) / duration;
                animation(p);
                timeCounter = Time.time;
                yield return null;
            }

            completion();
        }
}

然后以这种方式使用:

Task anim1 = GetComponent<AnimationModule>().animateWithDuration(duration: 2.0f, animation: (float p) => {

                                    card.transform.localPosition = position_from + p * (new Vector3(100, 0, 0));

                                }, completion: () => {

                                    Debug.Log("animateWithDuration, completion");

                                });

考虑到回答问题的重要一点是接近iOS风格。

它有效,但我不知道它在性能方面是否表现良好。

我使用实验性的Mono来使用Task之类的东西。