Unity3d + NGUI 的多分辨率适配

移动端的多机型适配

现在要介绍的是《锁链战记》这款游戏的适配方法,这种适配方法是UI是一个基础尺寸,背景是一个基础尺寸,背景比UI多出的部分是一些没有实际作用的部分,这样的适配方式避免了在iPhone5这样的小屏幕上镶边。

首先设定UIRoot的Scaling Style属性,如果是电脑现在FixedSize,如果要打包到移动端选择FixedSizeOnMobiles.

我这里是以960*640为UI基础尺寸所以这里填写640高。

下面编写脚本BaseAspect.cs

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(UICamera))]
public class BaseAspect : MonoBehaviour
{
    float standard_width = 960f;        //初始宽度
    float standard_height = 640f;       //初始高度
    float device_width = 0f;                //当前设备宽度
    float device_height = 0f;               //当前设备高度
    public float adjustor = 0f;         //屏幕矫正比例
    void Awake()
    {
      
        //获取设备宽高
        device_width = Screen.width;
        device_height = Screen.height;
        //计算宽高比例
        float standard_aspect = Screen.width / standard_height;
        float device_aspect = device_width / device_height;
        //计算矫正比例
        if (device_aspect < standard_aspect)
        {
            adjustor = standard_aspect / device_aspect;
            //Debug.Log(standard_aspect);
        }
        Debug.Log("屏幕的比例" + adjustor);
        if (adjustor < 2 && adjustor > 0)
        {
            camera.orthographicSize = adjustor;
        }
      
    }
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}


将该脚本添加到UICamera同一节点上


这样就可以实现适配了,这种适配的方式会有镶边的存在。

蓝色的就是要镶嵌边框的地方。

原文地址:https://www.cnblogs.com/lexiaoyao-jun/p/5208260.html