码迷,mamicode.com
首页 > 其他好文 > 详细

NGUI屏幕自适应

时间:2015-10-22 17:26:58      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

在NGUI的UIRoot里,把Scaling Style (缩放比例类型)设置为Fixed Size On Mobiles   (固定大小在手机)

因为各个手机分辨率不一样,所以,Manual Height这个值就不是固定不变的了。要Screen.width 和Screen.height来动态的计算它的实际高度,动态的修改这个值。

Min/Max inum Height:这就是支持的最大高度,和最小高度一般都是 640 到 1536。

然后,把这个脚本挂到UIRoot上,来实时的计算Manual Height这个值!

技术分享
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class UIRootExtend : MonoBehaviour {
 5 
 6     public int ManualWidth = 960;
 7     public int ManualHeight = 640;
 8     
 9     private UIRoot _UIRoot;
10     
11     void Awake()
12     {
13         _UIRoot = this.GetComponent<UIRoot>();
14     }
15     
16     void FixedUpdate()
17     {
18         if (System.Convert.ToSingle(Screen.height) / Screen.width > System.Convert.ToSingle(ManualHeight) / ManualWidth)
19             _UIRoot.manualHeight = Mathf.RoundToInt(System.Convert.ToSingle(ManualWidth) / Screen.width * Screen.height);
20         else
21             _UIRoot.manualHeight = ManualHeight;
22     }
23 }
View Code

当然,在设计UI的时候,是要根据设定的ManualWidth和ManualHeight的大小去设计才能起效。

NGUI屏幕自适应

标签:

原文地址:http://www.cnblogs.com/gzmumu/p/4901503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!