相当于在Unity中给定持续时间的SKAction scaleToX

时间:2016-05-23 10:24:37

标签: swift unity3d unity5 skaction

我试图在Unity中的给定持续时间内缩放精灵的x轴。基本上我已经在以下方式使用Swift之前完成了这个,

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的等价物。我真的很感激一些帮助。

1 个答案:

答案 0 :(得分:2)

使用组合CoroutineTime.deltaTimeMathf.Lerp,然后pyou将能够复制SKAction.scaleXTo函数的功能。

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 更改为 5 。同样的功能也可以很容易地扩展到与y轴一起工作。

类似问题:SKAction.moveToX