NSSwift
NSSwift

Reputation: 395

How to determine scale to increase sprite width to screen width

I am trying to achieve something similar to How to increase (animate) the width of the square on both ends in Unity. How can I determine the scale by which to increase the width (of the sprite) for it to fill the whole screen width?

UPDATE

Below is the Swift code for I implemented for expanding the sprite width to take the full screen width:

       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)
         }
        }

Upvotes: 2

Views: 1428

Answers (2)

Programmer
Programmer

Reputation: 125275

You can scale Image of an UI in x-axis full screen. Just get the RectTransform then modify the sizeDelta property of it to the Screen size of the device or the size of the Canvas.

The function below can scale Unity UI Image in x, y or both x and y axis full screen. The Image to scale must be under Canvas. Assign a Sprite to the Source Image of the Image component the code below should work.

//Attach the UI Image to scacle in the Editor here

public GameObject image;

To Scale:

Scale in X-axis Full Screen in 3 seconds:

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

Scale in Y-axis Full Screen in 3 seconds:

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

Scale in X-axis AND Y-axis Full Screen in 3 seconds:

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

The Scale function:

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;
}

Upvotes: 0

Dr Doodle
Dr Doodle

Reputation: 31

It all depends on the aspect ratio of the screen and the size of the object with the SpriteRenderer. You need to scale up the gameobject that holds the spriterenderer by a factor where you take these into consideration.

[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 );
}

}

Upvotes: 2

Related Questions