从C#代码对Unity 4.6中的图像进行动画/移动/平移/补间

强尼

如何使用Unity 4.6中的C#代码在位置A到位置B之间移动/设置动画/平移/切换图像?

假设Image是一个GameObject,那么它可以是一个Button或其他任何东西。为此必须有一个班轮,对吗?我已经搜索了一段时间,但是我能看到的即开即用的东西就是在Update中完成的工作,我坚信在Update中完成工作并不是编写脚本的快速方法。

马祖

如果您想自己做运动,可以使用以下方法:

public Vector3 targetPosition = new Vector3(100, 0, 0);
public float speed = 10.0f;
public float threshold = 0.5f;
void Update () {
    Vector3 direction = targetPosition - transform.position;
    if(direction.magnitude > threshold){
        direction.Normalize();
        transform.position = transform.position + direction * speed * Time.deltaTime;
    }else{ 
        // Without this game object jumps around target and never settles
        transform.position = targetPosition;
    }
}

或者,您可以下载例如DOTween包,然后启动补间:

public Vector3 targetPosition = new Vector3(100, 0, 0);
public float tweenTime = 10.0f;
void Start () {
    DOTween.Init(false, false, LogBehaviour.Default);
    transform.DOMove(targetPosition, tweenTime);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章