Unity发布游戏到安卓时NGUI屏幕分辨率的调整

     一.unity发布到安卓的游戏的开发环境一般为480*800,横屏时大概是800*480就够了。

首先,在UIRoot下创建一个Widget作为容器,将所有ui作为容器的子控件,容器下的所有UI控件随着Container进行缩放,接着为container添加脚本:

 
  
using UnityEngine;
using System.Collections;

public class AdjustUI : MonoBehaviour {
    public float stand_width=800;
    public float stand_height=480;
    float drivice_width;
    float drivice_height;
    float scale;
 // Use this for initialization
 void Start () {
        drivice_width = Screen.width;
        drivice_height = Screen.height;
        float dirvice_aspect = drivice_width / drivice_height;
        float stand_aspect = stand_width / stand_height;
        if (dirvice_aspect > stand_aspect)
        {
            scale = dirvice_aspect / stand_aspect;
            transform.localScale = new Vector3(scale, 1, 1);
        }
        else {
            scale = stand_aspect / dirvice_aspect;
            transform.localScale = new Vector3(1,scale,1);
        }
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
}

 
  


将stand_width和stand_height设置为开发环境的宽高;
UIRoot的Scaling Style还需要设置一下 Unity发布游戏到安卓时NGUI屏幕分辨率的调整_第1张图片
二.接着给摄像机添加脚本:
using UnityEngine;
using System.Collections;

public class AdjustCamera : MonoBehaviour {
    public float stand_width=800;
    public float stand_height=480;
    float dirvice_width;
    float dirvice_height;
   
 // Use this for initialization
 void Start () {
        float scale=0;
        dirvice_width = Screen.width;
        dirvice_height = Screen.height;
        float dirice_aspect = dirvice_width / dirvice_height;
        float stand_aspect = stand_width / stand_height;
        if (dirice_aspect < stand_aspect)
        {
            scale = stand_aspect / dirice_aspect;
            Camera.main.orthographicSize = scale;
        }
 }

}
三,这些完成后,发布游戏是需要修改一些设置:
Unity发布游戏到安卓时NGUI屏幕分辨率的调整_第2张图片
将游戏改为左横屏,好了以上就是NGUI屏幕分辨率遇到的问题,不足之处还望指出,大家共同学习

你可能感兴趣的:(Unity游戏开发)