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. C、C++ static 的作用

    1.隐藏 当同时编译多个文件时,所有未加static关键字的全局变量和函数都具有全局可见性. 例如:同时编译两个源文件 //ghz.c #include <stdio.h> char a ...

  2. HDU 2829 Lawrence

    $dp$,斜率优化. 设$dp[i][j]$表示前$i$个数字切了$j$次的最小代价.$dp[i][j]=dp[k][j-1]+p[k+1][i]$.观察状态转移方程,可以发现是一列一列推导出来的.可 ...

  3. DeprecationWarning: current URL string parser is deprecated解决方法

    我最近在使用mongoDB的时候,发现了这个警告语句,纳闷了,按照官方文档的教程去连接数据库还能出错,也是醉了. 后来尝试去阅读相关资料,发现只是需要将{ useNewUrlParser: true ...

  4. Problem R: 求斐波那契数列的前n项值

    #include<stdio.h> int main() { int n; while(scanf("%d",&n)!=EOF){ int x1,x2,i,x; ...

  5. matlab和c混合编程

    参考: http://blog.sciencenet.cn/blog-620659-579885.html http://baike.baidu.com/link?url=HnHd3lU9mcVXwh ...

  6. 动态NAT地址转换

    1.配置路由器的端口ip地址(注意外网和内网ip地址的设置) Router(config)#inter f0/0 Router(config-if)#ip add 192.168.1.1 255.25 ...

  7. 【记录一下】phpMyAdmin 4.5.0-beta1 发布,要求 PHP 5.5

    详情点击: [开源中国]http://www.oschina.net/news/65696/phpmyadmin-4-5-0-beta1 [phpMyAdmin]https://www.phpmyad ...

  8. C#分析URL参数获取参数和值得对应列表(二)

    不错博客: [C#HttpHelper]官方产品发布与源码下载---苏飞版http://www.sufeinet.com/thread-3-1-1.html http://blog.csdn.net/ ...

  9. Delphi 设置时间格式

    // 设置WINDOWS系统的短日期的格式SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, 'yyyy-MM-dd'); Applicat ...

  10. ASP.NET MVC DropdownList的使用

    1:直接使用HTML代码写 <select name="year"> <option value="2011">2010</opt ...