くらげになりたい。

くらげのようにふわふわ生きたい日曜プログラマなブログ。趣味の備忘録です。

【チラ裏】画面サイズに背景を合わせるときにやった試行錯誤【Unity】

チラ裏。ホントのメモです。あしからず

UnityでAndoridを開発している時に試行錯誤した時のJavaScriptコード。
1枚絵の画像を背景にしている時、解像度が異なってもいい感じに横幅のスケールをしたかった。

#pragma strict

var gos : GameObject[];
private var ScreenSize : Vector2;
private var rect : Rect;
private var size : Vector3;
private var deltaScale : Vector3;

function Start() {
    ScreenSize = Utils.getScreenSize();
    Debug.Log("ScreenSize = (" + ScreenSize.x + ", " + ScreenSize.y + ")");
    Debug.Log("Screen.width, Screen.height = (" + Screen.width + ", " + Screen.height + ")");
    rect = gameObject.GetComponent(SpriteRenderer).sprite.rect;
    Debug.Log("rect.width, rect.height = (" + rect.width + " , " + rect.height + ")");
    size = gameObject.GetComponent(SpriteRenderer).bounds.size;
    Debug.Log("size = " + size);


    Debug.Log("Before scale = (" + transform.localScale.x + ", " + transform.localScale.y + ")");
    // setScale();
    // setScale2();
    setScale3();
    Debug.Log("Delta Scale = (" + deltaScale.x + ", " + deltaScale.y + ")");
    transform.localScale += deltaScale;
    Debug.Log("After scale = (" + transform.localScale.x + ", " + transform.localScale.y + ")");

    for(var go : GameObject in gos) {
        go.transform.localScale += deltaScale;
    }
}

function Update() {
}

private function setScale2() {
    var Width : float = Screen.width;
    var Height : float = Screen.height;

    var newWidth : float = Height * 5 / 8;

    var scaleWidth : float = newWidth / Width;
    // var scaleWidth : float = Width / newWidth;
    deltaScale = new Vector3(scaleWidth - transform.localScale.x, 0, 0);
}

private function setScale() {
    var scaleWidth : float = 0;
    var scaleHeight : float = 0;   

    // scaleHeight = Screen.height / rect.height;
    // scaleHeight = rect.height / Screen.height;

    // scaleWidth = Screen.width / rect.width;
    // scaleWidth = rect.width / Screen.width;
    scaleWidth = Screen.width / size.x;
    // scaleWidth = size.x / Screen.width;

    // deltaScale = new Vector3(scaleWidth - transform.localScale.x, scaleHeight - transform.localScale.y, 0);
    deltaScale = new Vector3(scaleWidth - transform.localScale.x, 0, 0);
}

private function setScale3() {
    // var scaleWidth : float = Screen.width / rect.width;
    // var scaleWidth : float = rect.width / Screen.width;

    // var scaleWidth : float = Screen.width / size.x;
    // var scaleWidth : float = size.x / Screen.width;

    // var scaleWidth : float = ScreenSize.x / size.x;
    // var scaleWidth : float = size.x / ScreenSize.x;

    // var scaleWidth : float = ScreenSize.x / (size.x * transform.localScale.x);
    // var scaleWidth : float = (size.x * transform.localScale.x) / ScreenSize.x;

    var scaleWidth : float = (ScreenSize.x * transform.localScale.x) / size.x;
    // var scaleWidth : float = (size.x * transform.localScale.x) / ScreenSize.x;

    var scaleWidthF : float = Mathf.Ceil(100 * scaleWidth);
    scaleWidth = scaleWidthF / 100;

    deltaScale = new Vector3(scaleWidth - transform.localScale.x, 0, 0);
}