首页 > 代码库 > 自适应分辨率
自适应分辨率
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; } } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。