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

 

相关文章:

  • 2022-02-08
  • 2022-03-05
  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2021-12-20
  • 2021-09-19
猜你喜欢
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
相关资源
相似解决方案