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. ...
随机推荐
- Windows 结构化异常
结构化异常不能用于需要调用对象析构函数的函数中 __try{ } __except(){ } __try{ } __finally{ }
- 虚拟机上安装ArchLinux笔记
安装前的自白: 想使用ArchLinux,就直接在虚拟机上先装一个玩起来先.虚拟机使用的是Vmware,下载免费的个人版本就可以了. Arch Linux的版本为2016.4.1 内核为4.4.5 在 ...
- 解决ORA-00824: cannot set sga_target due to existing
今天Linux服务器机子重启了下,Oracle启动不起来,提示:解决ORA-00824: cannot set sga_target due to existing 看了很多解决方法,发现下面这个特别 ...
- SmartUpload实现文件上传时file和表单文本同时提交的问题
JSP页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...
- windows下监测tomcat7内存使用情况
在conf里面的server-user文件里添加 <tomcat-users> <role rolename="admin-gui"/> <user ...
- SQL Server 2012安装后找不到服务器名称的解决办法!!!
网上说使用localhost即可,确实没错,但是有的仍旧会报出无法找到错误,我在无法通过的时候又重新安装了SQLServer,这次选中全部默认安装,之前使用的是选择安装,然后发现多了几个配置,其中有一 ...
- Openlayers+Geoserver(一):项目介绍以及地图加载
项目验收完,趁着事情不是很多,对这个项目进行梳理.我主要负责地图模块,网站其他模块主要有两个,一个是报表,主要是100多张报表,技术没有难度,主要是工作量的问题.另一个是数据的校验,就是 ...
- [LeetCode] 435 Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [UCSD白板题] Least Common Multiple
Problem Introduction The least common multiple of two positive integers \(a\) and \(b\) is the least ...
- [Xpand] Error 1 Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
原因是用的vs2015 默认用了c#6 ,但是在没安装.net 4.6 环境下编译失败. 解决办法很简单,修改 6 为 5 做降级就可以了. 1.nuget install DotNetCompile ...