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;
}
}
}
}

最新文章

  1. Behind RabbitMQ Exchange Types
  2. 未完结第八节 JBPM流程节点
  3. php修改和增加xml结点属性
  4. linux内核驱动中_IO, _IOR, _IOW, _IOWR 宏的用法与解析(引用)
  5. 编写高效SQL最佳实践
  6. CUGBACM_Summer_Tranning 组队赛解题报告
  7. php实现和c#一致的DES加密解密
  8. Java课程设计—学生成绩管理系统
  9. 如何更简单方便地执行SQL操作?
  10. SQOOP安装部署
  11. SHELL-收集Oracle已应用的PSU信息
  12. Android: 创建一个AlertDialog对话框,必须按确定或取消按钮才能关闭对话框,禁止按[返回键]或[搜索键]关闭
  13. OpenGL 画出雷达动态扫描效果(二) 非底图
  14. HTML转义
  15. 软件魔方制作系统启动盘并安装win8系统
  16. HTTPS链式编程——AFNetworking 3.0
  17. Android 集成百度统计
  18. 队列 102 Binary Tree Level Order Traversal
  19. salt demo 环境
  20. 重复造轮子系列--dijkstra算法

热门文章

  1. 使用bottle进行web开发(6):Response 对象
  2. 【互动问答分享】第7期决胜云计算大数据时代Spark亚太研究院公益大讲堂
  3. mimikatz-域密码获取神器
  4. HDU 5969 最大的位或【贪心/按位或/思维】
  5. poj2778(AC 自动机)
  6. python3-关于GitHub的最基本操作
  7. 1.6(学习笔记)Session
  8. [SourceTree]--记录Win10 安装SourceTree免注册登陆
  9. iOS 10 资料整理笔记
  10. iOS开发技巧&mdash;&mdash;关闭Autoresizing开启Autolayout