在Unity中给定持续时间的等效于SKAction scaleToX [英] Equivalent of SKAction scaleToX for a given duration in Unity

查看:239
本文介绍了在Unity中给定持续时间的等效于SKAction scaleToX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Unity中的给定持续时间内缩放精灵的x轴.基本上,我是在以下列方式使用Swift之前完成此操作的,

I am trying to scale the x axis of a sprite over a given duration in Unity. Basically I've done this before using Swift in the following way,

let increaseSpriteXAction = SKAction.scaleXTo(self.size.width/(mySprite?.sprite.size.width)!, duration:0.2)
mySprite!.sprite.runAction(increaseSpriteXAction)
mySprite!.sprite.physicsBody = SKPhysicsBody(rectangleOfSize:mySprite!.sprite.size, center: centerPoint)

,但是在Unity中甚至没有找到scaleToX的等效项.我将非常感谢您的协助.

but haven't even found an equivalent for scaleToX in Unity. I'll really appreciate some assistance.

推荐答案

使用组合CoroutineTime.deltaTimeMathf.Lerp,则p您将能够复制SKAction.scaleXTo函数的功能.

Use combination Coroutine, Time.deltaTime and Mathf.Lerp then pyou will be able to replicate the function of SKAction.scaleXTo function.

bool isScaling = false;

IEnumerator scaleToX(SpriteRenderer spriteToScale, float newXValue, float byTime)
{
    if (isScaling)
    {
        yield break;
    }
    isScaling = true;

    float counter = 0;
    float currentX = spriteToScale.transform.localScale.x;
    float yAxis = spriteToScale.transform.localScale.y;
    float ZAxis = spriteToScale.transform.localScale.z;

    Debug.Log(currentX);
    while (counter < byTime)
    {
        counter += Time.deltaTime;
        Vector3 tempVector = new Vector3(currentX, yAxis, ZAxis);
        tempVector.x = Mathf.Lerp(currentX, newXValue, counter / byTime);
        spriteToScale.transform.localScale = tempVector;
        yield return null;
    }

    isScaling = false;
}

调用它:

public SpriteRenderer sprite;
void Start()
{
    StartCoroutine(scaleToX(sprite, 5f, 3f));
}

它将在 3 秒内将x轴比例从原来的值更改为 5 .同样的功能也可以轻松扩展为与y轴一起使用.

It will change the x-axis scale from whatever it is to 5 in 3 seconds. The-same function can easily be extended to work with the y axis too.

类似的问题: SKAction.moveToX .

这篇关于在Unity中给定持续时间的等效于SKAction scaleToX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆