zoukankan      html  css  js  c++  java
  • 自适应分辨率

    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;
                }
            }
        }
    }
  • 相关阅读:
    git 常用命令
    重要知识点
    HTML 标记语言
    js类型转换
    伪数组变数组 js
    在 JavaScript 中为什么 typeof null 的结果是 object?
    HTML中href、src区别
    解决for循环插入同一元素无法重复插入问题
    HTML5-语义化
    mount 和 /etc/fstab关系。
  • 原文地址:https://www.cnblogs.com/123ing/p/3816013.html
Copyright © 2011-2022 走看看