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

UIScreenAdaptive

时间:2014-09-16 15:29:40      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   div   art   sp   

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;
            }
        }
    }
}

 

UIScreenAdaptive

标签:des   style   blog   color   io   ar   div   art   sp   

原文地址:http://www.cnblogs.com/123ing/p/3974814.html

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