首页 > 其他 > 详细

自适应分辨率

时间:2014-06-30 23:03:01      阅读:392      评论:0      收藏:0      [点我收藏+]

3.xx 以上 绑定到UIRoot 

using UnityEngine;

namespace Com.Xyz.UI
{
    [ExecuteInEditMode]
    [RequireComponent(typeof(UIRoot))]
    public class UIScreenAdaptive : MonoBehaviour
    {
        public int aspectRatioWidth = 1280;
        public int aspectRatioHeight = 720;
        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;
            }
        }
    }
}

 

自适应分辨率,布布扣,bubuko.com

自适应分辨率

原文:http://www.cnblogs.com/123ing/p/3816013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!