gpt4 book ai didi

c# - 如何确定比例以将 Sprite 宽度增加到屏幕宽度

转载 作者:行者123 更新时间:2023-11-30 23:25:15 25 4
gpt4 key购买 nike

我正在尝试实现类似于 How to increase (animate) the width of the square on both ends 的东西在团结中。如何确定增加( Sprite 的)宽度以填充整个屏幕宽度的比例?

更新

下面是我实现的扩展 Sprite 宽度以占据全屏宽度的Swift代码:

       func expandEnemy () {

spritePosBeforeScaleX = CGPointMake((enemy?.sprite.position.x)!, (enemy?.sprite.anchorPoint.y)!)

enemy?.sprite.anchorPoint = CGPointMake((enemy?.sprite.position.x)! / self.size.width, (enemy?.sprite.anchorPoint.y)!)
let enemyScalingAction = SKAction.scaleXTo(self.size.width / (enemy?.sprite.size.width)!, duration: 1.0)
enemy!.sprite.runAction(enemyScalingAction)

delay(0.1)
{
center = CGPointMake(enemy!.sprite.size.width / 2 - (enemy!.sprite.size.width * enemy!.sprite.anchorPoint.x), enemy!.sprite.size.height / 2 - (enemy!.sprite.size.height * enemy!.sprite.anchorPoint.y))
enemy!.sprite.physicsBody = SKPhysicsBody(rectangleOfSize: enemy!.sprite.size, center: center)
}
}

最佳答案

这完全取决于屏幕的纵横比和带有 SpriteRenderer 的对象的大小。您需要将包含 spriterenderer 的游戏对象按比例放大,并将这些因素考虑在内。

[ExecuteInEditMode]
public class SpriteToScreen : MonoBehaviour {
public float sprw = 256f;
public float sprh = 256f;
float unitspp = 100f;
public float scrw = 0f;
public float scrh = 0f;
public float aspect = 0f;
public float spr_aspect = 1f;
public float factorY = 0.017578125f;
public void Update(){
scrw = Screen.width;
scrh = Screen.height;
aspect = scrw / scrh;
unitspp = this.GetComponent<SpriteRenderer>().sprite.pixelsPerUnit;
sprw = this.GetComponent<SpriteRenderer>().sprite.bounds.size.x * unitspp;
sprh = this.GetComponent<SpriteRenderer>().sprite.bounds.size.y * unitspp;
spr_aspect = sprw / sprh;
this.transform.localScale = new Vector3( (1152f / sprh * aspect) / spr_aspect,
1152f / sprh,
1 );
}

}

关于c# - 如何确定比例以将 Sprite 宽度增加到屏幕宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37414068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com