【问题标题】:How to determine scale to increase sprite width to screen width如何确定比例以将精灵宽度增加到屏幕宽度
【发布时间】:2016-06-03 22:28:23
【问题描述】:

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

更新

下面是我为扩展精灵宽度以占据全屏宽度而实现的 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)
         }
        }

【问题讨论】:

  • 你能解释一下你为什么要这样做吗?也许还有另一种方法可以做到这一点
  • @Programmer 我想统一完成链接中所做的事情 - 在两端扩展正方形(游戏对象)的宽度,直到它占据屏幕的宽度。 KnightOfDragon 的方法是缩放宽度以使其具有动画效果。我想使用相同的方法,但我对另一种方法持开放态度,因为我刚刚意识到这种方法会对具有多种颜色的精灵有问题,因为它们也会拉伸,例如(选中的块看起来不太好当以类似的方式拉伸时)。我希望这能让我更好地了解我想要完成的工作。
  • @Programmer 我会看一下,我应该有一个示例,我在 Swift 中实现了解决方案。找到问题后,我会更新问题。但正如我所提到的,拉伸宽度方法存在问题,因为它会破坏具有不止一种颜色的精灵。也许另一种方法可能是采用一个精灵,它是原始精灵的一条带(例如,如果精灵是 50x50,原始精灵的 5x50 部分),这将在原始精灵的两端重复动画,直到整个宽度被覆盖。请让我知道您对这种方法的看法。
  • @Programmer 我刚刚用 Swift 实现更新了这个问题。

标签: c# unity3d unity5


【解决方案1】:

这一切都取决于屏幕的纵横比和使用 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 );
}

}

【讨论】:

    【解决方案2】:

    您可以在 x 轴全屏中缩放 UI 的Image。只需获取RectTransform,然后将其sizeDelta属性修改为设备的屏幕大小或Canvas的大小。

    下面的函数可以在 x、y 或 x 和 y 轴全屏缩放 Unity UI Image。要缩放的Image 必须在Canvas 之下。将Sprite 分配给Image 组件的Source Image,下面的代码应该可以工作。

    //在此处将UI Image附加到编辑器中的scacle

    public GameObject image;
    

    按比例缩放

    3 秒内以 X 轴全屏缩放:

    StartCoroutine(scaleToFullScreen(image, 0, 3f));
    

    3 秒内以 Y 轴全屏缩放:

    StartCoroutine(scaleToFullScreen(image, 1, 3f));
    

    3 秒内以 X 轴和 Y 轴全屏缩放:

    StartCoroutine(scaleToFullScreen(image, 2, 3f));
    

    比例函数

    bool isScaling = false;
    IEnumerator scaleToFullScreen(GameObject imageToScale, int scaleType, float byTime)
    {
        if (isScaling)
        {
            yield break;
        }
    
        if (scaleType < 0 || scaleType > 2)
        {
            Debug.Log("Invalid ScaleType. Valid Scale Types X:0,  Y:1,  XandY:3");
            yield break;
        }
        isScaling = true;
    
        Canvas canvas = imageToScale.GetComponentInParent<Canvas>();
        float x, y;
    
        if (canvas != null)
        {
            x = canvas.pixelRect.width;
            y = canvas.pixelRect.height;
        }
        else
        {
            x = Screen.width;
            y = Screen.height;
        }
    
        RectTransform rect = imageToScale.GetComponent<RectTransform>();
        if (rect == null)
        {
            rect = imageToScale.AddComponent<RectTransform>();
        }
    
        //Center the position of the image so that it will be resized equally
        rect.anchoredPosition3D = new Vector3(0, 0, rect.anchoredPosition3D.z);
    
    
        //The default Size
        Vector2 originalScale = rect.sizeDelta;
    
        //The new scale we want to scale texture to
        Vector2 newScale = originalScale;
    
        if (scaleType == 0)
        {
            newScale.x = x;
        }
        else if (scaleType == 1)
        {
            newScale.y = y;
        }
        else if (scaleType == 2)
        {
            newScale.x = x;
            newScale.y = y;
        }
    
    
        float counter = 0;
        while (counter < byTime)
        {
            counter += Time.deltaTime;
            rect.sizeDelta = Vector2.Lerp(originalScale, newScale, counter / byTime);
            yield return null;
        }
        isScaling = false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多