![459c22f8850190647766f0a938360c25.png](https://img-blog.csdnimg.cn/img_convert/459c22f8850190647766f0a938360c25.png)
今天在做一些NGUI UI的时候发现了一些问题,检查了半天才发现是自己的屏幕尺寸取错了。
Screen.width Screen.height 可以获取设备屏幕的宽高,但是它并不是NGUI的宽高。比如你想做一个全屏的UISprite,这样的代码是错误的。
sprite.width = Screen.width ;
sprite.height = Screen.height
正确方法如下,其实就是计算出真实的展示高度比再乘Screen的宽高就可以了。
UIRoot root = GameObject.FindObjectOfType<UIRoot>();
if (root != null) {
float s = (float)root.activeHeight / Screen.height;
int height = Mathf.CeilToInt(Screen.height * s);
int width = Mathf.CeilToInt(Screen.width * s);
}