Unity3d + NGUI 的多分辨率适配
| 宽 | 高 | 宽高比 |
| 960 | 640 | 1.5 |
| 1136 | 640 | 1.775 |
| 1024 | 768 | 1.3333 |
| 2048 | 1536 | 1.3333 |
| 宽 | 高 | 宽高比 |
| 800 | 480 | 1.6667 |
| 854 | 480 | 1.7792 |
| 1280 | 720 | 1.7778 |
| 960 | 540 | 1.7778 |
| 1280 | 800 | 1.6 |
| 960 | 640 | 1.5 |
| 1184 | 720 | 1.6444 |
| 1920 | 1080 | 1.7778 |

1 using UnityEngine;
2 using System.Collections;
3
4 /// <summary>
5 /// 根据设备的宽高比,调整camera.orthographicSize. 以保证UI在不同分辨率(宽高比)下的自适应
6 /// 须与UIAnchor配合使用
7 /// 将该脚本添加到UICamera同一节点上
8 /// </summary>
9
10 [RequireComponent(typeof(UICamera))]
11 public class UICameraAdjustor : MonoBehaviour
12 {
13 float standard_width = 1024f;
14 float standard_height = 600f;
15 float device_width = 0f;
16 float device_height = 0f;
17
18 void Awake()
19 {
20 device_width = Screen.width;
21 device_height = Screen.height;
22
23 SetCameraSize();
24 }
25
26 private void SetCameraSize()
27 {
28 float adjustor = 0f;
29 float standard_aspect = standard_width / standard_height;
30 float device_aspect = device_width / device_height;
31
32 if (device_aspect < standard_aspect)
33 {
34 adjustor = standard_aspect / device_aspect;
35 camera.orthographicSize = adjustor;
36 }
37 }
38 }


1 using UnityEngine;
2 using System.Collections;
3
4 /// <summary>
5 /// 根据设备的宽高比,调整UISprite scale, 以保证全屏的背景图在不同分辨率(宽高比)下的自适应
6 /// 将该脚本添加到UISprite同一节点上
7 /// 须与UICameraAdjustor脚本配合使用
8 /// </summary>
9
10 [RequireComponent(typeof(UISprite))]
11 public class UIBackgroundAdjustor : MonoBehaviour
12 {
13 float standard_width = 1024f;
14 float standard_height = 600f;
15 float device_width = 0f;
16 float device_height = 0f;
17
18 void Awake()
19 {
20 device_width = Screen.width;
21 device_height = Screen.height;
22
23 SetBackgroundSize();
24 }
25
26 private void SetBackgroundSize()
27 {
28 UISprite m_back_sprite = GetComponent<UISprite>();
29
30 if (m_back_sprite != null && UISprite.Type.Simple == m_back_sprite.type)
31 {
32 m_back_sprite.MakePixelPerfect();
33 float back_width = m_back_sprite.transform.localScale.x;
34 float back_height = m_back_sprite.transform.localScale.y;
35
36 float standard_aspect = standard_width / standard_height;
37 float device_aspect = device_width / device_height;
38 float extend_aspect = 0f;
39 float scale = 0f;
40
41 if (device_aspect > standard_aspect) //按宽度适配
42 {
43 scale = device_aspect / standard_aspect;
44
45 extend_aspect = back_width / standard_width;
46 }
47 else //按高度适配
48 {
49 scale = standard_aspect / device_aspect;
50
51 extend_aspect = back_height / standard_height;
52 }
53
54 if (extend_aspect >= scale) //冗余尺寸足以适配,无须放大
55 {
56 }
57 else //冗余尺寸不足以适配,在此基础上放大
58 {
59 scale /= extend_aspect;
60 m_back_sprite.transform.localScale *= scale;
61 }
62 }
63 }
64 }
Unity3d + NGUI 的多分辨率适配的更多相关文章
- 【转】Unity3d + NGUI 的多分辨率适配
原文地址:http://www.cnblogs.com/cqgreen/p/3348154.html 一.当下移动设备的主流分辨率(数据来自“腾讯分析移动设备屏幕分辨率分析报告”) 1.1 iOS ...
- Unity3d + NGUI 的多分辨率适配(黑边)
原地址:http://www.2cto.com/kf/201310/250921.html 一.当下移动设备的主流分辨率(数据来自“腾讯分析移动设备屏幕分辨率分析报告”) 1.1 iOS设备的分辨率主 ...
- Unity NGUI的多分辨率适配
参考链接:http://blog.csdn.net/mfc11/article/details/17681429,作者:CSDN mfc11 1.NGUI默认的适配方式: NGUI默认是适配方式是根据 ...
- Unity3d + UGUI 的多分辨率适配
原文地址:http://blog.csdn.net/dingkun520wy/article/details/49471789 1.Canvas的属性配置 2.Canvas Scaler的属性配置 3 ...
- Unity3D NGUI自适应屏幕分辨率(2014/4/17更新)
原地址:http://blog.csdn.net/asd237241291/article/details/8126619 原创文章如需转载请注明:转载自 脱莫柔Unity3D学习之旅 本文链接地址: ...
- Cocos与Cocos2d-x协作教程——多分辨率适配
http://www.cocoachina.com/bbs/read.php?tid-288123.html Cocos v2.1开始新增了一种新的多分辨率适配方案:流式布局. 这种布局相比Cocos ...
- Android多分辨率适配
前一阶段开发android项目,由于客户要求进行多分辨率适配,能够支持国内主流的分辨率手机.因此经过了几次开发走了很多弯路,目前刚刚领略了android多分辨率适配的一些方法. 先介绍一下所走的弯路, ...
- Android多分辨率适配经验总结
Android多分辨率适配是一件很有意义但是比较麻烦的事情,网上有很多关于多分辨率适配的文章,多数文章讲解的都是整个APP的图片比较规则,可以将图片做成9图来完成多分辨率适配,但是对于一些游戏类应 ...
- Unity3D NGUI学习(一)血条
这次来讲讲Unity3D NGUI这个插件的学习,这个插件是收费的,不过去网上可以下载得很多可用版本.用来做用户的交互UI,学习起来比较简单 第一步,导入NGUI包 http://pan.baidu. ...
随机推荐
- LogNet4日志框架使用
.百度一下log4dll下载 .webconfig 里的<configSetions>节点中添加 <section name="log4net" type=&qu ...
- sqlite3 根据实体自动生成建表语句
public class BuildSqlTool { public static string GetCreateTableSql(object t) { //CREATE TABLE &quo ...
- SGA(System Global Area)
系统激活时在内存内规划的一个固定的区域,用于存储每位使用者所需存取的数据和必备的系统信息.这个区域成为系统全局区. 数据块缓存区:存放读取数据文件的数据块副本,或者曾经处理过的数据.有效减少读取数据时 ...
- Config文件的读写
using System; using System.Configuration; using System.Xml; namespace COMMON { public class ConfigHe ...
- Java学习笔记 01 基本数据类型、标识符、关键字和运算符
一.基本数据类型 基本数据类型 数据类型 内存空间(8位等于1字节) 取值范围 备注 byte 8位 -128~127 short 16位 -32768~32767 int 32位 -2147 ...
- Word 2007 文档结构图混乱
Word 2007在编写大型文档时经常出现文档结构图混乱的情况,经过多番检索试验,得出结论: 绝对有效的临时性解决方案:在打开word的时候左下角会有提示word自动更新文档样式,按esc键取消,然后 ...
- win10 上运行 curl_init() 函数一直报错的解决办法
[问题现象] 1.把 APACHE 的 ZIP 包解压到目录,比如 d:\apache24\ 2.把 PHP 的 ZIP 包解压到目录,比如:d:\php56\ apache 与 php 与 MySQ ...
- C# 实现 任意多边形切割折线算法
1. 内容简介 本文旨在解决任意多边形切割折线,获取切割之后的折线集合. 本文实现的算法内容包括:判断两条线段是否相交,如若相交,获取交点集合.对线上的点集,按斜率方向排序.判断点是否在多边形内 ...
- [UCSD白板题] Pairwise Distinct Summands
Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...
- c# 根据文件流查看文件真实格式
今天在做图片注册的功能的时候,测试提出一个问题:将随便一个非图片文件将后缀名改为jpg或其他,上传时应检验图片合法性.然后同事给提供了根据文件流前两个字节判断文件真实格式的思路,代码如下: publi ...