LoginSignup
3
2

More than 5 years have passed since last update.

unity > androidアプリ > 解像度を変更する > Screen.SetResolution (width, height, false);

Last updated at Posted at 2015-08-23
動作確認
Unity 5.1.2-f1 on MacOS X 10.8.5

androidにアプリを転送してみたが、UI>Buttonなどがすごく小さく表示されてしまった。

try01

とりあえず、以下のようにしてButtonなどの表示を大きくできる。

    void Start () {
        float screenRate = 4.0f;
        int width = (int)(Screen.width / screenRate);
        int height = (int)(Screen.height / screenRate);
        Screen.SetResolution (width, height, false);
    }

参考
http://tamilabo.blog.fc2.com/blog-entry-14.html

try02

どうもtry01のコードで実装すると Application.LoadLevel()で別シーンから再度戻ってきた時にも再度解像度を変更してしまう。

以下のようにmyButtonの幅がスクリーンの50%になるようにすると、シーン間の遷移による問題は避けることができる。

MainCameraControl.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MainCameraControl : MonoBehaviour {

    public Text msgText; // set Text to show message
    public Button myButton; // set UI>Button whose width is used as standard

    void Start () {
        float aspect = (float)Screen.height / (float)Screen.width;
        float buttonRatio = 0.5f; // 50%
        int buttonWidth = (int)myButton.GetComponent<RectTransform> ().rect.width;
        float newWidth, newHeight;

        newWidth = buttonWidth / buttonRatio;
        newHeight = newWidth * aspect;

        Screen.SetResolution ((int)newWidth, (int)newHeight, false);
        msgText.text = newWidth.ToString () + "x" + newHeight.ToString ();
    }   
}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2