using UnityEngine;

namespace Com.Xyz.UI
{
[ExecuteInEditMode]
[RequireComponent(typeof(UIRoot))]
public class UIScreenAdaptive : MonoBehaviour
{
public int aspectRatioWidth = ;
public int aspectRatioHeight = ;
public bool runOnlyOnce = false;
private UIRoot mRoot;
private bool mStarted = false; private void Awake()
{
UICamera.onScreenResize += OnScreenResize;
} private void OnDestroy()
{
UICamera.onScreenResize -= OnScreenResize;
} private void Start()
{
mRoot = NGUITools.FindInParents<UIRoot>(this.gameObject); mRoot.scalingStyle = UIRoot.Scaling.FixedSize; this.Update();
mStarted = true;
} private void OnScreenResize()
{
if (mStarted && runOnlyOnce)
{
this.Update();
}
} private void Update()
{
float defaultAspectRatio = aspectRatioWidth * 1f / aspectRatioHeight;
float currentAspectRatio = Screen.width * 1f / Screen.height; if (defaultAspectRatio > currentAspectRatio)
{
int horizontalManualHeight = Mathf.FloorToInt(aspectRatioWidth / currentAspectRatio);
mRoot.manualHeight = horizontalManualHeight;
}
else
{
mRoot.manualHeight = aspectRatioHeight;
} if (runOnlyOnce && Application.isPlaying)
{
this.enabled = false;
}
}
}
}

UIScreenAdaptive的更多相关文章

  1. ngui自适应

    增加UIROOT using UnityEngine; namespace Com.Xyz.UI { [ExecuteInEditMode] [RequireComponent(typeof(UIRo ...

随机推荐

  1. sql ---- count 误区

    select count(字段名) from table group by 字段; 分组后的内容统计. count统计的字段是 不包括空的数据.

  2. 中小型mysql数据库的备份与恢复

    #转载请联系 备份到桌面 cd /home/chichung/Desktop # 切换到桌面 mysqldump -u root -p db_jingdong>jd.sql # 重定向写入 jd ...

  3. python3生成测试数据,并写入ssdb

    import pyssdb import random import time c = pyssdb.Client() chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoP ...

  4. PO/POJO/BO/DTO/VO的区别(转)

    PO :persistent object持久对象 1 .有时也被称为Data对象,对应数据库中的entity,可以简单认为一个PO对应数据库中的一条记录. 2 .在hibernate持久化框架中与i ...

  5. javascript面试题(一)(转载)

    1,判断字符串是否是这样组成的,第一个必须是字母,后面可以是字母.数字.下划线,总长度为5-20 var reg = /^[a-zA-Z][a-zA-Z_0-9]{4,19}$/; /*注意:1.要用 ...

  6. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  7. Opencv利用Mat访问像素值

    如果是采用Mat形式存储,想要访问灰度图像的灰度值,可以采用如下方法: 如果是彩色图像,采用如下方法: 说明: 其中gray_value中存放灰度值,image是读入的图像,i表示行,j表示列: co ...

  8. 容斥原理 求M以内有多少个跟N是互质的

    开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理!   本题是求[a,b]中与n ...

  9. Swift中计算String的长度

        extension String {     var length: Int { return countElements(self) }  // Swift 1.1 } extension ...

  10. Saga的实现模式——进化(Saga implementation patterns – variations)

    在之前的几个博客中,我主要讲了两个saga的实现模式: 基于command的控制者模式 基于事件的观察者模式 当然,这些都不是实现saga的唯一方式.我们甚至可以将这些结合起来. 发布者——收集者 回 ...