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

标签 c# unity-game-engine

我正在尝试实现类似于 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/

相关文章:

c# - 为什么在尝试 Assembly.ReflectionOnlyLoad 时没有执行 ReflectionOnlyAssemblyResolve?

c# - 隐藏字段不在 MVC 中存储某些值

c# - 通用 C# 代码和加号运算符

c# - NUnit 嵌套集合比较

c# - 在具有通配符 Equatability 的类上实现 GetHashCode

c# - 将 Unity3d WebGL 项目构建连接到数据库时遇到问题,但在编辑器中工作正常

c# - 应用 'bouncy' 物理 Material 后,我无法让我的玩家统一跳跃相同的高度

unity-game-engine - 如何计算实例化克隆(即敌人)的剩余数量

ide - 在 Unity3D 中,点击“播放”时是否可以保持场景 View 聚焦?

android - 在 Unity 中销毁实例化对象