zoukankan      html  css  js  c++  java
  • UIScreenAdaptive

    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;
                }
            }
        }
    }
  • 相关阅读:
    51nod 1565模糊搜索(FFT)
    51nod 1851俄罗斯方块(trick)
    可持久化线段树(主席树)模板
    BZOJ2191:Splite
    BZOJ4197:[NOI2015]寿司晚宴
    BZOJ3198:[SDOI2013]SPRING
    BZOJ1500:[NOI2005]维修数列
    BZOJ3527:[ZJOI]力
    BZOJ3160:万径人踪灭
    CODE[VS]1372:DNA
  • 原文地址:https://www.cnblogs.com/123ing/p/3974814.html
Copyright © 2011-2022 走看看